Hello! I need to control the speed of a blinking LED with a pushbutton. When I press and hold down the button, the speed of the blinks should increase from 1 blink a second to 10 blinks a second, within the span of 10 seconds. I am using the Millis() function to blink but cannot figure out how to implement a button, or how to have the speed adjusted when I push down the button. I have attached my code:
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
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 setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// 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();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
THANKS
I have attached my code:
Where is the code where you read the pushbutton?
I know how to read the pushbutton with
buttonState = digitalRead(buttonPin);
if(buttonState == LOW){ //This is where I want to have the pushbutton being pushed increase the speed of blinking
digitalWrite(ledState, LOW);
}
but I do not know how to implement the button to be read into the blinkWithoutDelay
Why don't you show what you have tried?
This is what I currently have:
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() {
Serial.begin(9600);
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
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(buttonState == LOW){ //This is where I want to have the pushbutton being pushed increase the speed of blinking
digitalWrite(ledState, LOW);
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
This is what I currently have:
And . . . .?
Observations?
Does it do what you expect?
What do you expect?
How does it fall short?
Nope, it sucks. It blinks and that's about it. I know that I need to put the button in somehow so it can read the serial input and translate that into time held down (seconds() or mills()) and then from there, gradually increase the speed of blinking
I know that I need to put the button in somehow so it can read the serial input
Serial input?
Woah! Where did that expectation come form?
At least I think serial input... I need to somehow track how long the button is pressed