Loop toggling question

Hello. Recently I got an Arduino, and I'm hoping I can get some help with some code I'm working on.
There are 4 buttons, with Button1 and Button 2 controlling their own respected loops, operating an LED and a relay. At the moment, all the buttons do is momentarily trigger their respected loops. I'm hoping I can get some help with making it so a button can trigger it's respected loop, have it repeat for one minute (regardless of if the button is depressed or not), and have it automatically end, unless a different loop overrides it in the middle of it's cycle. Button3 simply momentarily turns on the LED and Relay, and Button4 is supposed take priority and cancel any loop that might be playing. Is this able to be done with the delay() command, or is it more complicated? I looked up the loop() command, and I wasn't quite able to wrap my head around it. If someone could help me out, that would be awesome. Thanks.

int RELAY1=2;
int RELAY2=3;
int RELAY3=4;
int RELAY4=5;

int LEDPin1=6;
int LEDPin2=7;
int LEDPin3=8;
int LEDPin4=9;

int button1=10;
int buttonRead1;
int button2=11;
int buttonRead2;
int button3=12;
int buttonRead3;
int button4=13;
int buttonRead4;
int dt=10;
void setup() {

Serial.begin(9600);
pinMode(LEDPin1, OUTPUT);
pinMode(LEDPin2, OUTPUT);
pinMode(RELAY1, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
}

void loop() {
buttonRead1=digitalRead(button1);
buttonRead2=digitalRead(button2);
buttonRead3=digitalRead(button3);
buttonRead4=digitalRead(button4);
Serial.println(buttonRead1);
Serial.println(buttonRead2);
Serial.println(buttonRead3);
Serial.println(buttonRead4);
delay(dt);
if(buttonRead4==1){
   digitalWrite(LEDPin1,LOW);
   digitalWrite(RELAY1, HIGH);
}
if(buttonRead3==1){
   digitalWrite(LEDPin1,HIGH);
   digitalWrite(RELAY1, LOW);
}
if(buttonRead3==0){
   digitalWrite(LEDPin1,LOW);
   digitalWrite(RELAY1, HIGH);
}
if(buttonRead2==1){
   digitalWrite(LEDPin1,HIGH);
   digitalWrite(RELAY1, LOW);
   delay(200);
   digitalWrite(LEDPin1,LOW);
   digitalWrite(RELAY1, HIGH);
   delay(200);
}
if(buttonRead1==1){
   digitalWrite(LEDPin1,HIGH);
   digitalWrite(RELAY1, LOW);
   delay(500);
   digitalWrite(LEDPin1,LOW);
   digitalWrite(RELAY1, HIGH);
   delay(500);
}

}

Start by looking at the StateChangeDetection example in the IDE to see how to detect when a button becomes pressed rather than when it is pressed

When you detect that a button has become pressed save the value of millis() as the start time, start the action and set a boolean to true indicating that the action is occuring

Then in loop() if the boolean is true and the current values of millis() minus the start time is not greater than the required period then continue to take the required action. When you find that the period has elepsed stop the action and set the boolean to false to prevent the action occurring again until the button becomes pressed again

Also in loop() read the state of the "cancel" input and set the boolean to false to stop the action immediately. Note that other circumstances in the program, such as another function doing something can also be made to set the boolean to false to stop the action

Note that the above depends on no blocking code being present in the program

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

To help you shorten your code, look into arrays, which will let you handle the inputs and leds in a more manageable way... there are many examples from previous threads.

Also look into timing with millis(), which will help you perform timed operations that can be interrupted as the program logic changes.

Enjoy, and don’t be afraid to play & ask for incremental advice.

Hi Harvey,

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

harvardharvey:
with Button1 and Button 2 controlling their own respected loops, operating an LED and a relay.

There can be only one loop(). You can however have multiple things happening simultaneously. Look at this state machine tutorial. There are many others both on this site and the web. You can set up separate switch/case structures for switches 1 and 2 and switch 4 can still interrupt/terminate either of them because the code is non-blocking.

In the finished code.. you want 3 relays? or just one? Should button 1..3 act the same but just on their respective LEDs & Relays? Or should button 3 be different?

You have too many missing bits of the puzzle here..

-jim lee