Question about pulseIn

Hello, sorry for my bad inglish, i am spanish.

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 aprox 2 segundos
{
derecha(20);
}

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.

Can you help me out?

Thank you!

intermitentesV1.ino (4.69 KB)

    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.

Thanks for responding.

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.

In the first post i attach the entire code .ino

Thank you very much.

You need to debounce your button take a look at debouncing in the playground section.

Mark

holmes4, I did not understand your answer, sorry.

Using Serial.print () shows these values, separated by tabs. Although many seconds press, usually 0:

0 145 0 0 0 0 0 323 463 0 0 549 2 0 0 0 0 0 0 0

Thank you.

What is it you are trying to accomplish with pulseIn()? There are probably better ways.

When you push a button you often get a small set of pulses before the button stays in the on condition this is know as bounce.

The state of the button goes from off to on to off to on to off before it stays on.

pulseIn (as used in your code) will only report the first of these bounces.

Mod your code so that it ignores any pulse that is less than say 100mS and you should be OK.

Mark

Thank you.

It would be something like this?

PulseIn (pushright, HIGH, 100000);???

If this does not work, I have another idea, I will make a loop and an operation with millis ().

I'll tell you! Thank you very much.

Solved:

timebase=millis();

if(digitalRead(pushright)==HIGH && digitalRead(pushleft)==LOW) //si se pulsa el derecho
{
do
{
tright=millis();
}while(digitalRead(pushright)==HIGH);

tright=tright-timebase;
Serial.print(tright);
Serial.print( "\n");
if(tright<=1200) //si se pulsa aprox 1 segundo
{
derecha(7);
}

else if(tright<=3000) //si se pulsa aprox 2 segundos
{
derecha(20);
}

else if(tright>3000) //si se pulsa más de 3 segundos
{
derecha(70);
}

else
{
error;
}
}

Thanks!!!!!!!!