Question about timer and counters with delay for "bounce"

Environment -
Mac Sierra 10.12.6
Arduino 1.8.3
Blynk 2.10.1

I have a successful project that monitors several remote sensors (temp, humidity, etc.).
All of the readings are called using the Blynk.timer function on a 60 second interval.

I need to code a new function that will count the revolutions of a waterwheel using a Hall Effect sensor. In past sketches, I would use a “counter” within a “while” loop to limit the function to perhaps 60 seconds while monitoring the “prior state” and “current state” of a digital pin attached to the Hall sensor. Because the sensor is acting as a switch and to prevent what has been referred to as “bounce” I would add a brief delay of 100 milliseconds every time I read and compared the state of the digital pin. That code does not seem to work in the Blynk environment.

The question is - what logic would you use to read a change of state using a Hall effect sensor to count the revolutions during a specific time period? And if I do not put in a delay, would you think the on/off state of the digital pin would be recognized? It is not a latching sensor.

After reading the posts on timers … I am thinking of perhaps imbedding a second timer “interval” function within the “counter” loop so that the comparison of the on/off state is effectively delayed for 100 milliseconds to let the wheel revolve. Does that make sense?

My current understanding of the way timers work is as follows:
The timers are “called” in the setup() section of the sketch.
They are used to call functions that perform various operations.
ALL of the timers are activated at one time by the command within the loop() section
That means that if I have 4 functions being called by 4 timers at 15 second intervals, they all run at the same time and return whatever the function creates.
If I stagger the interval times … the functions may run at different times. For example, I may want to monitor temp every 60 seconds, but only refresh the LCD every 5 minutes.
Am I correct?

Thanks for the education and advice.


They are called wherever you put them, not exclusively in setup().

The MCU’s can’t multi-task so they will follow each other.

You might want to upgrade… current is 2.15.0

(As stated above) That is typically where you set them up, but not limited to there. You can have a timeout timer runn in a independent function as required.

The void loop() runs hundreds/thousands of times a second, so the is basically checking all timers to see if it is 'time" for them to run.

Yes… I have many timers running in my main bench test… all spaced out to run at different intervals. NOTE: the order I have them listed in is semi-chronological for my own viewing, it is not necessary to do so for proper operation.

  // ===== Timer Setup =====
  PingTimer = timer.setInterval(550L, PingSensor);  // Ping distance routine - switched on via code
  timer.disable(PingTimer);  // Disable timer until needed

  PIRTimer = timer.setInterval(650L, PIRSensor);  // PIR motion routine - switched on via code
  timer.disable(PIRTimer);  // Disable timer until needed

  timer.setInterval(200L, whiteLED);  // White LED button monitor on CodeShield

  //timer.setInterval(500L, readVcc);  // Voltmeter

  timer.setInterval(995L, clockDisplay);  // Time and Date Widget

  timer.setInterval(1005L, SuperCapVoltage);  // Super Cap charge display

  timer.setInterval(2000L, AnalogSensors);  // Misc analog sensors on CodeShield

  timer.setInterval(2500L, DHTSensor);  // DHT22 sensor

  // This is a named timer, so as to allow activate/deactivate, change timing, etc. further in the code.
  // Similar to what is done with the PIR and PING timers above.
  HTBLEDTimer = timer.setInterval(5000L, HeartBeatLED);  // Heartbeat LED  

  timer.setTimeout(10000L, StartUpEmail);  // Send startup email once at bootup

  timer.setInterval(30001L, thermTemp);  // Thermistor  on CodeShield

  timer.setInterval(60000L, HeartBeatPrint);  // Heartbeat Print

  timer.setInterval(59990L, sendUptime);  // Up Time Widget

}  // END Setup Loop

Thank you Gunner for the reply -

So, If I am thinking correctly, I could put a timer into another function to act as a “delay” to allow the Hall sensor time to change state on the monitored pin, increase the counter for the “while” loop and count the change of state as a revolution of the wheel.

Do you have any other suggestion of the “logic” I may want to follow to count the revolutions of a wheel using a Hall sensor?

I think the latest version for the iOS is 2.10.1 Could be wrong … but upgraded today on my iPhone.

Opps, sorry about the version then… i thought IOS had a slightly different numbering scheme. I was refering to Android versions.

This is a group of functions I use for sweeping a servo back and forth without blocking. Perhaps you can dig through it to see how the timer works… It isn’t fancy or optimised, but it works:

//===== Servo Sweep Switch Function =====
BLYNK_WRITE(V0) // Switch Widget
  SrvoButtonState = param.asInt();
  if (SrvoButtonState == 1) {
    Blynk.setProperty(V0, "color", "#00FF00");
    timer.setTimeout(900L, ServoCW);  // Run Servo1 loop every 800ms
    Serial.println("Servo Sweep Active");
  } else if (SrvoButtonState == 0) {
        Blynk.setProperty(V0, "color", "#04C0F8");
    Serial.println("Servo Sweep Deactivated");
} // END Blynk Function

void ServoCW() {
  timer.setTimeout(900L, ServoCCW);  // Run Servo2 loop every 800ms
  Blynk.virtualWrite(V10, 160);
}  // END Servo1 Loop

void ServoCCW() {
  Blynk.syncVirtual(V0);  // Check if switch still activated
  Blynk.virtualWrite(V10, 0);
}  // END Servo2 Loop

//===== Servo Slider Function =====
BLYNK_WRITE(V10) // Servo Slider Function
  SrvoPos = param.asInt();
  timer.setTimeout(1000L, ServoDetach);
} // END Blynk Function

void ServoDetach() {