Go Down

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

cioto

Jan 19, 2011, 07:57 pm
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...

retrolefty

#1
Jan 19, 2011, 08:09 pm
You could just use the millis() function to track passage of time and along with proper standard code to accomplish that.

Lefty

cioto

#2
Jan 19, 2011, 09:22 pmLast Edit: Jan 19, 2011, 09:27 pm by cioto Reason: 1
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"?

Code: [Select]
`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);  }         }}`

AWOL

#3
Jan 19, 2011, 09:25 pm
Your second 'if' is legal C, but isn't sensible.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

cioto

#4
Jan 19, 2011, 09:35 pm
You mean it cannot work with Arduino, right?
I've solved this way:

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

Thanks a lot ;-)

MarkT

#5
Jan 20, 2011, 03:24 am
Quote
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 will NOT respond to personal messages, I WILL delete them, use the forum please ]

cioto

#6
Jan 21, 2011, 02:47 pm
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!

#7
Jan 23, 2011, 01:29 pmLast 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)      seconds++;   noInterrupts();   if (seconds > 30) {     interrupts();     its_been_longer_than_30s();     seconds = 0;   }   interrupts();}isr () {   seconds = 0;}`

______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

Go Up