1st button to start, 2nd button to stop time driven sequence.

Hi there Gents.

I have some coding problems with an project.

I have 2 buttons, if button 1 is pressed the led will go on and run for a set amount of time and then will automatically switch off.

I want to bring in an 2nd button that will cancel the whole sequence at any giving time.

Here is what i have so far... I have tried everything that I could think off and nothing will work.

here is an link to my project and the code

int pinButton1 = 2;
int pinButton2 = 3;
int pinButton3 = 4;
int pinButton4 = 5;
int Relay1 = 13;
int Relay2 = 12;
int stateRelay1 = LOW;
int stateRelay2 = LOW;
int stateButton1;
int stateButton2;
int stateButton3;
int stateButton4;
int previous1 = LOW;
int previous2 = LOW;
int previous3 = HIGH;
int previous4 = HIGH;
long time = 0;
long debounce = 10;
int stayON = 5000; //stay on for 5000 ms

void setup() {
pinMode(pinButton1, INPUT);
pinMode(pinButton2, INPUT);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
}

void loop()
{

stateButton1 = digitalRead(pinButton1);
if(stateButton1 == HIGH && previous1 == LOW && millis() - time > debounce)

{
if(stateRelay1 == HIGH)
{digitalWrite(Relay1, LOW);}
else
{digitalWrite(Relay1, HIGH);
delay(stayON);
digitalWrite(Relay1, LOW);}

time = millis();
}

stateButton3 = digitalRead(pinButton3);
if(stateButton3 == LOW && previous2 == HIGH && millis() - time > debounce)
if (stateButton3 == HIGH)
{ digitalWrite(Relay1, LOW);
}

stateButton2 = digitalRead(pinButton2);
if(stateButton2 == HIGH && previous2 == LOW && millis() - time > debounce)

{
if(stateRelay2 == HIGH)
{digitalWrite(Relay2, LOW);}
else
{digitalWrite(Relay2, HIGH);
delay(stayON);
digitalWrite(Relay2, LOW);}

time = millis();
}

previous1 == stateButton1;
previous2 == stateButton2;
previous3 == stateButton3;
}

Please help!!!

You need to use blink without delay, and perhaps a flag, something like this

void loop(){
currentMillis = millis();
if (ledOn == 0){
// read button, save state:
buttonPressed = digitalRead (buttonPin); // buttonPin with internal pullup, connect Gnd when pressed
if (buttonPressed ==0){
startTime = currentMillis;
ledOn = 1;
// turn on LED pin
}
if (ledOn ==1 && (currentMillis - startTime) >= onTime) || digitalRead (offPin) == 0){ // same setup as above for this button
ledOn = 0; // time expired or 2nd button was pressed
// turn off LED pin
}
}

all time related variables are type unsigned long