Help please!

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:

  1. On start up both LEDs are off.
  2. Push a button it lights LED2 waits for the delay then LED2 off.
  3. 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!

Your current program using delay() will not handle what you want to do.
You have to search forum on how to use interupt to get what you want :wink:

Welcome to the Forum. Please read the advice regarding the posting of your code and the use of code tags.

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); 
        }
    }
}

If you read the advice, you will find out that a title of "Help please" is not very useful because most people here are looking for help.

Thank you for the advice guys i will change the pointed out errors in my post. Will also try to incorporate interrupt and post the results afterwards.

Please take what @sudhabo wrote with a pinch of salt - you almost certainly do NOT need interrupts (I assume that's what they meant by the :wink: )

Don't use delay() if you want a responsive program because it blocks the Arduino until it completes.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

PS ... if you modify your Original Post you can change the Title to something more meaningful