I am trying to make a simple debounce function as explained here but the condition if((millis()-timeCheck)>reboundDelay)
is never met.
As you can see in my code below, I am trying to control a PWM with two push buttons. I am currently sending it on a LED.
With AS7 and vMicro I put some breakpoints and realized that timeCheck
was stuck at 0 and was an unsigned int
instead of the unsigned long
I declared. With the debugger I looked inside of the millis()
function and saw that timer0_millis
wasn't increasing neither, stuck at 0.
Here is my short code (it's a sketch, it can surely be rewritten in a more efficient way) :
int PWM=9;
int dutyP=2;
int dutyM=3;
int duty=123;
bool stateP=0;
bool stateM=0;
bool prevStateP=0;
bool prevStateM=0;
unsigned long reboundDelay=500;
unsigned long timeCheck=millis();
void setup() {
pinMode(PWM,OUTPUT);
pinMode(dutyP,INPUT);
pinMode(dutyM,INPUT);
}
void loop() {
stateP=digitalRead(dutyP);
stateM=digitalRead(dutyM);
if(stateP != prevStateP || stateM != prevStateM) {
timeCheck=millis();
}
if((millis() - timeCheck) > reboundDelay) {
if(stateP==1) {
duty=duty+25;
} else if(stateM==1) {
duty=duty-25;
}
analogWrite(PWM,duty);
prevStateP=stateP;
prevStateM=stateM;
}
}
And finally, the strange part. When I upload it on my Genuino Uno for like 30 seconds, the pushbuttons do nothing (it may be that the if
works) and afterwards I can increase and decrease the duty cycle but without the delay that the condition should bring (the LED flashes really fast due to an overflow in analogWrite).
Any help is welcomed, the strange part is secondary to me but I would really like to make millis()
work properly.
loop(){Serial.println(millis());delay(500);}
If you are right about millis not moving then this will prove it. There may be something in the debuggers that interfere with millis.