Push button control

Hi,

I want my program to wait until a push button is pressed then start my program. Once I push the button again I want the arduino to reset. Is there a simple way to do this?

Thanks

I want my program to wait until a push button is pressed then start my program. Once I push the button again I want the arduino to reset. Is there a simple way to do this?

There is a simple way to make the Arduino wait to run loop() until you have pressed the switch. In setup(), add something like this:

while(digitalRead(somePin) == HIGH) { };

You need to press the other switch to reset the Arduino.

Once I push the button again I want the arduino to reset

I don't think you do. Resetting a processor from code requires external hardware and is not necessary at all. Please rethink what you want to do.

Is there a simple way to do this?

Yes. look at the switch each time round the loop and if it is not pressed skip your code. When it is pressed your code will be run. At the end of this code hold until the button is released. Then call the setup() function exit your code to go round the loop again.

I want my program to start the main loop from the push button. Then loop continuously until the button is pressed again and restart the entire program from the beginning. Thanks for the quick feedback.

The only reason to do the hard reset is if you need it to re-initialize variables and run setup again.

A simple way to handle what you are trying to do is to use a big IF ELSE Loop.

At the beginning of your loop, include an IF statement that includes a variable that is set and reset by your button. If it is set to value 'x' it will do whatever you think needs to be done on startup. Once you do that, set it to a different value and this becomes your else statement so the rest of the time your loop will ignore that 'startup' section and only run the section of code you want.

When you press the button again, it is set and it runs the first part of your IF again....rinse and repeat...

Im still having issues with the code. I have a data logger that sets up a csv file in the setup function. I want it to wait to start taking data until I press the push button. Once I press the button again I want it to create a new csv file and then wait again to start taking data. I figured the easiest way to do this is to call a reset function that will start from the very beginning of the code. Help Please

I figured the easiest way to do this is to call a reset function that will start from the very beginning of the code

No that is over simplistic. Use flags, variables that tell the code what to do. Use the loop function to look at your push button. Don't do your code until you see the flag tells you you can. The push button sets the flag. When your code is done it resets the flag so it won't do it again, until you push the button again.

isn't it possible to cal the setup loop from within the main loop by just calling setup() ? that would make it easy

Steen: isn't it possible to cal the setup loop from within the main loop by just calling setup() ? that would make it easy

What an interesting question. I wish I could check but I am at work.

To the OP, Ask yourself what you gain by resetting the Arduino?

The answer is going to be absolutely nothing.

boolean state = false;

void setup()
{ 
  //Start serial or wire or whatever else you need
  //Do all your other typical setup stuff
}

void loop()
{
  while (state == false)        //Nothing else will ever happen in the loop until state == true. This allows you to wait for the button press
  {
    if (digitalRead(buttonpin) == HIGH)
    {
       //Do all your I pressed a button stuff
       state = true;               //The last thing you do after you do your press button stuff. Now the next time through the loop it will ignore this section and move on to the next which can
                                        //be another while or if or whatever you want to happen while the state is true and you should be recording data. Just don't forget to check for your button 
                                        //in this section. Once you see the button again, do your stop stuff and set your state = false;
     }
  }
}

isn't it possible to cal the setup loop from within the main loop by just calling setup()

Yes of course it is, setup() is just a function like any other.

Hi all!
I’m Zoltan from Hungary and absolute new here.
I’d like to get some help with my Arduino mega.
I’d like to start my program when a button pressed, and I red Your solutions above, but I use an lcd/key shield, so my buttons (select, left, etc…) are on an analog input. Can anybody show Me how to make the code waiting until one of these buttons pressed?
Thanks a lot!

Insert this at first lines after void loop () and your sketch will pause until keypress.

while (analogRead(0)>1000) { }   //nothing to do, just waiting for keypress on DF Robot LCD Keypad or compatible.

Gabriel_swe: Insert this at first lines after void loop () and your sketch will pause until keypress.

while (analogRead(0)>1000) { }   //nothing to do, just waiting for keypress on DF Robot LCD Keypad or compatible.

Really thank You, it works fine!

Gabriel_swe: Insert this at first lines after void loop () and your sketch will pause until keypress.

while (analogRead(0)>1000) { }   //nothing to do, just waiting for keypress on DF Robot LCD Keypad or compatible.

Would I put the action I want to preform a line under the wait code? I want to be able to press the select button on my LCD keypad shield to run the code after I push the button

You are replying to a thread from 2 years ago that resurrected a now 5 year old thread.

while (analogRead(0)>1000) { }   //nothing to do, just waiting for keypress on DF Robot LCD Keypad or compatible.

tarbear123: Would I put the action I want to preform a line under the wait code? I want to be able to press the select button on my LCD keypad shield to run the code after I push the button

You shouldn't use that kind of wait code at all. It makes everything wait, which becomes apparent once you try to do more than one thing at a time. Arduino Uno is capable of managing into the 100's of things at once, people show it on youtube mostly in led displays.

1st lesson explained well by Nick Gammon, with code is here: http://gammon.com.au/blink

The Arduino void loop() is the driving wheel for your sketch. You make your code into sections that run over and over as loop() runs.

If there is a condition to only run on time or if a pin or variable changes then that is the trigger event. One section of code can change a variable that triggers another section, gives it something to do and there you have tasking code that you don't have to write code structures to link.

If your void loop() has sections that only run between button presses then these small sections can handle the job.

1) make a button input section that reads and debounces the button continuously while updating a button status variable. 2) make a section that reads button status and updates a variable named runProcess depending on 1st and 2nd presses. 3) make a section that runs when runProcess is > 0 and stops when it is = 0.

If you want to add other processes then go right ahead. There is no big structure to fit them into as long as you don't put one in, being able to pass values makes that unnecessary.

Get into Nick's tutorial and see about blocking code, why and how to avoid it.

pattybart1023: Hi,

I want my program to wait until a push button is pressed then start my program. Once I push the button again I want the arduino to reset. Is there a simple way to do this?

Thanks

boolean i_am_running = false;

void loop() {
  if the button was not pushed last time I looped, and now it is, then
    if i_am_running, then
      do whatever has to be done to stop running
      i_am_running = false;
    else
      do whatever has to be done to start running
      i_am_running = true;
    end
  end

  if i_am_running, then
    do whatever I have to do as part of normal running operation
  else
    do nothing
  end  
}