아까 Switch로 RGB LED제어를 해봤는데.
추가로 현재의 LED와 Switch의 상태를 Serial로 출력해보는 것을 해봤어.
회로는 아까와 동일하고 동영상도 뭐 다를게 없어.
하지만 컴퓨토로 시리얼이 출력이 되지.
int LedR = 3,LedG = 5,LedB = 6; //Red LED 3, Green LED 5, Blue LED 6
int SW1 = 7, SW2 = 8, SW3 = 9;
int Red_Serial_cnt=0;
int Green_Serial_cnt=0;
int Blue_Serial_cnt=0;
void setup() {
// put your setup code here, to run once:
pinMode(LedR,OUTPUT);
pinMode(LedG,OUTPUT);
pinMode(LedB,OUTPUT);
digitalWrite(LedR,HIGH);
digitalWrite(LedG,HIGH);
digitalWrite(LedB,HIGH);
pinMode(SW1,INPUT);
pinMode(SW2,INPUT);
pinMode(SW3,INPUT);
Serial.begin(115200);
Serial.print("Hello Arduino\n");
}
void loop() {
// put your main code here, to run repeatedly:
int sw1_val, sw2_val, sw3_val;
sw1_val = digitalRead(SW1);
sw2_val = digitalRead(SW2);
sw3_val = digitalRead(SW3);
//////////////////////////////////////////////////
if(sw1_val == 1){
digitalWrite(LedR,HIGH);
if(Red_Serial_cnt == 0){
Serial.print("SW1 OFF, Red LED OFF \n");
Red_Serial_cnt++;
}
}
else{
digitalWrite(LedR,LOW);
if(Red_Serial_cnt == 1){
Serial.print("SW1 ON, Red LED ON \n");
Red_Serial_cnt=0;
}
}
////////////////////////////////////////////////////////
if(sw2_val == 1){
digitalWrite(LedG,HIGH);
if(Green_Serial_cnt == 0){
Serial.print("SW2 OFF, Green LED OFF \n");
Green_Serial_cnt++;
}
}
else{
digitalWrite(LedG,LOW);
if(Green_Serial_cnt == 1){
Serial.print("SW2 ON, Green LED ON \n");
Green_Serial_cnt=0;
}
}
////////////////////////////////////////////////////////
if(sw3_val == 1){
digitalWrite(LedB,HIGH);
if(Blue_Serial_cnt == 0){
Serial.print("SW3 OFF, Blue LED OFF \n");
Blue_Serial_cnt++;
}
}
else{
digitalWrite(LedB,LOW);
if(Blue_Serial_cnt == 1){
Serial.print("SW3 ON, Blue LED ON \n");
Blue_Serial_cnt=0;
}
}
}