Reading Input Count within a certain timeframe and output either true or false to Virtual Pin

I want to use NodeMCU on gate operator, the gate opener has an Led that Flashes indicating different states,
Ex. Constant ON- Gate Open
Constant OFF- Gate Closed
Slow Flash- Gate Opening
Fast Flash- Gate Closing
Two flashes every 2 seconds- Mains fail
So I need to count the pulses in a defined time to tell the state and provide an output accordingly.
Any help or guidance, please

  • When you start a test of teh current state save the value of millis() as the start time.
  • In loop(), count any pulses received.
  • Each time through loop() calculate the elapsed time by subrtracting the start time from the current value of millis().
  • You now have the number of pulses received in the period on which to base the determination of the current state

How about counting the number of times the light changes in 5 seconds. A power fail will be close to 8. Fast and Slow blinking will be larger numbers. A solid ON or solid OFF would be 0 changes.

const byte LightSensorPin = 4;

boolean LightWasOn = false;
unsigned EdgeCount = 0;
unsigned long FirstStateChangeTime;

void setup()
  FirstStateChangeTime = millis();

void loop()
  unsigned long currentTime = millis();
  boolean lightIsOn = digitalRead(LightSensorPin);
  // or lightIsOn = analogRead(LightSensorPin) > Threshold;

  if (lightIsOn != LightWasOn)
    LightWasOn = lightIsOn;

    // Light has just changed state

  // After 5 seconds
  if (currentTime - FirstStateChangeTime > 5000)
    Serial.print("Changes in 5 seconds: ");
    if (lightIsOn)
      Serial.println(" and the light is ON");
      Serial.println(" and the light is OFF");

    // Reset for next 5 seconds
    FirstStateChangeTime = currentTime;
    EdgeCount = 0;

Thank you, I will try it that way, boards arrived just now.
What I am still struggling is, serial print is easy, but so send data to Blynk 2.0 App is still rather complicated for me, see I need to interpret said values and was think of sending to various virtual pins depending on the counts.
For instance Virtual PIN:
V100 = Gate Closed LED constant ON
V101 = Gate Open LED constant ON
V102 = Gate Opening LED Slow flash
V103 = Gate Closing LED Fast Flash
How would I go about it?
Then I can easily create widgets in Blynk to show the different gate Status
Any help please, I am still very new to Arduino programming only about 1mnth experience

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.