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

**URL:** <https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540>\
**Category:** LEDs and Multiplexing\
**Created:** [September 27, 2023, 2:08am UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540 "2023-09-27T02:08:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![edrillz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/edrillz/32/1003438_2.png) [@edrillz](https://forum.arduino.cc/u/edrillz)\
**Post date:** [September 27, 2023, 2:08am UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/1 "2023-09-27T02:08:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [September 27, 2023, 2:46am UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/2 "2023-09-27T02:46:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [September 27, 2023, 2:51am UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/3 "2023-09-27T02:51:03Z")

</div>

Put each of the sequences into its own [function](https://www.tutorialspoint.com/arduino/arduino_functions.htm).  
When the state of a [button switch](https://roboticsbackend.com/arduino-push-button-tutorial/) changes, call the corresponding function.

---

<div class="post-metadata">

**Author:** ![odometer](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/odometer/32/1013044_2.png) [@odometer](https://forum.arduino.cc/u/odometer)\
**Post date:** [September 27, 2023, 3:42am UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/4 "2023-09-27T03:42:27Z")

</div>

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

```arduino
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.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [September 27, 2023, 4:24am UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/5 "2023-09-27T04:24:09Z")

</div>

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

> [@Make experienced users help: two ways to quickly post code as a code-section](https://forum.arduino.cc/t/make-experienced-users-help-two-ways-to-quickly-post-code-as-a-code-section/937298/1):
>
> Hi newcomer, This thread wants to show you how you can speed-up getting helpful answers with small effort. If you manage at least to post this way. You have the sympathy of the experienced users. If you are willing to invest another 20 minutes these 20 minutes extra-time will save you a minimum of 200 minutes to finish your project. always post your full sketch. As a code-section from the very first to the very last line A code-section has a small height =\> keeps your posting "short" but e…

Take a look into this tutorial:

[Arduino Programming Course](https://startingelectronics.org/software/arduino/learn-to-program-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

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [September 27, 2023, 4:24am UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/6 "2023-09-27T04:24:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [September 27, 2023, 4:36am UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/7 "2023-09-27T04:36:05Z")

</div>

Hello [edrillz](https://forum.arduino.cc/u/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++.

---

<div class="post-metadata">

**Author:** ![edrillz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/edrillz/32/1003438_2.png) [@edrillz](https://forum.arduino.cc/u/edrillz)\
**Post date:** [September 27, 2023, 5:03am UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/8 "2023-09-27T05:03:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edrillz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/edrillz/32/1003438_2.png) [@edrillz](https://forum.arduino.cc/u/edrillz)\
**Post date:** [September 27, 2023, 5:03am UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/9 "2023-09-27T05:03:47Z")

</div>

I dont need to use a library for the button ?

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [September 27, 2023, 11:24am UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/10 "2023-09-27T11:24:13Z")

</div>

> [@edrillz](#):
>
> First Time ever on fourms not sure how to use this yet.

All the more reason to read the guidelines.

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/681313/1):
>
> Welcome to the Arduino forum! A guide explaining how to get the best out of this forum is available at the link below. Please read it:

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.

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [September 27, 2023, 12:49pm UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/11 "2023-09-27T12:49:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![edrillz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/edrillz/32/1003438_2.png) [@edrillz](https://forum.arduino.cc/u/edrillz)\
**Post date:** [September 27, 2023, 2:20pm UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/12 "2023-09-27T14:20:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edrillz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/edrillz/32/1003438_2.png) [@edrillz](https://forum.arduino.cc/u/edrillz)\
**Post date:** [September 27, 2023, 2:21pm UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/13 "2023-09-27T14:21:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [September 27, 2023, 2:40pm UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/14 "2023-09-27T14:40:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edrillz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/edrillz/32/1003438_2.png) [@edrillz](https://forum.arduino.cc/u/edrillz)\
**Post date:** [September 27, 2023, 3:17pm UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/15 "2023-09-27T15:17:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edrillz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/edrillz/32/1003438_2.png) [@edrillz](https://forum.arduino.cc/u/edrillz)\
**Post date:** [September 27, 2023, 3:43pm UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/16 "2023-09-27T15:43:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [September 27, 2023, 3:52pm UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/17 "2023-09-27T15:52:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [September 27, 2023, 3:54pm UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/18 "2023-09-27T15:54:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![edrillz](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/edrillz/32/1003438_2.png) [@edrillz](https://forum.arduino.cc/u/edrillz)\
**Post date:** [September 27, 2023, 5:18pm UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/19 "2023-09-27T17:18:24Z")

</div>

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.

```arduino
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);

```

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [September 27, 2023, 5:29pm UTC](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540/20 "2023-09-27T17:29:40Z")

</div>

> [@edrillz](#):
>
> i dont really care if i learn

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

[Next page](https://forum.arduino.cc/t/how-can-i-make-a-specific-button-run-a-specific-loop/1172540.md?page=2)
