I've ran into an issue that I belive can't be solved. Let's see if I'm wrong (which I do hope :o )
Let's say you have a void loop(), stuff happens inside that loop all the time. When I press a button a value goes from 0 to 1, inside this loop there is an if statement that will make things happen if the value turns 1, but before the stuff happens I want it to be a delay of 5 seconds, first that dont seem to hard, just write delay(5000), problem is that this delay will occur every time the loop starts over. I want the delay to happen only one time. I've tried like 30 ways all day long whitout any luck.
So in short words, I want a delay inside a loop that occurs one time. Appreciate anyone who can make this happen.
I've ran into an issue that I belive can't be solved. Let's see if I'm wrong (which I do hope :o )
Let's say you have a void loop(), stuff happens inside that loop all the time. When I press a button a value goes from 0 to 1, inside this loop there is an if statement that will make things happen if the value turns 1, but before the stuff happens I want it to be a delay of 5 seconds, first that dont seem to hard, just write delay(5000), problem is that this delay will occur every time the loop starts over. I want the delay to happen only one time. I've tried like 30 ways all day long whitout any luck.
So in short words, I want a delay inside a loop that occurs one time. Appreciate anyone who can make this happen.
Thanks
capture the millis() into unsigned EventCaptureTime variable one-time
Then keep checking ...
Thank you for the reply. I've tried this, the problem is that the event trigger is also inside the loop so there is no way for me to capture.
There is one solution / workaround but it requires me to be able to delay for X seconds but another function needs to be able to run in the background so to speak.
To make this easyer, this is an alarm system, if the system is triggered I need a few seconds to get to the deactivate button/panel before the alarm sounds. A Delay function delays the whole arduino which will make the alarm sound anyway.
I belive the best solution here is some sort of delay function that only delays a small snippet of code and no the whole system.
Help us help you.
Always attach your code using the </> icon in the posting menu.
Is this correct?
The first time you push the button you want a five second delay, then some action.
Then the 2nd and subsequent pushes of the button you want no delay but the same action.
Here's the loop code. Correct, first time it should be a delay then second and infinite times there should not be a delay. As you can see in the code, I've tried to do this with Millis() but it seems to use different timings every time depending om how long I press the button.
void loop() {
// read the LDR sensor
reading = analogRead(inputPin);
// check to see if the button is pressed
int buttonVal = digitalRead(armButton);
if ((buttonVal == HIGH) && (prev_buttonVal == LOW)) {
if (isArmed == false && isTriggered == false) {
arm();
}
}
// check to see if the laser beam is interrupted based on the threshold
if ((isArmed) && (reading < threshold)) {
isTriggered = true;
}
if (isTriggered) {
unsigned long then = millis();
// Turn Off the laser.
laserOff();
// Activate the FPS.
fps.SetLED(true);
if (validFP() == true) {
fps.SetLED(false);
reboot();
}
// Set delay before sounding the alarm.
if (times == 0) {
if (millis() - then > 5000) {
//SOUND THE ALARM
times = 1;
}
} else
if (times == 1) {
//SOUND THE ALARM
}
}
}
if ((buttonVal == HIGH) && (prev_buttonVal == LOW))
You are checking for a very specific state change and ignoring all others. Detect a state change, record the new state and use that result to decide what to do.
if (buttonVal != prev_buttonVal)
{
prev_buttonVal = buttonVal ; // always record the new state
if (prev_buttonVal == HIGH)
{
// button was pressed
}
else
{
// button was released
}
}
Thanks for the info Jimmy, i got it working by storing the last event, basically what you described. Once again thanks, I've been strugeling the whole day and now that I got it working I feel silly ^^