Weird delay to button push

I received an Ardunio Uno R3 today and started going through the examples. I got as far as the Digital button push when I decided to combine two of the examples with a twist. I wanted to take the blink code, and have it blink faster when somoene pushes a button. Not hard, not too complex.

I got that part working in half an hour. But I noticed that when I pushed the button, the led would go through the 1 second on then one second off before blinking more rapidly. I rewrote the code to do a for loop and sliced the delay into 50ms pieces to allow code to break out of what it was doing faster. It is working mostly. There is still the occasional random seeming delay before it starts blinking faster.

I am posting the code and hoping somebody will take the time to review the code and let me know where my issue lies. Thanks in advance.

/*

 Slowly blinking light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2 LED blinks
 faster. The delay is checked every 20th of a second to improve 
 response to button pushes.
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
const int Fast = 1;
const int Slow = 0;

// global variables that will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   
}

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

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // Blink LED fast:   
      BlinkSpeed(Fast); 
  } // if
  else {
    // Blink LED slowly:
      BlinkSpeed(Slow);
  } // end else
}

void BlinkSpeed(int pace){
int fastcycle = 4;           // 200 ms blink rate
int slowcycle = 20;          // 1000 ms blink rate
int count = 0;
  
  switch (pace){
  case 1:                  // Blink Fast
    for (count = 0; count < fastcycle; count ++) {
        if (buttonState == HIGH) {     // Check button state
          digitalWrite(ledPin, HIGH);   // turn the LED on (Voltage HIGH)
          delay(50);                   // wait for 1/20th of a second
        }else{
          break;              // Get out of loop if button released
        } // end else
    } // end for
    for (count = 0; count < fastcycle; count ++) {
        if (buttonState == HIGH) {     // Check button state
          digitalWrite(ledPin, LOW);    // turn the LED on (Voltage LOW)
          delay(50);                   // wait for 1/20th of a second
        }else{
          break;              // Get out of loop if button released
        } // end else
    } // end for
    break;
  default:                  // Blink Slow
    for (count = 0; count < slowcycle; count ++) {
        if (buttonState == LOW) {     // Check button state
          digitalWrite(ledPin, HIGH);   // turn the LED on (Voltage HIGH)
          delay(50);                   // wait for 1/20th of a second
        }else{
          break;              // Get out of loop if button pressed
        } // end else
    } // end for
    for (count = 0; count < slowcycle; count ++) {
        if (buttonState == LOW) {     // Check button state
          digitalWrite(ledPin, LOW);    // turn the LED off (Voltage LOW)
          delay(50);                   // wait for 1/20th of a second
        }else{
          break;              // Get out of loop if button pressed
        } // end else
    } // end for
  } //  end switch
}

You're problem is delay().

Take a look at the Blink Without Delay example. To change speeds, simply change the interval value.

Thanks Arrch for the heads up. When I read the delay() description, I saw the caveat. I'll have to readjust my thinking before I make changes to my code.

When I read the XXXXX() description, I saw the caveat. I'll have to readjust my thinking before I make changes to my code.

... OH--- PLEASE Don't do that...
If 90% of the people who post here... did just that, " have to readjust my thinking "... then most of the questions would disappear...

Docedison:
... OH--- PLEASE Don't do that...
If 90% of the people who post here... did just that, " have to readjust my thinking "... then most of the questions would disappear...

And no one would be the target of sarcasm. :roll_eyes: You're right, we should request everyone post more questions. :wink: