Advertisement
skizziks_53

Persistent State Example 1.0

Aug 31st, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. /*
  2.   31 August 2019
  3.   Example Arduino Sketch - persistent operations v1.0
  4.   (operations that continue for more than 1 pass through the main loop)
  5.  
  6.   This sketch doesn't really do anything as it is.
  7.   It is just a rough example of how to have a LED blink while waiting for control messages from the serial port.
  8.  
  9. */
  10.  
  11. bool blink_the_led = false;
  12. int blink_led_interval = 500; // A time in milliseconds to blink the LED on and off (500 is a half-second).
  13. unsigned long led_blink_previous_time = 0; // used for blinking the LED
  14. unsigned long led_blink_current_time = 0; // used for blinking the LED
  15. int LED_pin = 13; // The pin that the LED is on.
  16. int LED_pin_value = 0; // The value to write to the pin.
  17.  
  18.  
  19. void setup() {
  20.   Serial.begin(9600);
  21.  
  22.   // (various things not included here)
  23.  
  24.   pinMode(LED_pin, OUTPUT);
  25.   digitalWrite(LED_pin, LOW); // I have seen that some clone Unos will put pin# 13 high on start-up, unless you write it low.
  26.   Serial.println("Exiting setup()");
  27. }
  28.  
  29. void loop() {
  30.  
  31.   // In this sketch, the LED can blink independently of whatever the serial-message-reading function does,
  32.   // because the function to blink the LED is not inside the serial-message-reading function.
  33.   // The function check_serial_buffer() changes the value of the variable blink_the_led,
  34.   // and then check_to_blink_led() acts based on the value of blink_the_led.
  35.   check_serial_buffer();
  36.   check_to_blink_led();
  37.  
  38. }
  39.  
  40.  
  41. void check_serial_buffer() {
  42.   // This is the function that checks the serial buffer. (This part isn't written really)
  43.  
  44.   // ...If the value of the serial buffer is the "on" value, then do the following things:
  45.   //            change blink_the_led to true
  46.   //            change LED_pin_value to 1
  47.   //            digitalWrite(LED_pin, LED_pin_value); // This turns the LED on immediately.
  48.   //            led_blink_previous_time = millis(); // This starts the timer off properly.
  49.  
  50.   // ...If the value of the serial buffer is the "off" value, then change blink_the_led to false
  51.  
  52. }
  53.  
  54.  
  55. void check_to_blink_led() {
  56.   if (blink_the_led == true) {
  57.     check_blink_timer();
  58.   }
  59.   else {
  60.     // This part is here to ensure that the LED always gets turned off.
  61.     // If blink_the_led was changed to FALSE while the LED was still on, then the LED timer code would never get checked to turn it off.
  62.     LED_pin_value = 0;
  63.     digitalWrite(LED_pin, LED_pin_value);
  64.     // There is different ways you could do this, but the above two lines immediately turn off the LED.
  65.   }
  66. }
  67.  
  68. void check_blink_timer() {
  69.   led_blink_current_time = millis();
  70.   if (led_blink_current_time >= led_blink_previous_time) {
  71.     if (led_blink_current_time >= (led_blink_previous_time + blink_led_interval)) {
  72.       if (LED_pin_value == 0) {
  73.         LED_pin_value = 1;
  74.       }
  75.       else {
  76.         LED_pin_value = 0;
  77.       }
  78.       digitalWrite(LED_pin, LED_pin_value);
  79.       led_blink_previous_time = millis();
  80.     }
  81.   }
  82.   else { // if (led_blink_current_time < led_blink_previous_time)
  83.     // If led_blink_current_time was ever less than led_blink_previous_time, then that would mean that the millis() timer had rolled over.
  84.     // Since the blinker code won't work under that condition, the solution I often use is just re-set the previous time when that happens.
  85.     // This means you will get one timer cycle that will be up to 2X as long as normal, but that is a minor issue and everything works normal after that (until the next timer rollover).
  86.     led_blink_previous_time = millis();
  87.     // There are different ways of dealing with clock rollover in code, depending on what the code is doing and how much precision you want to have.
  88.     // For CNC machine operations that are time-critical, the usual solution is (before every task) you compute how long the task will take and then check that there is enough time remaining before the timer rollover to do it.
  89.     // If there isn't enough time, then the microcontroller just waits until the clock rolls over to begin that task.
  90.   }
  91. }
  92.  
  93.  
  94.  
  95. // ~~~~~~~~ end ~~~~~~~~~
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement