• 강의
  • 커뮤니티
  • 북마크
GCR

자료실

교육자료 | 코D알고 [하]_ 15강.스마트 전등 만들기

최고관리자
2021.01.27 14:26 111 0

본문

#include <SoftwareSerial.h>

#define redLED 9
#define blueLED 7
#define greenLED 5
SoftwareSerial bluetooth(2,3); // TX, RX
char set = 0;

int redButton = LOW;
int blueButton = LOW;
int greenButton = LOW;

void setup( ){
  Serial.begin(9600);
  bluetooth.begin(9600);
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(blueLED, OUTPUT);
}

void loop( ){
  if(bluetooth.available( )){
    set = bluetooth.read();
    Serial.print(“입력 값(R, G, B, O 중 입력하세요) : ”);
    Serial.println(set);

    switch(set){
      case ‘G’ : //Green의 G
        if(greenButton == LOW) {
          greenButton =HIGH;
        }
        else if(greenButton == HIGH) {
          greenButton =LOW;
        }
        digitalWrite(greenLED, greenButton);
        break;
      case ‘B’ : //Blue의 B
        if(blueButton == LOW){
          blueButton = HIGH;
        }
        else if(blueButton == HIGH){
          blueButton =LOW;
        }
        digitalWrite(blueLED, blueButton);
        break;
      case ‘R’ : //Red의 R
        if(redButton == LOW){
          redButton = HIGH;
        }
        else if(redButton == HIGH){
          redButton = LOW;
        }
        digitalWrite(redLED, redButton);
        break;
      case ‘O’ : // Off의 O
        redButton = LOW;
        blueButton = LOW;
        greenButton = LOW;
        digitalWrite(greenLED, greenButton);
        digitalWrite(blueLED, blueButton);
        digitalWrite(redLED, redButton);
        break;
      default :
        break;
    }
  }
}

댓글목록 0

등록된 댓글이 없습니다.