Advertisement
igendel

Arduino non-blocking S.O.S blinker

Jul 25th, 2022 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define DELAY_LIST_SIZE 18U
  2. const uint16_t delayList[DELAY_LIST_SIZE] = {
  3.    100, 150, 100, 150, 100, // S
  4.    500,
  5.    400, 150, 400, 150, 400, // O
  6.    500,
  7.    100, 150, 100, 150, 100, // S
  8.    1500};
  9.  
  10. void timedToggle() {
  11.  
  12.   static uint16_t msLeft = 0;
  13.   static uint8_t currentState = 0;
  14.   static uint8_t delayListIndex = 0;
  15.  
  16.   // Have we waited long enough?
  17.   if (0 == msLeft) {
  18.    
  19.     // Toggle LED state
  20.     currentState = 1 - currentState;
  21.     digitalWrite(13, currentState);
  22.    
  23.     // Get the next wait time
  24.     msLeft = delayList[delayListIndex];
  25.     delayListIndex++;
  26.     // Restart if reached the end of the list
  27.     if (DELAY_LIST_SIZE == delayListIndex) {
  28.       delayListIndex = 0;
  29.     }
  30.    
  31.   } else {
  32.       msLeft--;
  33.     }
  34.  
  35. }
  36.  
  37. void setup() {
  38.   pinMode(13, OUTPUT);
  39. }
  40.  
  41. void loop() {
  42.  
  43.   static uint32_t lastMillis = 0;
  44.  
  45.   // Call the LED toggling handler every 1ms
  46.   if (millis() != lastMillis) {
  47.     lastMillis = millis();
  48.     timedToggle();
  49.   }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement