Wait for button press then start loop

How do you make an arduino wait for a button press before it starts the loop forever ?

Thanks Reece

Wire the button to take the pin to GND when pressed, then

void setup()
  {
    const byte buttonPin = 2;
    pinMode(buttonPin, INPUT_PULLUP);
    while (digitalRead(buttonPin) == HIGH)
  {}
  }

void loop()
  {
    //repeating code goes here
  }

Thankyou amazing help !! :slight_smile: :slight_smile:

Is they also a way to make it if you press a button it turns the arduino off ??

Thanks Reece

maskedcoding:
Thankyou amazing help !! :slight_smile: :slight_smile:

Is they also a way to make it if you press a button it turns the arduino off ??

Thanks Reece

Sure, get a latching button and put it in the power line feeding power to the Arduino.

Arduino can't turn itself off from code. But it is easy to just cut the power.

Someone already done that.
http://www.ebay.com/itm/28CM-USB-Cable-with-Switch-Control-for-Raspberry-Pi-Arduino-USB-On-Off-Toggle-CA-/121967778305

maskedcoding:
Thankyou amazing help !! :slight_smile: :slight_smile:

Is they also a way to make it if you press a button it turns the arduino off ??

Thanks Reece

How "turned off" do you want it? Youll need a physical solution to turn off power to the processor, if you just want it to stop the light being on, thats in code.

The physical solution would be to build a logic gate with transitors that control power to the arduino. You could program a long button press to output a pin that kills the gate and turns off the power and would reset when power was applied by the on button to the logic gate again.