Hi everyone i just started some projects as a hobby i enjoyed reading all the forum guides here and acquired a little understanding how the arduino program works. I got here a sketch wayback from 2014 (thanks to luisilva the author) and with a little change i got it to work for my needs but i want to add more spice to it and since i dont know any complex methods i hope anybody here could give me tips.
Heres the code:
int ledPin1 = 11;
int ledPin2 = 10;
int switchPin = 8;
int count = 0;
boolean lastButton;
boolean currentButton = false;
boolean ledOn = false;
void setup() {
pinMode(switchPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT) ;
count = 0;
}
//debounce function to stabilise the button
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
void loop() {
lastButton = currentButton;
currentButton = debounce(lastButton);
if (lastButton == false && currentButton == true)
{
if (count == 0)
{
count++;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
else if (count == 1)
{
count++;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
else if (count == 2)
{
count --;
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
delay(500);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
}
}
The sketch above does the following:
- On start up both LEDs are off.
- Push a button it lights LED2 waits for the delay then LED2 off.
- Push button again it lights LED1 now waits for the delay then LED1 off. Then back at step 2.
It works as it should and i would like to add this feature.
I pushed the button for the first time, Led2 lights for the duration of the delay but i changed my mind pushed the button in the middle of the delay time and it lights led1 instead with the duration of time the led2 took before me pushing the button to light Led1, Led1 turns off.
It should also work the other way around.
I pushed the button for the second time to light Led1 it waits for the delay before turning off but in the middle of the delay i pushed the button again and led2 lights with the duration of led1 took to light before me interrupting by pushing the button.
Does it make any sense?
I hope its doable. Thanks in advance guys!