reading digital INPUT for 30 sec: at least one 1

Hi!
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?
I have never used arduino's clock, timer...

You could just use the millis() function to track passage of time and along with proper standard code to accomplish that.

Lefty

Thanks!
I'm losing my mind... why does it enter in BOTH first and second "if" at the beginning, than it enters in BOTH second and third "if"?
:o

long start;

void setup(){
  pinMode(12, OUTPUT);
  Serial.begin(9600);
}

void loop(){

  start = millis();    
while (digitalRead(8)==LOW){
  if ((millis() - start) <= 15000){
    Serial.println("1");
    digitalWrite(12, HIGH);
    delay(1000);
    digitalWrite(12, LOW);
    delay(1000);
  } 
  if (15000 < (millis() - start) <= 30000){
    Serial.println("2);
    digitalWrite(12, HIGH);
    delay(500);
    digitalWrite(12, LOW);
    delay(500);
  }      
  if ((millis() - start) > 30000){
    Serial.println("3);
    digitalWrite(12, HIGH);
    delay(150);
    digitalWrite(12, LOW);
    delay(150);

  }         
}

}

Your second 'if' is legal C, but isn't sensible.

You mean it cannot work with Arduino, right?
I've solved this way:

if ( (x>15000) && (x<30000) )

Thanks a lot :wink:

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.

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!

I would run the input into an interrupt pin (if you can), then

volatile int seconds;

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

loop () {

   if ((millis() % 1000) == 0)
      seconds++;

   noInterrupts();
   if (seconds > 30) {
     interrupts();
     its_been_longer_than_30s();
     seconds = 0;
   }
   interrupts();
}

isr () {

   seconds = 0;

}

Rob