How do I make the program pause until user input?

How do I make the program pause until user input?
In this program, I want the LED2 to blink after push button being pressed and released. LED2 should do nothing if push button is not pressed.

I don't want the blink part inside the if function. I'm making a game, and now I'm just experimenting the codes. Imagine turning LED1 on is what the pushbutton does, and the blink is the rest of the game, so the blink part won't be executed until push button is pressed.

Here's my codes:

const int buttonPin = 13;     // the number of the pushbutton pin
const int LED1 =  12;      // the number of the LED pin
const int LED2 = 11;

int buttonState = 0; 

void setup() {
  pinMode(LED1, OUTPUT);     
  pinMode(LED2, OUTPUT); 
  pinMode(buttonPin, INPUT);     
}

void loop(){
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {     
    digitalWrite(LED1, HIGH);  
  } 
  else {
    digitalWrite(ledPin, LOW); 
  }
  
  delay (1000);
  digitalWrite(LED2, HIGH);
  delay (200);
  digitalWrite(LED2, LOW);
  delay (200);
  digitalWrite(LED2, HIGH);
  delay (200);
  digitalWrite(LED2, LOW);
  delay (200);
  
}

First in with; you need to go read and understand blink without delay. XD

One thing you'll need is a variable to use to flag whether led2 should be blinking or not. Have the button press set the flag and then an if statement can start the led blinking.

I didn't try to compile your sketch but it looks like you have an undeclared variable named ledPin should probably be LED1.

Jimmy60:
First in with; you need to go read and understand blink without delay. XD

One thing you'll need is a variable to use to flag whether led2 should be blinking or not. Have the button press set the flag and then an if statement can start the led blinking.

I didn't try to compile your sketch but it looks like you have an undeclared variable named ledPin should probably be LED1.

I'm new to programming.. Is the "flag" a logic statement? I can't find that from Arduino library. Anyways, it sounds like equivalent to put blink part directly inside the if statement, right?

joexusun:
I'm new to programming.. Is the "flag" a logic statement? I can't find that from Arduino library. Anyways, it sounds like equivalent to put blink part directly inside the if statement, right?

A "Flag" is a variable you declare and set to certain values. Typically, either 1 or 0. You can then check if the value is 1 or 0 and do things depending on the value.

A flag is just a variable, usually a boolean type, that has two possible values, on/off, true/false, HIGH/LOW.

So you might declare:

boolean LED2shouldBeBlinking = false;

When the desired button press occurs you would change its value with:

LED2shouldBeBlinking = true;

Then you can use an if:

if (LED2shouldBeBlinking == true)
{
// blink the led
}

Does that help?

Jimmy60:
A flag is just a variable, usually a boolean type, that has two possible values, on/off, true/false, HIGH/LOW.

So you might declare:

boolean LED2shouldBeBlinking = false;

When the desired button press occurs you would change its value with:

LED2shouldBeBlinking = true;

Then you can use an if:

if (LED2shouldBeBlinking == true)
{
// blink the led
}

Does that help?

Ya. but isn't it equivalent to put the blink inside the if statement?

I guess I present the question in a wrong way. In C# console application, there's a command probably called console.read (don't remember the exact name). if you have a line of console.read, the program won't execute the codes after that line until it gets some text from the user. I'm looking for a similar mechanism here. I want the blink not to be executed until the user push the button.

In this program, I want the LED2 to blink after push button being pressed and released. LED2 should do nothing if push button is not pressed.

You need to clarify the above statement. Do you want to toggle on/off the LED when the button is pressed, or do you want the LED on when the button is pushed and off when the button is not pushed?

@zoomkat

I want the "blink" part not to be executed until the pushbutton is released. see my last reply. I'm looking for a mechanism similar to console.read from the c# console application.

The below is for servo button control, but you could modify to have the LED blink routine where the "servo1.write(160);" is located, and set the LED pin low where the "servo1.write(20);" is located.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

I want the blink not to be executed until the user push the button.

You have to realize that the Arduino is trapped in the loop() function so you have to think of things as happening over and over again. Each time through loop() you will just check if LED2shouldBeBlinking == true. If it is start the blinking, if it isn't don't start blinking. Also each time through loop() you'll check for button presses and when the correct one is seen you set LED2shouldBeBlinking = true which will start the blinking next time through loop(). So it won't blink until the right button press occurs yet it won't just sit waiting for a button press.

If you really wanted to wait then trap it in a while() loop until a button is pressed. Careful though trapping your code in a while loop can create issues like what if the condition to exit the loop is never met.

You have to get away from this wanting to sit and wait.

See if the below works close enough (not tested). The button press is simulated by touching pin 4 to ground.

//zoomkat LED button test 11-24-2012

int button1 = 4; //button pin, connect to ground to test
int press1 = 0;
int ledPin = 13;

void setup()
{
  pinMode(button1, INPUT);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    digitalWrite(13, HIGH);   // set the LED on
    delay(100);              // wait for a second
    digitalWrite(13, LOW);    // set the LED off
    delay(100);              // wait for a second
  }
  else {
    digitalWrite(13, LOW);    // set the LED off
  }
}