I'm trying to make a driver for my bike intermittent. I want to press the button 1 second intermittent flashes 7 times, pressing two seconds, flashes 20 times and pressing more than 3 seconds, flashes 70 times.
if(digitalRead(pushright)==HIGH && digitalRead(pushleft)==LOW) //si se pulsa el derecho
{
tright=pulseIn(pushright, HIGH); //tiempo que se pulsa
tright=tright/1000; //pasamos de micro a milisegundos
if(tright<=1200) //si se pulsa aprox 1 segundo
{
derecha(7);
}
else if(tright>3000) //si se pulsa más de 3 segundos
{
derecha(70);
}
else
{
error;
}
}
I have written this piece of code, but ignores the seconds down and always keep it for 7 times. I think it's because as in the header of "if", I do a digitalWrite and while continuing to hold the button in the "if" expected move from LOW to HIGH to start counting, then stop going from HIGH to LOW . But as it enters the "if" with the button down, never goes from LOW to HIGH, and falls under the "if" in HIGH.
if(tright<=1200) //si se pulsa aprox 1 segundo
{
derecha(7);
}
else if(tright<=3000) //si se pulsa aprox 2 segundos
{
derecha(20);
}
You need a Serial.print() statement (or two) to show what the value of tright is. If it is less than or equal 1200, the first block will be executed. If it is greater than 1200 and less than or equal to 3000, the second one will. In determining whether the code is behaving correctly, it is important to know what the value in tright is.
tright=pulseIn(pushright, HIGH); //tiempo que se pulsa
The pulseIn function is not intended to be used for switches pressed by humans. It is for measuring much shorter times than that. The function will actually wait for you to release the switch and press it again. Is that really what you want?
else
{
error;
}
What is this supposed to be doing? It is not calling a function.
I'll try to put a Serial.print (), but while I hold down the button, the LEDs are lit before you release it. I think it means that "PulseIn" time does not count.
I need to know which button or buttons pulse, yet know how long they are pressed to determine the operating ranges.
And I put the error function always as a precaution, in case the event occurred "else" and stay standing loop. Error is a function that turns on and then off the LED 13.