int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int sw = 8;
boolean sw000 = HIGH;
int ledStatus = 0;
void setup() {
// put your setup code here, to run once:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(sw, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int swInput = debounceSwitch(sw, sw000);
if((swInput == HIGH) && (sw000 == LOW)) {
//กำหนดสถานะ led
ledStatus = ledStatus +1;
if (ledStatus > 4)ledStatus = 0;
}
sw000 = swInput;
if (ledStatus == 0){
ledOutput(LOW,LOW,LOW,LOW);
} else if (ledStatus == 1){
ledOutput(LOW,LOW,LOW,HIGH);
} else if (ledStatus == 2) {
ledOutput(LOW,LOW,HIGH,HIGH);
} else if (ledStatus == 3) {
ledOutput(LOW,HIGH,HIGH,HIGH);
} else {
ledOutput(HIGH,HIGH,HIGH,HIGH);
}
}
void ledOutput (int ledVal1, int ledVal2, int ledVal3, int ledVal4) {
digitalWrite (led1,ledVal1);
digitalWrite (led2,ledVal2);
digitalWrite (led3,ledVal3);
digitalWrite (led4,ledVal4);
}
boolean debounceSwitch(int swPin, boolean LastLogic) {
boolean currentLogic = digitalRead (swPin);
if (currentLogic != LastLogic) {
delay(2);
currentLogic = digitalRead(swPin);
}
return currentLogic;
}
No comments:
Post a Comment