I have 2 different functions for blinking an LED. When the device books up I want it to run function A for 2 mins and then run function B in a loop forever. What is the simplest way to achieve this?
put function a in setup with 2 min delay and b in loop
unsigned long ms;
bool func2;
void setup() {
func2 = false;
ms = millis();
}
void loop() {
if (!func2) {
if (millis() - ms <= 1000L*60*2) {
func1();
}
else {
func2 = true;
}
}
else {
func2();
}
}
Danger -- overflowing an int gives undefined behavior:
try:
if (millis() - ms <= 1000L*60*2) {
Predict the output of:
void setup() {
Serial.begin(115200);
if (1000 * 60 * 2 > 10000 ) {
Serial.print("overflowed signed int greater than 10K");
}
if (1000L * 60 * 2 > 10000 ) {
Serial.print("unsigned int greater than 10K");
}
}
void loop() {
}
Edit:
Or simpler: Would you expect this to print -11072
?
Serial.println(1000 * 60 * 2);
Thank you!
Perfect! Thank you. I don't think you can define func2 as bool and a function though, can you?
Yes, you are right, sorry for that
simplest way is what i said(i think)
It definitely is, but glutio's solution caters better to my use case, since I also want to be able to switch between func1 & func2 via button press, which starts the timer over by setting ms = millis();
and mode = !mode;
on button press. I should have indicated that in my post.
ok....well...yeah that figures
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.