ronbentley1
Published

ez_timers - unlimited timers

Define and operate any number of concurrent timers in your sketches, each independent and all non-blocking.

BeginnerFull instructions provided277
ez_timers - unlimited timers

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
Any Arduino microcontroller should be okay.
×1
5 mm LED: Red
5 mm LED: Red
8 x LEDs (any colour) if running the sketch example
×8
Resistor 220 ohm
Resistor 220 ohm
8 x 220 ohm resistors if running the sketch example
×8
Solderless Breadboard Half Size
Solderless Breadboard Half Size
1 x breadboard (small is okay, but any you have lying around) if running the sketch example
×1
Jumper wires (generic)
Jumper wires (generic)
jumper wires if running the sketch example
×9

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

LED circuit diagram for ez_timers sketch example

This circuit reflects the example use of 8 x timers running concurrently.

Code

ez_timers technique

C/C++
A sketch that demonstrates the use of multilple, independent and concurrent timers that are non-blocking.
//
//   ez_timers - multiple, concurrent, independent non-blocking timers
//
//   Ron D Bentley, Stafford, UK
//   August 2021
//
//   This example and code is in the public domain and
//   may be used without restriction and without warranty.
//

// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Timer definitions, declarations and functions...
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#define max_timers           8

#define elapsed            true
#define not_elapsed        !elapsed
#define active             true
#define not_active         !active    // double defined for user preference
#define inactive           not_active // double defined for user preference

uint8_t timer; // general variable for use dealing with timers

// timer control struct(ure)
struct timer_control {
  bool     timer_status;    // records status of a timer - active or not active
  uint32_t start_time;      // records the millis time when a timer is started
} timers[max_timers];       // declare an entry for each timer - 0 to (max_timers-1)

//
// set the given timer active and note start time in milliseconds
//
void start_timer(uint8_t timer) {
  if (timer < max_timers) {
    // valid timer
    timers[timer].timer_status = active;   // mark this timer as active
    timers[timer].start_time   = millis(); // record time this timer is started
  }
}

//
// cancel (stop) the given timer
//
void stop_timer(uint8_t timer) {
  if (timer < max_timers) {
    // valid timer
    timers[timer].timer_status = inactive;  // mark this timer as inactive
  }
}

//
// Function determines if the time has elapsed for the given timer, if active.
//
bool timer_elapsed(uint8_t timer, uint32_t elapsed_time) {
  if (timer < max_timers) {
    // valid timer
    if (timers[timer].timer_status == active) {
      // timer is active so check elapsed time
      if (millis() - timers[timer].start_time >= elapsed_time) {
        // this timer has elapsed
        timers[timer].timer_status = inactive; // mark this timer no longer active
        return elapsed;
      }
    }
  }
  return not_elapsed;
}

void setup() {
  // initialise timer control structure for each defined time entry
  for (timer = 0; timer < max_timers; timer++) {
    timers[timer].timer_status = inactive;    // mark this timer inactive
    timers[timer].start_time   = 0;           // clear timer start time
  }
  // add any other setup requirements here...

}

void loop() {
  // example of use of timers - one led assigned to each timer - 8 off
  // we use a structure to gather together all the data we need to manage
  // the leds. Each led structure entry is referenced by the timer number
  // associated with each led, eg timer = 0 associated with led entry 0, etc.
  struct led_control {
    uint8_t led_pin;    // digital pin assigned to the led
    uint32_t led_time;  // the time to elapse between led state transitions (millisecs)
    bool led_status;    // the current state (LOW/HIGH) of the led
  } leds[max_timers] = {// preset data for each timer's led
    2, 100, HIGH,
    3, 200, HIGH,
    4, 300, HIGH,
    5, 400, HIGH,
    6, 500, HIGH,
    7, 600, HIGH,
    8, 700, HIGH,
    9, 800, HIGH
  };
  // set up timer leds and their associated timers
  for (timer = 0; timer < max_timers; timer++) {
    pinMode(leds[timer].led_pin, OUTPUT);
    digitalWrite(leds[timer].led_pin, LOW);//ensure each timer led is off
    start_timer(timer);
  }
  // now keep track of each timer and deal with associated leds when times are elapsed
  do {
    for (timer = 0; timer < max_timers; timer++) {
      if (timer_elapsed(timer, leds[timer].led_time) == elapsed) {
        // this timer has elapsed for this timer's led so change led state
        leds[timer].led_status = !leds[timer].led_status;          // invert this timer's led status
        digitalWrite(leds[timer].led_pin, leds[timer].led_status); // update physical led
        start_timer(timer);// restart this led's timer
      }
    }
  } while (true); // keep looping the timers
}

Credits

ronbentley1
25 projects • 13 followers

Comments