I want to stop millis when it's more than a certain time.
Is there any good way?
title_:
I want to stop millis when it's more than a certain time.
Is there any good way?
There's no good reason.
want to stop millis when it's more than a certain time.
Why ?
If millis() goes past a value that you are interested in then either stop calling it or ignore the value that it returns
I'd like to stop the timer using millis under certain conditions.
What timer would that be ?
Strange times indeed.
See Using millis() for timing. A beginners guide - Introductory Tutorials - Arduino Forum Using millis() for timing. A beginners guide by UKHeliBob for your education.
When you use the clocks in your house for timing, do you stop them forcibly? Well, you don't forcibly stop millis() either.
I made a six-digit timer using millis, with the fifth digit going up one by one per second.
I want the timer to stop after 30 seconds.
Can't the timer made with millis stop?
I made a six-digit timer using millis, with the fifth digit going up one by one per second.
I want the timer to stop after 30 seconds.
So stop updating the display after 30 seconds
Abandon all thought of stopping millis()
We might be able to help you further when you post your code. If you post your code, please post correctly. The three locked posts at the top of this forum (and what they link to etc) will explain how to do that.
The most important actions are to use the Arduino IDE autoformat feature, and use copy-and-paste, and use code tags to post inline. Attachments cannot usually be viewed by the many helpers using mobile devices. Sad but true.
Auto format the code in the IDE using Ctrl/T then right click and select Copy for Forum and paste it in a post here and the code tags will be added automatically for you
Is there any good way?
There are good ways to do whatever it is that you really want to do, but stopping the millis() timer is not one of them.
- Capture the elapsed time millis() has been “stopped” for: ElapsedStopped = MillisRestarted - MillisStopped
- Subtract ElapsedStopped from all future millis() values.
Edit: Something like...
bool isRunning = true;
uint32_t elapsedStopped, millisStopped;
void stopMillis() {
if (isRunning) {
millisStopped = millis();
isRunning = false;
}
}
void startMillis() {
if (!isRunning) {
elapsedStopped += millis() - millisStopped;
isRunning = true;
}
}
uint32_t getMillis() {
return (isRunning ? millis() : millisStopped) - elapsedStopped;
}
If you use a digital watch with a start-stop time-measuring-functionality.
Does the "general" time stop when you stop the start-stop-time-measuring-function?
No it doesn't. The general clock runs and runs until the battery is empty.
You will have it fifty times harder to make millis() stop than coding your own start-stop counting functionality.
To make millis() stop you would have to learn a lot about interrupts, interrupt-registers and mess around with the core-code of the arduino-IDE.
it is as simple as adding some if-conditions
boolean timer_run
if (!timer_run && "startButtonpress") {
timer_run = true;
StartMillis = millis();
}
if (timer_run) {
//insert your 6-digit-timer-code here
if (currentMillis - StartMillis) >= 30000) {
timer_run = false
}
}
the first if-condittion checks for timer_run beeing false
if start-button is pressed timer_run is set to true
which means the if-condition
if (!timer_run) becomes false
the "!" means not !false results in true
!true results in false
effect: no more updating of startMillis
next condition chekcs for timer_run to be true
the next if.condition checks if 30 seconds have passed by
if this is the case run_timer is set to false
if (run_timer) is false your 6-digit-timer-code gets not executed
best regards Stefan
You can, just do the call to stop interrupts. I forget the name of the call, but these guys will know what it is.
(Its like handing him a gun. "Sure, use this and they'll stop working.")
-jim lee
/Start of bad advice
@Jim Lee. I think you are referring to noInterrupts() which indeed will stop millis. ![]()
If you power off your arduino then millis will stop counting too and even reset to 0 next time you apply power ![]()
/End of bad advice
No interrupts:
no serial send receive
no I2C-communication,
no SPI-communication
no timing
@Jim Lee provided a clear answer to the question. OP did not ask about side effects ![]()
No cigar
J-M-L:
@Jim Lee provided a clear answer to the question. OP did not ask about side effectsNo cigar
What? OP(s) can read the stuff inside parentheses?