How can I make a specific button run a specific loop?

I am trying to code a program that allows the first button to run one loop which will make the LEDS solid and lit up, the second to run the second loop which will make a sequential pattern, and the 3rd to run the 3rd loop which will flash twice. How can i go about doing that, ill send the loops in this chat in case anyone wants to help me out.

Loop 1
int led1 = A0;
int led2 = A1;
int led3 = A2;
int led4 = A3;
int led5 = A4;

void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}

void loop() {
digitalWrite(led1, HIGH);//it mean to give 5v(high) to pins.here ,the led will be on.
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
}

Loop 2
int led1 = A0;
int led2 = A1;
int led3 = A2;
int led4 = A3;
int led5 = A4;

void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}

void loop() {
digitalWrite(led1, HIGH);
delay(70);
digitalWrite(led2, HIGH);
delay(70);
digitalWrite(led3, HIGH);
delay(70);
digitalWrite(led4, HIGH);
delay(70);
digitalWrite(led5, HIGH);
delay(70);

digitalWrite(led1, LOW);
delay(70);
digitalWrite(led2, LOW);
delay(70);
digitalWrite(led3, LOW);
delay(70);
digitalWrite(led4, LOW);
delay(70);
digitalWrite(led5, LOW);
delay(70);

digitalWrite(led1, HIGH);
delay(70);
digitalWrite(led2, HIGH);
delay(70);
digitalWrite(led3, HIGH);
delay(70);
digitalWrite(led4, HIGH);
delay(70);
digitalWrite(led5, HIGH);
delay(70);
}

Loop 3
int led1 = A0;
int led2 = A1;
int led3 = A2;
int led4 = A3;
int led5 = A4;

void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}

void loop() {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
delay(60);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(60);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
delay(60);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(60);
delay(5000);
}

Well, you can only have one setup() and one loop() in your arduino program, so why not try it with just one program and call different functions for what you want?

Put each of the sequences into its own function.
When the state of a button switch changes, call the corresponding function.

Have you learned about if statements and else statements? They look kind of like this:

if (something == true) {
  perform_a_task();
}
else if (another_thing == true) {
  perform_some_other_task();
}

I think that you will absolutely need to learn about, and use, if statements to make your project work as you wish.

Hi @edrillz ,

welcome to the arduino-forum.

to make your project work much quicker
directly after registering you have been presented a tutorial that explains how to use this forum.

You can move a car by going to the tail and try to push it with your hands and feet,
Very very ineffictive.
Much more effective is to learn

  • there is an igintion key to start the motor
  • there is a clutch, throttle, brake and gears
    and if you use them in this ..... way
    you can be 100 miles away from here within one hour.

Similar thing in using this forum and in programming.
Invest some time to learn how things can be used in a much more effective way.

How to post code in this forum

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

It would be nice if you read the Forum guidelines and posted your code accordingly using code tags.

Hello edrillz

Welcome to the worldbest Arduino forum ever.

It seems to be a school assignment.

What is the learning objective to be achieved with this task?

Have a nice day and enjoy coding in C++.

First Time ever on fourms not sure how to use this yet.

I dont need to use a library for the button ?

All the more reason to read the guidelines.

We're not beating on you, we're trying to suggest that the best way to get quick help, with many helpers, is to help us help you.

No, not necessarily, but a library may be useful to help with switch debounce or sensing long or double presses. Did you read the page that i linked in post #3. That page discusses debounce, state change and other important topics.

This isn’t a school project it’s a personal project I’m making for a car headlight

Ahh okay I’m still a little confused but ima try it out

What is confusing? Maybe we can make it more clear.

So between each loops do i put if statements and else statements?

Ive been going at this for a while and i cant seem to put a function in bout to give up lol

Please post your best effort at writhing the code. Tell us what the code actually does and what the code is supposed to do.

I really do not want to write the code for you, you will not learn that way.

Well, have you looked any of the button examples, for ideas on how to read your buttons? We're at a loss to help you, if we can't see where you're at.

Ill send you what i have so far but i mean i dont really care if i learn since i dont do this much im just trying to make lights for my car lol
But what im trying to do is make 1 button run one pattern, another seprate buttton run another pattern, and the last button run a 3rd pattern.

int led1 = A0;
int led2 = A1;
int led3 = A2;
int led4 = A3;
int led5 = A4;
#define BUTTON_PIN A5
#define BUTTON_PIN A6
#define BUTTON_PIN A7



void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}

void loop() {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
}

////////next code

digitalWrite(led1, HIGH);
delay(70);
digitalWrite(led2, HIGH);
delay(70);
digitalWrite(led3, HIGH);
delay(70);
digitalWrite(led4, HIGH);
delay(70);
digitalWrite(led5, HIGH);
delay(70);

digitalWrite(led1, LOW);
delay(70);
digitalWrite(led2, LOW);
delay(70);
digitalWrite(led3, LOW);
delay(70);
digitalWrite(led4, LOW);
delay(70);
digitalWrite(led5, LOW);
delay(70);

digitalWrite(led1, HIGH);
delay(70);
digitalWrite(led2, HIGH);
delay(70);
digitalWrite(led3, HIGH);
delay(70);
digitalWrite(led4, HIGH);
delay(70);
digitalWrite(led5, HIGH);
delay(70);

//////////////////last code

digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
delay(60);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(60);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
delay(60);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(60);
delay(5000);

Well, if that's the case, then we don't need to check your programmes for errors.