Hi guys, im super new to arduino having only really started a few days ago so please don't skin me alive if this is a simpleton question.
I have a sensor that goes low when it detects a signal, so far i am able detect this change and turn on a led on my arduino, but now i want to only turn on the led if the signal is detected for longer than say 1 second, in order to prevent false alarms.
My code so far
if (Sensor_Status == LOW) {
digitalWrite(Led_Pin, HIGH); // turn LED ON
delay(6000);
Thanks.
Note the time that it goes low with millis( ).
After 1 second, read the sensor again, if still low then do whatever.
Something like this. All time elements are unsigned long
if (Sensor_status == LOW && waiting == 0){
endTime = millis(); // capture the time
waiting = 1;
}
elapsedTime = millis() - endTime; // time since started= currentTime - earlierTime
if (waiting == 1 && elapsedTime >=1000){
// read the sensor again
...
// still low?
if (Sensor_status == LOW){
waiting = 0;
// turn on the LED
}
else {
waiting = 0;
// do not turn on LED
}
}
Wow thanks, i think its gonna take me a while to figure out whats going on 
The one problem with Crossroad's approach is that it won't detect if the sensor went back to HIGH in that one second. So it doesn't really check that the sensor was LOW for the second only that it is still LOW after.
I'd handle it much like reading a switch. Using state change detection. It's similar to debouncing.
int currentSensorState;
int lastSensorState;
unsigned long readTimer;
void loop() {
unsigned long currentMillis = millis();
// I'll assume you read the sensor something like this
currentSensorState = digitalRead(SENSOR_PIN);
// check for change from last state
if (lastSensorState != currentSensorState)
{
lastSensorState = currentSensorState; // store the change
readTimer = currentMillis; // start the timer
}
// check if timing and if timing finished
if (readTimer > 0 && currentMillis - readTimer >= 1000)
{
// reset timer
readTimer = 0;
// If it makes it here it has been in the new state for a second
if (currentSensorState == LOW)
{
// do stuff for LOW state
}
else
{
// do stuff for HIGH state if you want
}
}
}
If, at any time during the one second, the sensor's state changes the readTimer will be set to a new millis() value and the timer will start again. Thus the sensor will have to be LOW for the entire second to pass.
Check out the sketches "Blink without delay" and "Debounce". And stop using delay(), it isn't scalable.
If I were evil and wanted to make as many beginners as possible give up in frustration, having everyone start with the Blink sketch using delay() would likely be my first choice.
polymorph:
Check out the sketches "Blink without delay" and "Debounce". And stop using delay(), it isn't scalable.
If I were evil and wanted to make as many beginners as possible give up in frustration, having everyone start with the Blink sketch using delay() would likely be my first choice.
thanks dude i was just staring to use denounce() for my 1 second delay, delay() really did not seem to be a simple solution