[SOLVED] Pulse reading by analog port

I want a function that waits for a certain signal for the condition to be met and to be able to continue with the next

for example, a function that waits for three pulses 1 second apart each through the analog port. And if this condition is met, I continued with the following function

this is the type of signal you receive through the analog pin (state> 400)

This example code is one that tries to wait for the 3 delayed pulses for a set time. But the idea is that with the function it counts the 3 pulses regardless of the time it takes to arrive so that it is more exact. Because using delays what you are reading can fail because sometimes it does not send the pulses with an exact time

void n3(){
  digitalWrite(llave, HIGH);
  delay(1500);
  if (analogRead(led2) > 400) {
    delay(4500); // wait pulse
  }
  digitalWrite(llave, LOW);
  delay(1000);
}

And this is the other one that is supposed to wait until a continuous 2 second pulse arrives in order to continue. But the question is whether it can be done without delays. The idea is, if the function can be executed and it waits for a continuous pulse of 2 seconds to continue.

void waitpulse(){
  if (analogRead(led2) > 400) {
    delay(500);
      if (analogRead(led2) > 400) {
        delay(200);
    } else {
      enterpro();      
    }
      if (analogRead(led2) > 400) {
        delay(800);
    } else {
      enterpro();      
    }
      if (analogRead(led2) > 400) {
        delay(500);
    } else {
      enterpro();      
    }
  } else {
    enterpro();
  }
}

I would greatly appreciate the help[

Don't use delay() for timing as it blocks program execution

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Let loop() loop freely and use millis() timing to determine whether the required number of pulses has arrived in the required time period

What is the voltage of the pulses ?
If the voltage is suitable then why not use digitalRead() instead of analogRead() ?

Hello Jonhander. I believe u need to check also the LOW part of the pulse. Otherwise in your code a stable HIGH signal wil also count.
The given suggestion to check every certain milliseconds should work.

I will check how to apply the millis in my program to see if it works for me as I need it

UKHeliBob:
What is the voltage of the pulses ?
If the voltage is suitable then why not use digitalRead() instead of analogRead() ?

and don't use the digital pin because the pulse it receives has a voltage of 1.5v

oswe:
Hello Jonhander. I believe u need to check also the LOW part of the pulse. Otherwise in your code a stable HIGH signal wil also count.
The given suggestion to check every certain milliseconds should work.

Hi oswe. And how would you do it that way?

Use an interrupt and a counter inside the interrupt. On a 3 pulse train you will get 3 interrupts

if count is 0 save the time.

if count is 1 save the time and compare it with time from count 0 (should be a second)

if count is 2 save the time and compare it with time from count 1 (should be a second)
reset count and set a flag

The flag is checked in your loop and if set runs the function that you want.