I have been working to code the Arduino program to control 3 LED timing separately using 3 separate button. What I want to do is:
1. To lit the LED independently using 3 different push buttons
2. The LED can be lit using separate timing.
Eg;
-push button 1 will lit LED1 for 3 seconds
-push button 2 will lit LED2 for 2 seconds
-push button 3 will lit LED3 for 5 seconds
or I can just use the same timing for 3 LED but still able to adjust the timing independently.
3. All 3 LED can be lit all at once, or just LED2 and LED3 at once or LED1 and LED2 at once or LED1 and LED3 at once. This has been an issue if you try to use delay() as the timing. In my code here, there is NO delay() at all.
unsigned long onTime; unsigned long onTime2; unsigned long onTime3; const int button1 =2; const int button2=7; const int button3=6; const int LED1=8; const int LED2=12; const int LED3=4; void setup() { onTime = millis(); onTime2 = millis(); onTime3 = millis(); pinMode(LED1, OUTPUT); //Initialize pin 8 as status LED pinMode(button1, INPUT); // PIN 2 pinMode(LED2, OUTPUT); //LED2 pinMode(button2, INPUT); //input at 7 pinMode(LED3, OUTPUT); //LED2 pinMode(button3, INPUT); //input at 7 } void loop() { if (digitalRead(button1) == LOW) // ON- STARTS TIMER { digitalWrite(LED1, HIGH); // LED1 comes On onTime = millis(); } if(millis() - onTime > 5000) //5000=5 SEC, 10000=6 SEC,15000=16 SEC { digitalWrite(LED1, LOW); // LED goes off onTime = onTime2; } //start new button operation if (digitalRead(button2) == LOW) // ON- STARTS TIMER { digitalWrite(LED2, HIGH); // LED comes On onTime2 = millis(); } //if(onTime > 0 && millis() - onTime > 1000) //5000=5 SEC, 10000=6 SEC,15000=16 SEC if(millis() - onTime2 > 5000) //5000=5 SEC, 10000=6 SEC,15000=16 SEC { digitalWrite(LED2, LOW); // LED goes off onTime2 = onTime3; } //start new button operation if (digitalRead(button3) == LOW) // ON- STARTS TIMER { digitalWrite(LED3, HIGH); // LED comes On onTime3 = millis(); } //if(onTime > 0 && millis() - onTime > 1000) //5000=5 SEC, 10000=6 SEC,15000=16 SEC if(millis() - onTime3 > 5000) //5000=5 SEC, 10000=6 SEC,15000=16 SEC { digitalWrite(LED3, LOW); // LED goes off onTime3 = 0; } }
I have TESTED the code and it works flawlessly. Be free to edit, share or do anything to it!
The code here has been edited by me from the original source here: http://forum.arduino.cc/index.php?topic=154966.0