Im trying to control the delay time of a flickering LED with a pushbutton, I figured out my problem originally was that I was not debouncing my button. To solve that I am using/modifying this code (https://www.arduino.cc/en/Tutorial/Debounce) because I am still very new to programing.
As of when the button is pressed it only adds time everyonce and a while, and im not sure, any help is greatly appreciated.
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int delaytime = 500;
// Variables will change:
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
int thetime = 500;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
thetime = (thetime + 1000);
}
}
}
digitalWrite( 13 , HIGH) ;
delay(thetime) ;
digitalWrite( 13 , LOW ) ;
delay(delaytime) ;
lastButtonState = reading;
}