Go Down

Topic: Push Button - Servo Motor - Variable problem (Read 358 times) previous topic - next topic

Gedon

Hey guys need help on some programming logic...
i'll go straight through the important part of my code:
Code: [Select]

.
.
.
void loop(){
buttonstate = digitalRead(button);
if (buttonstate == HIGH){
   num = num - 1;
   myservo.write(45);
}
lcd.setCursor (0,5);
lcd.write(num);
}


So here's the thing,
I know the logic of the code would be if you press the push button the servo motor would move to the programmed degree and the "num" variable would be subtracted by 1. I imagine if the user pressed and hold the push button the servo motor would move (but in real life it would seem like it wouldn't) and the variable "num" would continuously decrement by 1.

The outcome i'm trying to pursue is that even if you push and hold the button, the "num" variable will only decrement once. After releasing and pushing the button again would the "num" variable decrement again.

Any advise/codes on how can i do this?

HazardsMind

There is an example provided to you in the IDE called "button state change" This will do exactly what your discribing.
My GitHub:
https://github.com/AndrewMascolo?tab=repositories


Go Up