Hello there, I'm new in the world of coding and Arduino, i need guidance on how to make my sensor triggered after a set of period. On my current programming code, it will trigger instantly. After i insert delay command, the sensor gives output after the period and its is now accurate as how i want it to be. in my case, i want it to give HIGH output only after the sensor is triggered equal or more than 2 seconds. if less than that, the output will remain low. Hope that all the veterans and experts will provide advice and guidance with proper explanation to help me understand better.
This will go better if you post your current code using code tags. Can't tell you what to change if we don't see what you have. ![]()
Ron
It is better to use milli() for timing than delay(). Timing with millis() (or micros()) can be non-blocking so other things can happen during the timing cycle.
Non-blocking timing tutorials:
Blink without delay().
Beginner's guide to millis().
Several things at a time.
Have a squiz at this:
// following assumes switch active low
// ... you're looking for it to go low
// if that's not the case, reverse the logic
// based on #3 at https://forum.arduino.cc/t/hold-button-for-call/578751
switchState = digitalRead(switchPin);
if (switchState == HIGH) // so inactive
{
lastTimeSwitchWasNotActive = millis(); //effectively nudges the clock back to zero
// unless switch is active
}
if (millis() - lastTimeSwitchWasNotActive >= interval)
{
// make your output high
}
else
{
// make your output low
}
So a straight delay will not do it.
After you sense a valid state set a Boolean flag to true and then enter a while loop using the millis timer count to make sure you exit after two seconds.
Then in the body of the loop you check to see if the sensor still returns a valid reading when you read it. If not you set a Boolean flag from true to false.
Then when this while loop terminates if the flag is false the signal was not consistently valid over the whole of your two seconds.
I like the "nudge the timer back to 0" approach of the one I suggested, which I hasten to add was not my idea. The link in my example is to a post of one Robin2.
Wow, the Arduino community is surprisingly nice and supportive, thank for all the advices
To Ron: i just started the code and its is just if and else, and i dont have the full code yet
to groundFungus: thanks for the suggestions and thnks once more for providing the link for me and showing me where i can start.
To Grumpy_Mike: i heard about the Boolean before but coulldnt quite understand the use of it, i hope i could find a coding regarding the Boolean flag that can help me in my research. I understand what you are trying to say but sadly i failed to imagine the code structure cause i dont have enough knowledge yet.
To octopirate: thnks for helping me by provinding the code to help the others.
OK so here is a demo code I wrote for you. It is a simple example where it detects a button press and monitors the button for two seconds. When the two seconds are up it prints whether the button has been held throught the two seconds.
// use of flags to determine if a push button has been held down for two seconds continuously
// push button wired between input pin 4 and ground
// Demo code by Grumpy_Mike
unsigned long startTime;
unsigned long holdDuration = 2000;
byte inputPin = 4;
boolean held = false;
void setup() {
Serial.begin(9600);
pinMode(inputPin, INPUT_PULLUP); // so the pin returns HIGH when not pressed
}
void loop() {
// first wait until button not pressed
while( digitalRead(inputPin) == LOW) { }
delay(500); // bit of debounce
// next see if it has been pressed
if(digitalRead(inputPin) == LOW) { // it has been pressed
held = true; // flag to say it is pushed at the moment
startTime = millis(); // make a note of when it started
Serial.println("button pressed");
delay(100); // just to clear any debounce issues
// now monitor the state of the button continuously
while( millis() - startTime < holdDuration) { // wait for the time to pass
if(digitalRead(inputPin) != LOW) { // if the button has been released
held = false;
}
} // end of while loop
// now report if the button was held down continuously
if(held) { // this is the same as putting if(held == true) only shorter
Serial.println("Push button held down all the time");
}
else{
Serial.println("Push button released for some of the time");
}
} // end of it has been pressed if statement
} // end of loop
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.