Program with two push buttons a “gas” and “brake” button.

I want to make program with two push buttons a“gas” and “brake”button. The“gas”button should speed up the blinking rate of the LED, and the “brake” button should slow it down. Why my program doesn`t work. :confused: :confused:

This is my code:

const int kPinButtonGas = 5;
const int kPinButtonBreak = 3;
const int kPinLed=9;

void setup() {
pinMode(kPinButtonGas, INPUT);
pinMode(kPinButtonBreak, INPUT);
pinMode(kPinLed, OUTPUT);
digitalWrite(kPinButtonGas, HIGH);
digitalWrite(kPinButtonBreak, HIGH);

}

int delayTime=1000;
long lastTime =0;
int ledState = LOW;

void loop() {
if(digitalRead(kPinButtonGas) == LOW){
delayTime=delayTime --;
}
else if(digitalRead(kPinButtonBreak)==LOW){
delayTime=delayTime ++;
}
delayTime = constrain(delayTime, 10, 5000);
if((lastTime + delayTime)<millis()){
if(ledState ==LOW){
ledState =HIGH;
}
else{
ledState=LOW;

}
digitalWrite(kPinLed, ledState);

lastTime = millis();

}
delay(100);

}

delayTime=delayTime ++;

The behavior of this is undefined. You're mixing up two different syntax.

Either write:

delayTime = delayTime + 1;

or write:

delayTime++;

But you cannot mix the two.

Same issue with the one above using --.

if((lastTime + delayTime)<millis()){

Also note that this line has a bug at millis rollover where lastTime + delayTime will equal some very small number before millis rolls over and is still equal to some large number.

It should be written like this to be safe.

if(millis() - lastTime > delayTime)

Why my program doesn`t work.

It does work. That it apparently does not meet your expectations simply means that your expectations are wrong.

To help us help you realign them, you must tell us what the program actually does, and what you expect it to do.

You also need to tell us, with a schematic, just how the switches are wired. You should also explain why you are not using the built in pullup resistors.

Mixing millis() and delay() in the same program is silly. Stop that!

Declare lastTime as unsigned long to avoid it going negative.