arduino-timer-programming

Migrate from https://cmakerhk.wordpress.com/2018/10/19/timer-programming-on-arduino/


ul millis() Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
ul micros() Returns the number of microseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 70 minutes. On 16 MHz Arduino boards (e.g. Duemilanove and Nano), this function has a resolution of four microseconds (i.e. the value returned is always a multiple of four). On 8 MHz Arduino boards (e.g. the LilyPad), this function has a resolution of eight microseconds
delay(ul ms) Pauses the program for the amount of time (in milliseconds) specified as parameter
delayMicroseconds(ui us) Pauses the program for the amount of time (in microseconds) specified as parameter. There are a thousand microseconds in a millisecond, and a million microseconds in a second.

Currently, the largest value that will produce an accurate delay is 16383. This could change in future Arduino releases. For delays longer than a few thousand microseconds, you should use delay() instead

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    ledState = !ledState;

    // set the LED with the ledState of the variable:
    digitalWrite(LED_BUILTIN, ledState);
  }
}

Programing Flow of Cenz

#define led_pin 13

void setup() {
  // put your setup code here, to run once:
  pinMode(led_pin, OUTPUT);
}

void loop() {
  //variable declaration
  unsigned long current_millis = millis();
  static unsigned long led_previous_millis = 0;
  static bool led_toggle_flag = false;


  //=========checkFlag()======
  if(current_millis - led_previous_millis >= 1000){
    //toggle LED function flag on
    led_toggle_flag = true;
  }

  //===========do()===========
  if(led_toggle_flag){
    led_toggle(led_pin);

    //reset the flag and timer
    led_toggle_flag = false;
    led_previous_millis = current_millis;
  }
}

void led_toggle(unsigned char pin){
  digitalWrite(pin, !digitalRead(pin));
}
#define led_pin 13

typedef struct EventRegister{
  unsigned long previous_millis;
  bool check_flag;
  unsigned long duration;
};

EventRegister ledToggle{0, false, 1000};

void setup() {
  // put your setup code here, to run once:
  pinMode(led_pin, OUTPUT);
}

void loop() {
  //variable declaration
  unsigned long current_millis = millis();

  //=========checkFlag()======
  if(current_millis - ledToggle.previous_millis >= ledToggle.duration){
    //toggle LED function flag on
    ledToggle.check_flag = true;
  }

  //===========do()===========
  if(ledToggle.check_flag){
    led_toggle(led_pin);
    //reset the flag and timer
    ledToggle.check_flag = false;
    ledToggle.previous_millis = current_millis;
  }
}

void led_toggle(unsigned char pin){
  digitalWrite(pin, !digitalRead(pin));
}

Ref:
https://www.arduino.cc/reference/en/language/functions/time/millis/
https://www.arduino.cc/reference/en/language/functions/time/micros/
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay