Hi everyone, how can I create a countdown using millis()?
I'm building a rocket, so I need to print with the Serial.print func a number every second. I could use the delay() function, but I also need to have a way to stop the countdown.
Help me!!!
Start by reading Using millis() for timing. A beginners guide and look at the BlinkWithoutDelay example in the IDE.
Using the BWOD principle decrement a number each time a one second interval ends and print it. Read the cancel key each time through loop()
You guys in the same class?
Did you read the locked topic Using millis() for timing. A beginners guidehttps://forum.arduino.cc/index.php?topic=503368.0 at the top of the forum?
“Help me!!!”
Help us!!!
Use CTRL T to format your code.
Attach your complete sketch between code tags.
Use the </> icon in the posting menu.
[code]Paste your sketch here[/code]
Show us a good schematic & image of your circuit wiring.
Posting images:
http://forum.arduino.cc/index.php?topic=519037.0
larryd, I think that posting my sketch and wires would be a lot confusing. I have like 30 wires connected to my breadboards and my code is more than 150 lines long. It doesn't contain only the countdown, but also a lot of functions and stuff for Servos, potentiometers, piezo.... I'll revise the articles that you mentioned, even if I already did it lol.
I'll tell ya if I find a way, thx
Use the blink without delay example in the IDE
Every second, reduce the countdown by one.
When the count reaches zero, light the blue touch paper, and retire at least 25 metres.
What's the problem?
150 lines of code? That's a pretty small sketch imo.
larryd, . . . , thx
Well you could attempt a simple sketch and wiring circuit to come up with a solution to this problem.
Get this part of the code working with/without our help, then add it to your 150 lines of code.
It is always easier to separate a problem from the whole when you have problems or are testing things.
const long interval = 1000;
unsigned long currentMS = 0;
unsigned long prevMS = 0;
int countdown = 30;
void setup() {
Serial.begin(9600);
}
void loop() {
currentMS = millis();
while(int i = 0 < countdown) {
if (currentMS - prevMS >= interval) {
Serial.println(countdown - i);
i += 1;
}
}
}
It doesn't print anything...
while(int i = 0 < countdown) {
You didn't get that from blink without delay.
while(int i = 0 < countdown) {
I think you started with a for() loop and didn't complete the change to while(). Declare i outside the loop and give it its initial value. Then use the less-than comparison in the while().
No I didn't, but I thought that I needed it(?)
Tried to fix it:
const long interval = 1000;
unsigned long currentMS = 0;
unsigned long prevMS = 0;
int countdown = 30;
int control = 1;
void setup() {
Serial.begin(9600);
}
void loop() {
currentMS = millis();
if (currentMS - prevMS >= interval && control == 1) {
countdown -= 1;
Serial.println(countdown);
if (countdown < 0) {
control = 0;
}
}
}
prevMS
Updated. . . where?
Ok, I think that I found the solution:
const long interval = 1000;
unsigned long currentMS = 0;
unsigned long prevMS = 0;
int countdown = 30;
int control = 1;
void setup() {
Serial.begin(9600);
}
void loop() {
currentMS = millis();
if (currentMS - prevMS >= interval && control == 1) {
countdown -= 1;
Serial.println(countdown);
prevMS = currentMS;
if (countdown = 0) {
control = 0;
}
}
}
Yeah, I forgot to update the prevMS lol
if (countdown = 0) {
oops
AWOL:
Use the blink without delay example in the IDE
Every second, reduce the countdown by one.
When the count reaches zero, light the blue touch paper, and retire at least 25 metres.What's the problem?
25 metres? It will go up to 2 kilometers at the end of the entire project! (it will take something like 2 years but ok lol)