Programming a push button to restart loop

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

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.

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"

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. Take a look. It might help you figure out a way to build what you are trying to do.

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

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

Hello 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++.

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

Hello

Start with coding now.

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.

  • 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

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

is this something like what you're looking for?

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