# Programming a push button to restart loop

**URL:** <https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525>\
**Category:** Programming\
**Created:** [April 8, 2023, 8:12am UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525 "2023-04-08T08:12:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![champ2552](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/champ2552/32/943257_2.png) [@champ2552](https://forum.arduino.cc/u/champ2552)\
**Post date:** [April 8, 2023, 8:12am UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/1 "2023-04-08T08:12:02Z")

</div>

I juts love arduino but not knowing what i have to code to make something work....i want to work in a push button that will reset the loop....program will run until a delay....delay will run until button is pressed and then the loop must start again....how do i program this...the wording is my problem

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [April 8, 2023, 8:17am UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/2 "2023-04-08T08:17:27Z")

</div>

so you want "something" to start on button press,  
this "something" should run for a specific time?  
this "something" should stop after that specific time?

a compileable code would make it easier to understand what you want.

```cpp
const uint8_t buttonPin = A0;
const uint16_t interval = 1000; // interval at which to blink (milliseconds)

void doSomething() {
  Serial.println(F("do something"));
}

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  uint32_t currentMillis = millis();
  static uint32_t previousMillis = 0; // must be static as it is a local variable and we need the value in next iteration
  static bool isRunning = false;
  
  if (digitalRead(buttonPin) == LOW) { 
    // (re-)start
    isRunning = true; // activate on button press
    previousMillis = currentMillis; // remember time
  }
  
  if (isRunning == true && currentMillis - previousMillis >= interval) {
    // stop
    isRunning = false;
  }

  if (isRunning == true) {
    doSomething();
  }
}

```

if not done yet, read and test the example "Blink Without Delay"

> **[Blink Without Delay | Arduino Documentation](https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay)**
>
> Blink an LED without using the delay() function.

---

<div class="post-metadata">

**Author:** ![ryancasler](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryancasler/32/1109959_2.png) [@ryancasler](https://forum.arduino.cc/u/ryancasler)\
**Post date:** [April 8, 2023, 8:37am UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/3 "2023-04-08T08:37:05Z")

</div>

> [@champ2552](#):
>
> loop must start again

That's not how the loop function in Arduino works. The loops starts after it finishes. It will start over again at the beginning as soon as the last line of code is executed.. On and on and on forever. You can't just stop it and start it over again with a button press. The only real way to do that would be to reset the power on the board. But I assume you want the board to do something once you press it.

There are a bunch of libraries out there to help you do basic "button things". One I really like is [Button2](https://github.com/LennartHennigs/Button2). Take a look. It might help you figure out a way to build what you are trying to do.

---

<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:** [April 8, 2023, 9:31am UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/4 "2023-04-08T09:31:34Z")

</div>

Hello  
Take some time and describe the desired function of the sketch in simple words and this very simple.

---

<div class="post-metadata">

**Author:** ![champ2552](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/champ2552/32/943257_2.png) [@champ2552](https://forum.arduino.cc/u/champ2552)\
**Post date:** [April 8, 2023, 9:43am UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/5 "2023-04-08T09:43:40Z")

</div>

Thank you,yes its a program that runs for an ice machine....at the end it must wait for the ice to drop(where the flap will press the button)and then start the program from the top...reset can work yes i did think of that but knowing how to program this would just be an advantage

---

<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:** [April 8, 2023, 9:49am UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/6 "2023-04-08T09:49:54Z")

</div>

> [@champ2552](#):
>
> .reset can work yes i did think of that but knowing how to program this would just be an advantage

Hello [champ2552](https://forum.arduino.cc/u/champ2552)

Take a piece of paper, a pencil and design, based on the IPO model, a program structure plan before start coding.  
Identify functions and their depencies. Now start with coding and testing, function by function.  
At the end merge all tested function together to get the final sketch.  
The basics for coding the project are to be found in the IDE as examples.

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

---

<div class="post-metadata">

**Author:** ![champ2552](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/champ2552/32/943257_2.png) [@champ2552](https://forum.arduino.cc/u/champ2552)\
**Post date:** [April 9, 2023, 10:13am UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/7 "2023-04-09T10:13:19Z")

</div>

Good day

The program starts the kompressor and water to start making the ice....after a desired time the water stops and a defrost cycle starts which is just a valve that opens and compressor stays on....that will drop the ice out....when the ice drops there is a cover that will move when the ice falls into a bucket....this cover will have a switch....so simply the defrost will start and continue to defrost until the switch is pressed....after the switch is pressed the valve for defrost will close and water will come in to start the new cycle....the whole program is fine and working just not the switch for when the defrost must stop

---

<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:** [April 9, 2023, 11:25am UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/8 "2023-04-09T11:25:22Z")

</div>

Hello

Start with coding now.

---

<div class="post-metadata">

**Author:** ![ryancasler](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryancasler/32/1109959_2.png) [@ryancasler](https://forum.arduino.cc/u/ryancasler)\
**Post date:** [April 11, 2023, 8:16pm UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/9 "2023-04-11T20:16:59Z")

</div>

Is is not the loop you are going to want to start over. Making ice is going to take a LONG time. The loop on your board is something that is designed to run every few milliseconds. I would look at something called a state machine. I think that is going to be what you are going to want to use. It will allow you to react to changes more readily than a simple single path loop.

---

<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:** [April 11, 2023, 10:16pm UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/10 "2023-04-11T22:16:54Z")

</div>

- The program starts the kompressor and water to start making the ice....

- after a desired time the water stops

- and a defrost cycle starts which is just a valve that opens and compressor stays on

- that will drop the ice out

- when the ice drops there is a cover that will move when the ice falls into a bucket

- this cover will have a switch

- so simply the defrost will start and continue to defrost until the switch is pressed

- after the switch is pressed the valve for defrost will close and water will come in to start the new cycle

- the whole program is fine and working just not the switch for when the defrost must stop

In your intial post you are writing about stop and start void loop()  
In this description abouve you are writing

> [@champ2552](#):
>
> the whole program is fine and working just not the switch for when the defrost must stop

**The whole program is working fine.**  
Does this mean you have an existing code that runs on an Arduino Uno and just needs some modifikation?

You should post your existing code as a code-section

From this base suggestions can be made

best regards Stefan

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [April 11, 2023, 11:03pm UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/11 "2023-04-11T23:03:11Z")

</div>

is this something like what you're looking for?

```cpp
// simple button processing

const byte butPin = A1;
byte ledPin = 13;

byte butState;

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void loop (void)
{
    byte but = digitalRead (butPin);
    if (butState != but) { // check that button state changed
        butState = but;
        delay (10); // debounce

        if (LOW == but) { // button pressed
            digitalWrite (ledPin, On);
            delay (1000);
            digitalWrite (ledPin, Off);
        }
    }
}

// -----------------------------------------------------------------------------
void setup (void)
{
    digitalWrite (ledPin, Off);
    pinMode (ledPin, OUTPUT);

    pinMode (butPin, INPUT_PULLUP);
    butState = digitalRead (butPin);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 8, 2023, 11:03pm UTC](https://forum.arduino.cc/t/programming-a-push-button-to-restart-loop/1112525/12 "2023-10-08T23:03:30Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
