MrAlvin

state machine example A

Mar 20th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.46 KB | None | 0 0
  1. /*
  2.  *  This is a sample sketch to show a solution to Simons 4x LED challenge
  3.  *  see: https://www.facebook.com/groups/1437500423193520/permalink/1705520479724845/
  4.  *  
  5.  *  The challenge is that any one of three events can occur
  6.  *    Event A : the user can press a button
  7.  *    Event B : time has passed
  8.  *    Event C : the user releases the button
  9.  *    
  10.  *    Event A & C can only happen sequentally. That is one event has to happen before the other can happen
  11.  *    Ecent B can only happen after event A
  12.  *    Event C can happen at any time after event A has happened
  13.  *    
  14.  *
  15.  * Test circuit:
  16.  *
  17.  *  Connect a push-button to pin 10 (ButtonPin), connect other side of button to ground.
  18.  *  Connect LEDs (including resistors) to pins 3, 4, 5 & 6
  19.  *  
  20.  *  Pin 13 (StatusPin) is used as a I-am-alive-blinking-signal
  21.  *  
  22.  *  This skect uses the ButtoOne library from https://github.com/MrAlvin/ButtonOne
  23.  *
  24.  */
  25.  
  26. #include "ButtonOne.h"
  27.  
  28. // Setup a ButtonOne instance on pin 10.  
  29. ButtonOne button(10);
  30.  
  31. #define LED1_PIN 3
  32. #define LED2_PIN 4
  33. #define LED3_PIN 5
  34. #define LED4_PIN 6
  35. #define DEBUG_LED_PIN 13
  36.  
  37. boolean button_pressed = false;    // flag used to evaluate on event B - the passing of time between activation of LEDs
  38. unsigned long previousMillis = 0;  // used to track the passing of "interval" time
  39. unsigned long interval = 0;        // used to track time to occur between LED activations. Default action is; that LED 1 turns on, as the button is pressed
  40. int idx = 1;                       // used keep track of the current state in the state-machine
  41.  
  42. //*********************************************
  43. // setup function - to run once:
  44. //*********************************************
  45. void setup() {
  46.   // enable the standard led on pin 13.
  47.   pinMode(DEBUG_LED_PIN, OUTPUT);      // sets the digital pin as output
  48.  
  49.   pinMode(LED1_PIN, OUTPUT);      // sets the digital pin as output
  50.   pinMode(LED2_PIN, OUTPUT);      // sets the digital pin as output
  51.   pinMode(LED3_PIN, OUTPUT);      // sets the digital pin as output
  52.   pinMode(LED4_PIN, OUTPUT);      // sets the digital pin as output
  53.  
  54.   //initiate internal button management values
  55.   button.begin();
  56.  
  57.   // link the Press function to be called on a button Press event.  
  58.   button.attachPress(btnPress);
  59.   // link the Release function to be called on a button Release event.
  60.   button.attachRelease(btnRelease);
  61. } // setup
  62.  
  63.  
  64. //*********************************************
  65. // main code -  to run repeatedly:
  66. //*********************************************
  67. void loop() {
  68.   // check for event A (button press)
  69.   button.check();
  70.  
  71.   // if event A has occured, check for event B
  72.   if(button_pressed)  is_it_time();
  73.  
  74.   // something I usually use when dealing with event-style programming
  75.   debug_blink();  // lets me know that the program is up and running
  76. } // loop
  77.  
  78.  
  79. //*********************************************
  80. // button events:
  81. //*********************************************
  82. // this function will be called when the button is pressed
  83. void btnPress() {
  84.   button_pressed = true;  // raise flag. Response to flag happens in the main loop()
  85. } // btnPress
  86.  
  87. // this function will be called when the button is released - which is also event C
  88. void btnRelease() {
  89.   button_pressed = false; // button is no longer pressed
  90.   LedsOff();              // turn off all LEDs
  91.   idx = 1;                // reset the pointer in the state machine, so ready for next press of the button
  92.   previousMillis = 0;     // reset the time tracker
  93.   interval = 0;           // set time interval for time to pass between pressing of button to turn on LED 1
  94. } // btnRelease
  95.  
  96.  
  97. //*********************************************
  98. // timer function:
  99. //*********************************************
  100. void is_it_time() {
  101.  
  102.   if (millis() - previousMillis > interval) {
  103.     previousMillis = millis();  // store the current time, so we can waite "interval" amount of time, before we take next action
  104.    
  105.     // do you timed event
  106.     timed_action();
  107.   }
  108. } //is_it_time
  109.  
  110.  
  111. //*********************************************
  112. // state machine:
  113. //*********************************************
  114. void timed_action() {
  115.   switch(idx){
  116.     case 1: //turn on one led
  117.       led_1_on();
  118.       interval = 1000; // time until LED 2 will be turned on
  119.       idx++;           // ready to activate LED 2
  120.       break;
  121.     case 2: //turn on two leds
  122.       led_2_on();
  123.       interval = 1000; // time until LED 3 will be turned on
  124.       idx++;           // ready to activate LED 3
  125.       break;
  126.     case 3: //turn on three leds
  127.       led_3_on();
  128.       interval = 1000; // time until LED 4 will be turned on
  129.       idx++;           // ready to activate LED 4
  130.       break;
  131.     case 4: //turn on four leds
  132.       led_4_on();
  133.       idx++;           // ready to simply waite
  134.       break;
  135.     case 5: //no changes
  136.       break;
  137.   }
  138. } //timed_action
  139.  
  140. //*********************************************
  141. // LED functions:
  142. //*********************************************
  143. void LedsOff(){
  144.   led_1_off();
  145.   led_2_off();
  146.   led_3_off();
  147.   led_4_off();
  148. }
  149.  
  150.  
  151. void led_1_on(){
  152.   digitalWrite(LED1_PIN, HIGH);
  153. }
  154.  
  155. void led_1_off(){
  156.   digitalWrite(LED1_PIN, LOW);
  157. }
  158.  
  159.  
  160. void led_2_on(){
  161.   digitalWrite(LED1_PIN, HIGH);
  162. }
  163.  
  164. void led_2_off(){
  165.   digitalWrite(LED1_PIN, LOW);
  166. }
  167.  
  168.  
  169. void led_3_on(){
  170.   digitalWrite(LED1_PIN, HIGH);
  171. }
  172.  
  173. void led_3_off(){
  174.   digitalWrite(LED1_PIN, LOW);
  175. }
  176.  
  177. void led_4_on(){
  178.   digitalWrite(LED1_PIN, HIGH);
  179. }
  180.  
  181. void led_4_off(){
  182.   digitalWrite(LED1_PIN, LOW);
  183. }
  184.  
  185.  
  186. //*********************************************
  187. // I-am-alive debug LED blink function:
  188. //*********************************************
  189. #define ON_DELAY  500
  190. #define OFF_DELAY 500
  191.  
  192. void debug_blink() {
  193.   static unsigned long blink_millis = 0;
  194.   static unsigned long blink_interval = 0;
  195.   static boolean do_on = true;
  196.  
  197.   if ( millis() - blink_millis >  blink_interval )  { // if its time to change the blink
  198.     if (do_on) { //use a flag to determine wether to turn on or off the Blink LED
  199.       digitalWrite(DEBUG_LED_PIN, HIGH);   // set the LED on, if okay to use power for it
  200.       blink_millis = millis();
  201.       blink_interval = ON_DELAY; // wait for a second
  202.       do_on = false;
  203.     }else{
  204.       digitalWrite(DEBUG_LED_PIN, LOW);    // set the LED off
  205.       // set the time to do next blink
  206.       blink_millis = millis();
  207.       blink_interval = OFF_DELAY;  // wait for a second
  208.       do_on = true;
  209.     }
  210.   }
  211. } //debug_blink
  212.  
  213. // End
Advertisement
Add Comment
Please, Sign In to add comment