Switch가 눌러질 때 LED가 켜졌다가 꺼졌다가 되는거야.
인터럽트를 사용할까 했는데 UNO에서 지원되는 인터럽트는 2개 밖에 없더라고.
그래서 일반핀을 이용해서 했어.
Switch가 눌러질 때, 즉 HIGH에서 LOW로 될때(Falling Edge) LED가 ON/OFF 되.
int ledRPin = 3, ledGPin = 5, ledBPin = 6; //Red LED 3, Green LED 5, Blue LED 6
int sw1Pin = 7, sw2Pin = 8, sw3Pin = 9;
int ledRStatus = HIGH;
int ledGStatus = HIGH;
int ledBStatus = HIGH;
int sw1BeforeValue = HIGH;
int sw2BeforeValue = HIGH;
int sw3BeforeValue = HIGH;
void setup() {
// put your setup code here, to run once:
pinMode(ledRPin, OUTPUT);
pinMode(ledGPin, OUTPUT);
pinMode(ledBPin, OUTPUT);
digitalWrite(ledRPin, HIGH);
digitalWrite(ledGPin, HIGH);
digitalWrite(ledBPin, HIGH);
pinMode(sw1Pin, INPUT);
pinMode(sw2Pin, INPUT);
pinMode(sw3Pin, INPUT);
Serial.begin(115200);
Serial.print("Hello Arduino\n");
}
void loop() {
// put your main code here, to run repeatedly:
int sw1Value, sw2Value, sw3Value;
sw1Value = digitalRead(sw1Pin);
sw2Value = digitalRead(sw2Pin);
sw3Value = digitalRead(sw3Pin);
sw1Toggle(sw1Value);
sw2Toggle(sw2Value);
sw3Toggle(sw3Value);
digitalWrite(ledRPin, ledRStatus);
digitalWrite(ledGPin, ledGStatus);
digitalWrite(ledBPin, ledBStatus);
}
////////////////////////////////////////////////////////
int sw1Toggle(int swValue) {
if (swValue == LOW) {
if (sw1BeforeValue == HIGH) {
//Serial.println("LED ON");
ledRStatus = !ledRStatus;
sw1BeforeValue = LOW;
return true;
}
else {
sw1BeforeValue = LOW;
return false;
}
}
else {
sw1BeforeValue = HIGH;
return false;
}
}
////////////////////////////////////////////////////////
int sw2Toggle(int swValue) {
if (swValue == LOW) {
if (sw2BeforeValue == HIGH) {
//Serial.println("LED ON");
ledGStatus = !ledGStatus;
sw2BeforeValue = LOW;
return true;
}
else {
sw2BeforeValue = LOW;
return false;
}
}
else {
sw2BeforeValue = HIGH;
return false;
}
}
////////////////////////////////////////////////////////
int sw3Toggle(int swValue) {
if (swValue == LOW) {
if (sw3BeforeValue == HIGH) {
//Serial.println("LED ON");
ledBStatus = !ledBStatus;
sw3BeforeValue = LOW;
return true;
}
else {
sw3BeforeValue = LOW;
return false;
}
}
else {
sw3BeforeValue = HIGH;
return false;
}
}