Change LED blink speed with Button

New to Arduino-

I wrote a program that uses the example blinkWithoutDelay to control an LED blink speed. I put in a pushbutton and want to control the speed of the blink based on pushing and holding down the pushbutton. How would I do that?

Set a to 12 and b to a. Please post your code if you want useful help :slight_smile:

Thanks! I have attached my code. I am a bit confused at your suggestion…

int pushButton = 2; // pushbutton is in pin 2
StreamOut serial;
int ledPin = 13;// the number of the LED pin
int ledState = LOW; // ledState used to set the LED
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status

unsigned long previousMillis = 0; // will store last time LED was updated

// constants won’t change:
const long interval = 1000; // interval at which to blink (milliseconds)

void begin() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);

}

void step() {
// check to see if it’s time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);

if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

if(buttonState == HIGH){ //This is where I want to have the pushbutton being pushed increase the speed of blinking
digitalWrite(ledState, LOW);
}
// if the LED is off turn it on and vice-versa:
else if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}

A single button will only allow you to increase or decrease the speed until it rolls over yo zero. Is that acceptable ?

How to do it ?
Detect when the button becomes pressed and save the millis() value and keep going round loop(). If the button is still pressed after a period then change the blink speed variable. I assume that you are not using delay() to time the blink and use millis() instead otherwise nothing will happen during any delay()s that you are using.

See Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE if you are not familiar with the principle.

First of all, you need to move your button handling code outside the LED handling code if you want the button to be of any real use. Your button handling code should be right under the line where you "digitalRead" the "buttonState". You change the LED's blink-rate by lowering "interval" (faster) or by increasing it (slower).

A smarter way to invert the state of an LED

 digitalWrite(ledPin, !digitalRead(ledPin));

No need for a state variable and if/else to set the state

@UKHeliBob: yes that is exactly what I want to do. How would I do what youre saying? I am just confused on how to have the button time recorded and how to increase the interval and set the button to go faster

And if ledState is used by other parts of the sketch, you can simply invert it from HIGH (1) to LOW (0) vice versa with "ledState ^= 1" which is probably faster than reading the pin state :wink:

start of loop()
  if button becomes pressed
    save value of millis()
    set boolean variable to true to indicate timing
  else
   if button becomes released
    set boolean variable to true to indicate not timing
   end else

  if timing is true
    if update period has passed
      decrement LED period variable
       save value of millis()
    end if

//other code here to blink LED using millis() and LED period variable
end of loop()