Pages: [1]   Go Down
Author Topic: reading digital INPUT for 30 sec: at least one 1  (Read 4078 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 331
Posts: 16514
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Lefty

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:


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);

  }        
}

}
« Last Edit: January 19, 2011, 03:27:22 pm by cioto » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24322
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Thanks a lot ;-)
Logged

0
Offline Offline
Shannon Member
****
Karma: 161
Posts: 10431
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Newbie
*
Karma: 0
Posts: 44
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8439
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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

« Last Edit: January 23, 2011, 07:33:38 am by graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: