difficulty in timing and delaying processes

Hi everyone , i have the setup below where :

at button hold:

activate set1() function

  1. turn on playPin

  2. 500uS interval, then turn off playPin

activate set2() function

  1. turn on ledPin1

  2. wait for 5oouS then turn on ledPin2

  3. wait for 5oouS then turn on ledPin3

  4. wait for 5oouS then turn on ledPin4

*led1, led2, led3and led4 mus be hold to ON state as long as the button is being held

at button release:

activate set1() function again

  1. turn on playPin

  2. 500uS interval, then turn off playPin

activate set2() function

  1. turn off ledPin1

  2. wait for 5oouS then turn off ledPin2

  3. wait for 5oouS then turn off ledPin3

  4. wait for 5oouS then turn off ledPin4

*led1, led2, led3and led4 mus be hold to OFF state as long as the button is at rest

these two functions are of independent of each other , and the sketch is working properly . However,

I am having difficulty delaying the operation of set2 for this kind of setup without affecting the timing of the processes inside set2 and set1 .

That is, if i hold the button down, set1 will process and then set2 must wait for a period of 5s before it starts its process .

I’m just a beginner and i am in still in the process of learning things around arduino .

Thanks in advance for the help guys .

here’s the code

const int playPin = 9;
const int ledPin1 = 10;
const int ledPin2 = 11;
const int ledPin3 = 12;
const int ledPin4 = 13;

const int DisplayButton = 2;
unsigned long startTime; // will store last time LED was updated
unsigned long startTime2;


int lastButtonState = 0;
int lastButtonState2 = 0;
unsigned long interval = 500UL; // interval at which to blink (milliseconds)
unsigned long interval2 = 500UL;
int ledState;
int ledState2;

void setup() 
{
pinMode(ledPin1, OUTPUT); 
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(playPin, OUTPUT);
pinMode(DisplayButton, INPUT); 
}

void loop() 
{


set1();
set2();
}

void set1() 
{ 
{
int buttonState = digitalRead(DisplayButton);
if (buttonState != lastButtonState) // on any state change...
{
startTime2 = millis();// .... reset the timer
ledState = !ledState;

}
lastButtonState = buttonState;

if (millis() - startTime2 <= interval2)
{
digitalWrite(playPin, HIGH);
}
else
{
digitalWrite(playPin, LOW);
} 
} 
}


void set2()
{
int buttonState2 = digitalRead(DisplayButton);



if (buttonState2 != lastButtonState2) // on any state change...
{
startTime = millis();// .... reset the timer
ledState2 = !ledState2;
}
lastButtonState2 = buttonState2;

if (ledState2)
{
if (millis() - startTime <= interval)
{
digitalWrite(ledPin1, HIGH);
}
else if (millis() - startTime <= interval * 2)
{
digitalWrite(ledPin2, HIGH);
}
else if (millis() - startTime <= interval * 3)
{
digitalWrite(ledPin3, HIGH);
}
else if (millis() - startTime <= interval * 4)
{
digitalWrite(ledPin4, HIGH);
}
}

else 
{
if (millis() - startTime <= interval)
{
digitalWrite(ledPin1, LOW);
}
else if (millis() - startTime <= interval * 2)
{
digitalWrite(ledPin2, LOW);
}
else if (millis() - startTime <= interval * 3)
{
digitalWrite(ledPin3, LOW);
}
else if (millis() - startTime <= interval * 4)
{
digitalWrite(ledPin4, LOW);
}
}
}

It seems to me like a simple solution would be to get rid of your set1() and set2() functions and make two new ones that cover the states of the button being pressed and the button being released. For example:

Button_Pressed_Function()

  1. turn on playPin

  2. turn on ledPin1

  3. Delay 500us (did you mean microseconds or milliseconds?)

  4. turn off playPin

5 turn on ledPin2

  1. Delay 500us

  2. turn on ledPin3

  3. Delay 500us

  4. turn on ledPin4

Button_Released_Function()

  1. turn on playPin

  2. turn off ledPin1

  3. Delay 500us

  4. turn off playPin

5 turn off ledPin2

  1. Delay 500us

  2. turn off ledPin3

  3. Delay 500us

  4. turn off ledPin4

In your higher level loop, you can keep track of when the button was pushed and released and then call the appropriate function.

I hope that makes sense!

-Bill

phalanx:
3. Delay 500us (did you mean microseconds or milliseconds?)

Good question. This from the above code suggests it's msecs. It's a big difference when it comes to how-to-code. ``` unsigned long interval = 500UL; // interval at which to blink (milliseconds) ```