/*
Counter Sw To On/Off LED Togle bit
- CntOn = 1 Wiat 3 Sec --> LED On , CntOff = 1 Wiat 3 Sec --> LED1 Off
*/
#define led1 2 // the number of the pushbutton pin
#define led2 3
#define swPin 7 // 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++;
Serial.print("cnt ="); Serial.println(cnt);
delay(10); // 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);
}
if(st1 == 1) // led1 ON/Off
Serial.println(" LED1 Is No ");
else
Serial.println(" LED1 Is Off ");
cnt = 0;
timer = 0;
if (cnt == 2) {
st2 = !st2; // Togle Bit st
digitalWrite(led2, st2);
if(st2 == 1) { // led1 ON/Off
Serial.println(" LED2 Is Active ");}
else {Serial.println("LED2 Is Off");}
cnt = 0; timer = 0;
}
/* ------------------------------------------- */
cnt = 0;
}
timer++;
};