Go Down

Topic: reading digital INPUT for 30 sec: at least one 1 (Read 4 times) previous topic - next topic


Problem: I have a digital input connected to a digital sensor, that of course sends 0/1.
I need to check that, in 30 seconds, there has been at least one 1.
How would you do that?

It depends on how short the pulses from the sensor are.  If it can send very short pulses and you don't want to miss any, polling the pin in software is not the right solution - monitoring the pin via an interrupt (pins 2 and 3 only) or using a counter is the right way.

If the pulses are long and you can guarantee to poll with digitalRead() often enough then this is a simpler solution.   pulseIn() works nicely if you don't have any other task to perform while waiting.

Another approach is to add some extra hardware to stretch or latch the pulses you are looking for, so the software doesn't have to work so hard.
[ I won't respond to messages, use the forum please ]


The pulses may be quite short, but I think at least 10 ms.
Thanks so much, I will check about interrupts and pulsein function.

As regards the hardware addition, I totally agree and had already thought about that: do you think a simple s-r flip flop could be ok? Which component could be used?
Thanks a lot!


Jan 23, 2011, 01:29 pm Last Edit: Jan 23, 2011, 01:33 pm by graynomad Reason: 1
I would run the input into an interrupt pin (if you can), then

Code: [Select]
volatile int seconds;

setup () {
  attachInterrupt(1, isr, RISING);

loop () {

  if ((millis() % 1000) == 0)

  if (seconds > 30) {
    seconds = 0;

isr () {

  seconds = 0;



Rob Gray aka the GRAYnomad www.robgray.com

Go Up