/*
Counter Sw To On/Off LED Togle bit
- CntOn = 1 Wiat 3 Sec --> LED On , CntOff = 1 Wiat 3 Sec --> LED1 Off
*/
#define led1 4 // the number of the pushbutton pin
#define led2 5
#define swPin 2 // the number of the LED pin
int timer = 0; // Innitail timer;
byte cnt = 0;
boolean st1 = 0;
boolean st2 = 0;
/* ----------------------------------------------------------- */
void setup() {
Serial.begin(9600);
Serial.println();
pinMode(swPin, INPUT_PULLUP); // initialize the pushbutton pin as an input:
pinMode(led1, OUTPUT); // initialize the LED pin as an output:
pinMode(led2, OUTPUT); // initialize the LED pin as an output:
digitalWrite(led1, 0);
digitalWrite(led2, 0);
Serial.println(" Wait Time To On ! ");
}
/* ----------------------------------------------------------- */
void loop() {
if (digitalRead(swPin) == 0) { // Press Sw
while (digitalRead(swPin) == 0) delay(10);
cnt++; // Counter Press Sw
timer = 0; // Reset timer
if (cnt > 2)
cnt = 2;
}
/* ----------------- ON-Off LED -------------- */
if (timer == 200) { // 1000 ms = 1sec
if (cnt == 1) {
st1 = !st1; // Togle Bit st
digitalWrite(led1, st1); // led1 ON/Off
Serial.println("cnt" + String (cnt));
Serial.println(" LED1 Is On");
cnt = 0; timer = 0;
delay(1000);
}
if (cnt == 2) {
st2 = !st2; // Togle Bit st
digitalWrite(led2, st2); // led1 ON/Off
Serial.println("cnt" + String (cnt));
cnt = 0; timer = 0;
delay(1000);
}
/* ------------------------------------------- */
cnt = 0;
}
timer++;
delay(10);
};
/* ----------------------------------------------------------- */