I've been scouring the internet trying to find the answer to this dilemma however was unable to figure it out. A synopsis of what I'm attempting to do is move four servos after 20 seconds and make the light stay on. Code is down below. (Btw im new at this so if this doesn't make any sense sorry in advance)
millis() wraps around to zero after 49 days but if you're not running the Arduino for more than that then the above will be fine. Else, use this in loop:
So just to be sure, apart from what you mention there as part of your need not yet met, you're happy that @gerivega 's solution works in setup() not loop() and happens just once at power up or reset, and that's what you want?
Yes I'm ok with moving things to setup() rather then loop(). I had put my work in loop() because of the if(){} statement. As long as the light blinks for every second until the timer reaches zero, and at zero the light stops blinking while the servos simultaneously actuate, I'm ok with whatever.
Well you didn't mention that before Do you want the light to flash ON at every second, or cycle for on for a seconds, off for a second? (so the light comes on every two seconds)
All I wanted was to make my previous code work because I thought it made sense. Then I saw other code that literally does the same thing with no if()else() statement and decided that I could do that. This isn't an assignment btw because that would be plagiarism. This is more for me to figure out what I did wrong and other means to figure out this problem. Also thank you for said assistance with the code
void loop() {
// A static variable is only visible inside the scope it's declared in. So this keeps it local to loop();
// It also 'exists across time' so to speak. I.e it's not 'reinitialised every time through the loop.
// We'll use a moveComplete flag so that we don't accidentally re-enter the if statement when millis() wraps around to zero.
static bool moveComplete = false;
// We'll use a timerExpired flag so that we don't continually flash the LED after the 20seconds has expired
static bool timerExpired = false;
// timeCapture will be used to hold the current time at a given point so we can compare it to how much time has elapsed...
//... since we last toggled the LED
static unsigned long timeCapture;
// millis() is an arduino function that gives you how many milliseconds the arduino has been running for since it started up...
// ... it's effectively the 'current time'.
// We enter this statement if the current time in milliseconds is greater than or equal to 200000 millis (i.e 20 seconds)... as in if 20 seconds ...
// has passed since we started the Arduino, AND the moveComplete flag has not been set to true yet...
if (millis() >= 20000 && !moveComplete) { // ! before a variable means 'not' so !moveComplete means that moveComplete = false
// 20000 above actually is a bit of a 'magic number' you should probably move it to a #define above like the rest.
//Then we move the servos
moveAllServos(90);
// We set the LED HIGH
digitalWrite(LED_PIN, HIGH);
// We then set the timerExpired flag to true, so that the LED flash code block cannot be entered.
timerExpired = true;
}
// We enter this statement if the current time, minus the last time we captured the current time is greater than the flash interval / 2
// (the flash interval was every 1 second (1000ms) so we want to toggle the led every 500ms)
// AND the timerExpired flag is still false (because if the timer has expired we want to stop toggling the LED)
if ((millis() - timeCapture) > FLASH_LED_EVERY / 2 && !timerExpired) {
// If we're inside the statement we update our timeCapture with the current time so we can compare is to the next +500ms interval
timeCapture = millis();
// Read this inside out... so digitalRead(LED_PIN) returns true or false depending on if the pin is HIGH or LOW
// so !digitalRead(LED_PIN) will give the opposite of whatever digitalRead returns.
// Effectively were saying write to the LED_PIN, the opposite of whatever it currently is. So this toggles it on/off.
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
}