int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int sw = 8;
boolean sw000 = HIGH;
int ledStatus = 0;
int ledStep1[4][4] = { 1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};
int ledStep2[4][4] = { 0,0,0,1,
0,0,1,0,
0,1,0,0,
1,0,0,0};
int ledStep3[4][4] = { 0,1,0,1,
1,0,1,0,
1,1,0,0,
0,0,1,1};
// แถวที่จะอ่านจาก ledStep
int ledRow = 0;
int delayCount = 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 > 3)ledStatus = 0;
}
sw000 = swInput; //เก็บสถานะปัจจุบัน
int ledTemp1,ledTemp2,ledTemp3,ledTemp4;
if (ledStatus == 0){
ledTemp1 = LOW;
ledTemp2 = LOW;
ledTemp3 = LOW;
ledTemp4 = LOW;
// ledOutput(LOW,LOW,LOW,LOW);
} else if (ledStatus == 1){
ledTemp1 = ledStep1[ledRow][0];
ledTemp2 = ledStep1[ledRow][1];
ledTemp3 = ledStep1[ledRow][2];
ledTemp4 = ledStep1[ledRow][3];
// ledOutput(LOW,LOW,LOW,HIGH);
} else if (ledStatus == 2) {
ledTemp1 = ledStep2[ledRow][0];
ledTemp2 = ledStep2[ledRow][1];
ledTemp3 = ledStep2[ledRow][2];
ledTemp4 = ledStep2[ledRow][3];
// ledOutput(LOW,LOW,HIGH,HIGH);
//} else if (ledStatus == 3) {
// ledOutput(LOW,HIGH,HIGH,HIGH);
} else {
ledTemp1 = ledStep3[ledRow][0];
ledTemp2 = ledStep3[ledRow][1];
ledTemp3 = ledStep3[ledRow][2];
ledTemp4 = ledStep3[ledRow][3];
// ledOutput(HIGH,HIGH,HIGH,HIGH);
}
ledOutput(ledTemp1,ledTemp2,ledTemp3,ledTemp4);
delayCount = delayCount +1;
if (delayCount > 500) {
delayCount = 0;
ledRow = ledRow +1;
if (ledRow > 3) ledRow = 0;
}
//delay(100);
}
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