Zaehlen von millis() stoppen nach erreichen eines Wertes
How to stop a timer delay using millis() from executing with NRF24L01 Network (Arduino)
Millis over run, how to stop Millis after successful run
Code to stop millis() - #8 by rmetzner49 - Programming - Arduino Forum
Videos
One of the most frequent recommendations I make when auditing Arduino code comes to the difference in use cases for millis() and delay(). This little blurb should help you to differentiate the two and understand why you would use one over the other.
First, millis() is an essential function in programming that returns the elapsed time in milliseconds since the board began running the current program. Unlike other timing functions, millis() is non-blocking, meaning it does not interrupt the flow of your program. Instead, it allows you to check the passage of time at any point in your program. This is particularly useful in scenarios requiring simultaneous tasks or tasks at varying intervals. For instance, if you're operating an LED light while gathering sensor data at different intervals, millis() allows you to do both independently.
Blinking Light Example:
#define LED_PIN LED_BUILTIN
#define BLINK_INTERVAL 1000 // Blink every 1000 ms (1 second)
unsigned long previousMillis = 0;
bool ledState = LOW;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= BLINK_INTERVAL) {
previousMillis = currentMillis; // Remember the time
ledState = !ledState; // Toggle the LED state
digitalWrite(LED_PIN, ledState);
}
}While it may require a bit more complexity in the code to store timestamps and check time differences, the benefits of non-blocking multitasking outweigh this additional complexity.
It's sibling, delay(), is used to pause the execution of the current program for a specified number of milliseconds. Unlike millis(), delay() is a blocking function, meaning it stops all other operations on the board for the duration specified. For example, if you are blinking an LED and use delay(1000), the LED will turn on, the program will pause for one second, and then the LED will turn off. While delay() is a simpler and more straightforward function to use, it is best suited to simpler tasks that do not require multitasking.
Blinking Light Example:
#define LED_PIN LED_BUILTIN
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH); // Turn the LED on
delay(1000); // Wait for a second
digitalWrite(LED_PIN, LOW); // Turn the LED off
delay(1000); // Wait for a second
}
If the task is time-sensitive or requires simultaneous operations, delay() might not be the best option due to its blocking nature.
I hope this clears up the "why" of each! Please ask any questions
In arduino UNO
extern volatile unsigned long timer0_millis;
noInterrupts ();
timer0_millis = 0;
interrupts ();
But it makes no sense at all.
Simply remember the previous millis and calculate time passed (by substracting).
This should work
//#include "SevSeg.h"
int button1 = 11;
int button2 = 12;
int button3 = 13;
int value = 10;
int timer = 0;
bool n = true;
//Added
int currentTime = 0;
long int t0 = 0;
long int t1 = 0;
//SevSeg Display;
void setup() {
Serial.begin(9600);
byte numDigits = 2;
byte digitPins[] = {9,8};
byte segmentPins[] = {10,2, 3, 4, 5, 6, 7,1};
bool resistorsOnSegments = true;
bool updateWithDelays = true;
//byte hardwareConfig = COMMON_ANODE;
//Display.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
//Display.setBrightness(100);
}
void loop() {
//Display.setNumber(value, 1);
// Display.refreshDisplay();
if (digitalRead(11)==HIGH){
Start(value);
}
}
void Start(int value){
t0 = millis();
while(n){
t1 = millis();
if(t1 > (1000+t0)){
//has passed 1 second
currentTime++;
if(value-currentTime == 0){
n = false;
}
}
unsigned long timerGlobal = t1-t0;
//Display.setNumber(value-timerGlobal/1000, 1);
//Display.refreshDisplay();
}
}