Go Down

Topic: Using if statement with time (Read 455 times) previous topic - next topic

Sanchitjain2003

Hey guys,
I have a project in which I need to use an if statement over a sensor's value for a specific time.

Like, 1. if sensor at A0 gives value> 500 for a time >8 sec
          OUTPUT
        2. if sensor at A0 gives value> 500 for a time < 6sec
          OUTPUT

Wawa

Hobby or school project.
You should have posted what you have tried.

Nothing else to do, so I wrote a compressed test sketch.
Leo..
Code: [Select]
boolean sensorState, prevState;
unsigned long startMillis;

void setup() {
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop() {
  sensorState = (analogRead(A0) > 500); // compressed if/else
  if (sensorState) { // if >=500
    if (!prevState) startMillis = millis(); // mark if just changed to >=500
    digitalWrite(10, (millis() - startMillis < 6000)); // compressed if/else
    digitalWrite(11, (millis() - startMillis > 8000));
  } else { // if <500
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
  }
  prevState = sensorState; // update
}

Sanchitjain2003

Thanks a lot for your reply. It worked. I would look forward to your guidelines, since I used this forum for the first time I actually posted it in a confused state. Hope you understand.

Again Thanks a ton.

Wawa

Glad it worked.
The forum posting guidelines are in a thread on top of every main page.
Leo..

not_a_noob

But what's supposed to happen between 6 and 8? Should the <6 output be cancelled?

The proposed solution (if I read the sketch correctly, that is ;) ) only makes the outputs low if it goes under 500 again, but I think the spec is silent on what's supposed to happen between 6 and 8.




Wawa

Pin 10 goes HIGH at 6 seconds, when (millis() - startMillis < 6000) becomes true.
Like writing
digitalWrite(10, 1);
or
digitalWrite(10, HIGH);
Same for pin 11 at 8 seconds.
I don't see what should happen between 6 and 8 seconds.
Things are instantly reset if analogRead is <500.
Because there is no delay() used in the code (loop runs fast).
Leo..



not_a_noob

Ok I get it now, but shouldn't this:

Pin 10 goes HIGH at 6 seconds, when (millis() - startMillis < 6000) becomes true.
.... say:

Pin 10 goes LOW at 6 seconds, when (millis() - startMillis < 6000) becomes false.
ie, the led on 10 goes on as soon as the sensor goes over 500, then goes off at 6 seconds?

So to summarise, and this is how it works on my rig with a pot on A0 and leds from pins 10 & 11 to ground:

If sensor < 500, both leds off.

If sensor > 500 and time < 6 seconds, only led10 on
If sensor > 500 and time > 6 seconds, both leds off until...
If sensor > 500 and time > 8 seconds, only led11 on


Sanchitjain2003

I would like to give some explaination of my project here,

Actually I am building a miniature signalling system for trains at unmanned level crossings. In this system I want that, (Right now I am using IR obstacle detection sensor for this, but I would be switching over to Ultrasonic sensor in near future, when I expand my project). This project will let the Loco Pilot of train know the status at unmanned level crossing, will instruct him to reduce speed by how much and will prevent accidents in future in India. The project also uses IoT technology.

1. When motion is detected for more than a half a second and less than 4 seconds, 2 YELLOW LEDS should glow.
2. When motion is detected for more than 4 seconds & less than 6 seconds, 1 YELLOW LED should glow.
3. When motion is detected for more than 6 seconds, 1 RED LED should glow.

Go Up