This is my code. What I am trying to do is when I press the button, all of the LED will be "pause" immediately.

I have 3 LED. All of LED will blink consecutive. When I press the button (BA), only red is HIGH, red and yellow is LOW. RED - HIGH GREEN & YELLOW - LOW

Then when I press another button, BB (I dont know how to do it, that's why I didnt put on the code), LED will continue blink.

I am still a newbie on Arduino. Can someone help me edit this simple code?

[This in my code][1]

const int BA = 2;     // the number of the pushbutton pin
const int BB = 3;
const int ledPin =  13;      // the number of the LED pin

int BA = 0;         // variable for reading the pushbutton status
int red = 13;
int yellow = 11;
int green = 9;

void setup() {
    pinMode(BB, INPUT)
    pinMode(BA, INPUT);
    pinMode(red, OUTPUT);
    pinMode(yellow, OUTPUT);
    pinMode(green, OUTPUT);

void loop() {
    // read the state of the pushbutton value:
    BAstate = digitalRead(BA);

    if (BAstate == HIGH) {
        digitalWrite(red, LOW);
        digitalWrite(yellow, LOW);
        digitalWrite(green, LOW);
    } else {
        digitalWrite(red, HIGH); // turn the red light on
        delay(2000); // wait 5 seconds

        digitalWrite(green, HIGH); // turn green on
        digitalWrite(red, LOW); // turn red off
        digitalWrite(yellow, LOW); // turn yellow off
        delay(2000); // wait ledDelay milliseconds

        digitalWrite(red, LOW);
        digitalWrite(green, LOW);
        digitalWrite(yellow, HIGH); // turn on yellow
        delay(2000); // wait 2 seconds
        digitalWrite(yellow, LOW);
  • How is it wired? Please provide a sketch or simple schematic. – Nick Gammon Jul 31 '16 at 21:06
  • @NickGammon I don't see how that's relevant, other than maybe an absent pull-down resistor in the button input pin. – Gerben Aug 1 '16 at 15:33
  • The problem with the above code is that it only check the button when it's done doing the entire leds/traficlight sequence. So only once every 6 seconds. Check out the Blink without delay article. – Gerben Aug 1 '16 at 15:36
  • Would ask this as a comment, but I don't have 50 reputation. Why did you write "const int BA = 2" but then "BA = 0" immediately after? – Tri Aug 1 '16 at 19:28
  • 1
    @Gerben - something happens when he presses a button, so knowing how it is wired will help. He initially said (when I responded) that pressing the button makes the LEDs go LOW, which is what you would expect with certain wiring. – Nick Gammon Aug 2 '16 at 1:55

Put the button you will press to pause the blinking on one of the interrupt pins on your board and preferably set to trigger on the rising edge(say this is pauseButton). Put the other button on any other pin (continueButton).

In the setup attach the interrupt using attachInterrupt(digitalPinToInterrupt(pauseButton), pauseFunction, RISING); The pauseFunction is the Interrupt Service Routine, or Interrupt Handler. Rising means trigger on the rising edge.

In your loop place the code to blink the lights continuously. You don't have to poll the interrupt pin, it will automatically take you to the handler.

Add a new function pauseFunction, the interrupt handler, and set your lights to the state your desired state in the function. Then use something like a while loop to continuously poll the continueButton pin, and exit once the pin is pressed. It's something like this for a pull down button:


The loop will exit on the press of the button(when it gets low), and exit the interrupt handler as well. This will take your code back to where it left off. If you are particular about the previous states of the lights, read the pins, save them in temporary variables and restore them before you exit the handler.

If you need more on interrupts, look at this article on how to use interrupts on an arduino.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.