Seems you're trying to make it read a discrete click to do one increment or decrement, which might be over-complicating things unless your assignment specifically asks for that. I'd have gone for the simpler approach of just inc/decrementing while the button is held down.
It is your sketch in Wokwi, I have not changed it.
Do you have the pullup resistors to 5V ?
Tips:
Make the text of the source code look better. It is not possible to make a good sketch when the text layout is not good. Put every space, every indent, every comma, every new line, at the right place.
The first line has spaces around the "=". The third line has no spaces around "-=". That is not consistant.
The names "but" and "but2" have no meaning. They can be called "slowerButtonPin" and "fasterButtonPin".
In the setup(), you can use INPUT_PULLUP for extra safety. Together with the external pullup, they will keep the input HIGH when the button is not pressed.
You have different code for the two buttons. The buttons should be treated in the same way.
You use the 'lastButtonState' for both buttons. Then you mix things that should not be mixed. Better names will help, for example "lastSlowerButtonState" and "lastFasterButtonState". Rewrite that part of the code, it is not there yet.
Set the initial value of those two variables to HIGH. That is the default when no button is pressed.
Do as silvershovel wrote. Let the led blink at the end of the loop(), there is no need to do something with the "ledState" or "digitalWrite()" at the moment a button is pressed. Use the buttons to only change the delay.
Start with a delay of 100ms or 200ms, then you see something happening faster.
This assignment SEEMS simple; however there are complications.
What happens if the button shows switch bounce?
What if both are pressed at once?
What are the limits of the "faster" or "slower"?
A VERY simple interpretation of the assignment would allow just two flash rates - "fast" and "slow". A bit like this:
int flashrate;
int fast = 200;
int slow = 500;
loop{
if (button a pressed) flashrate = fast:
if (button b pressed) flashrate = slow.
blink(flashrate);
}
and a simple routine using the "blink" program
void blink (int rate) {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(rate);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(rate);
}
And there's the voice of experience- 80% of the time the actual coding is simpler than the effort spent trying to understand the requirement in the first place.
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin
const int Button1 = A0;
const int Button2 = A1;
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long interval = 1000;
// 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 unsigned long Interval1 = 1000; // interval at which to blink (milliseconds)
const unsigned long Interval2 = 100; // interval at which to blink (milliseconds)
const unsigned long IntervalStep = 25;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
Serial.begin(9600);
}
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;
// read button and make action
if (!digitalRead(Button1) && interval< Interval1) interval +=IntervalStep;
if (!digitalRead(Button2) && interval> Interval2) interval -=IntervalStep;
// 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);
Serial.println(interval);
}
}
Have a nice day and enjoy coding in C++.
Дайте миру шанс!