Delayed pressed button

Hi everyone,
I’m very new to this and have a very simple question,
How do I create a program so the arduino waits till a button is pressed, then goes onto the next program,
Sorry it’s such a basic question but I am still new to all this,
Thanks very much,

First there's no such thing as a "next" program in Arduino, there's only "the" program.

But the way forward for you is probably edit perhaps to use to separate the parts of "the" program so that each case contains the "next" part. Each case would be identified by a variable called say systemState having different values 0, 1, 2 etc.

Then the last line in each case block would be to read the button and if it's pressed, increment the systemState so that next time through loop() the next case runs.

Something like that anyway; details will depend on getting more detail from you.

Actually, this would be a neater way. Check the button in loop() ala state change detect and increment the counter edit systemState that's nicer than having the button code in each case.

Assuming you know how to read a button, you can use it to set a flag if you detect that the button is/becomes pressed.

Next you can react on that flag (if statement) and execute the other part of the code. Once that part is finished, that part clears the flag.

OP is this diagram the kind of thing you have in mind? (Just a random one from a Google search but strikes me as what you're wanting to do?)


states with button.PNG

states with button.PNG

Wow you lot are quick to reply, I actually want a button to sound a horn wait for 5 seconds release 2 solenoids then drive a couple of motor, all this is because I’m building the back to the future robot arm with ramp and can opener, I’m out at the moment so I’ll take a look when I get in,
Thank you all very much for your replies

So to clarify... seems the thing is idle waiting for the button, then the button only gets pressed once to start the whole cycle (horn, solenoids, motors) then returns to idle? (that is, only one button press per cycle, not lots of presses to move between the phases of the cycle?)

And supplementary question: is there anything else going on that you didn't say, that would require the code to be looking at (say) a different button all the time while it's doing all of that?

Yep that’s it, idle till the button is pressed, go through the cycle then idle again, the sequence will be:-

Press button
Sound horn straight away
Delay maybe 2-3 seconds
Pulse solenoids for 0.3 seconds (release can and stop next can)
Drive motor 1 (close claw) (brushed motor)
Drive motor 2 (move arm to can opener) (stepper motor)
Drive motor 3 part way through motors 2’s full movement (turn wrist slightly to tilt can) (stepper motor)
Drive motor 4 (push can openers arm down to open can) (stepper motor)
Delay maybe 4-5 seconds (while can is being opened)
Drive motor 2 half way back to original starting point and stop
Drive motor 3 180 degrees (turn wrist to empty can/dog food)
Drive motor 3 180 degrees back (turn wrist to original point)
Drive motor 2 the rest of the way to original starting point
Drive motor 1 part way through motor 2’s movement (release claw and drop can in bin)

I’m sure there are many people out there that can write code as easy as I just wrote this post but for me as beginner it seems like a different language, I will look at other programs I need and hopefully splicing them together :slight_smile:
Thanks again for any help,

For the idle part, waiting for the button press…
Use a while loop if you don’t need anything else to happen whilst waiting.
I would probably have the button pulled up with a 10k resistor, and pulled to ground on button press…

while (digitalRead(button) == HIGH) {
  // wait
<other code in here>

Once the button is pressed, it will no longer be high, and the while loop will exit, and the code below will execute…
Consider debouncing the button, I tend to use hardware and software measures… (google these)…

You could have all of this in the setup section too, it sounds like you don’t want the code looping around anyway…

You could have all of this in the setup section too, it sounds like you don't want the code looping around anyway....

I was just typing very similar reply apart from that part. Looked to me that OP wants the code to revert to waiting for the button for the again and again (it has "idle" at the bottom of the list). Seems they want to do more tins without a re-start.

But @OP be careful of such an approach because it blocks anything else from happening while it's waiting. It seems like that won't be problem in your case, but there may be other requirements you haven't mentioned, like reading other buttons to do other stuff.

On a practical note, I have never once in decades of feeding pets, ever had a tin of dog food that emptied just with gravity. The tin always needs a good shake or bash onto the side of the dog bowl, or a good dig around with a spoon.

How are you going to determine when a motor movement is complete?

Thanks lee737 that’s a great start, I’ll get on it after some food and let you know how I get on,
The code needs to start at idle and finish at idle ready for that same button to be pressed again, there is no other actions other than the list,
Determining the motor movement will be steps on the stepper motor and time on the brush motor, Ive worked out the steps it needs to go from start to the tin opener and I’ll edit this in a mo after some food,
As for the dog food, if you cast your mind back to the opening scene of back to the future (with the can of dog food) it just plops out, I plan to use bits of paper for my scene, not real food,

if you cast your mind back to the opening scene of back to the future

I would remember it, had I seen it.

Void-basil :-0

Void-basil :-0


I can’t believe you haven’t watched back to the future :smiley:

Ok so I’ve tried modifying a button program that puts an LED on when pressed, with the while loop but I can’t get it to work, I’ve also tried putting delays in but it bypasses the button and just flashes the LED, I did say I was a beginner,
I may be going about the start of the program the wrong way, is it that once a program is uploaded to the arduino, and the first command a pressed button, it will wait anyway won’t it?

Show your code :wink: And please post it between
** **[code]** **
** **[/code]** **
so it looks like

your code here

I can’t believe you haven’t watched back to the future :smiley:

Isn't it amazing that on a planet with upwards of 7B inhabitants, not everyone likes science fiction?

Have a look at this, which is an implementation of the suggestion from Lee737 in #8:

// 4 April 2021

// ***** wire the button from pin to ground *****
const byte button = 2;

void setup()
  pinMode(button, INPUT_PULLUP);
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println("setup() done");
} //setup

void loop()
  Serial.print("Press the button...");
  while (digitalRead(button) == HIGH)
    // wait for button to be pressed

  // if you get here the button was pressed
  Serial.println("  pressed");
  digitalWrite(LED_BUILTIN, HIGH);
  delay(5000); //not a big fan of delay(), illustrative purposes only ;)
  digitalWrite(LED_BUILTIN, LOW);

} //loop

The code needs to start at idle and finish at idle ready for that same button to be pressed again, there is no other actions other than the list,

Sounds like a fun project - in this case, don’t worry what I said about setup, you’ll need to use loop for the body of your code… good luck!