Guest User

Untitled

a guest
Jan 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. // millis and a finite state machine
  2. //
  3. // The "State Change Detection" example is used.
  4. // The "Blink without Delay" example is used.
  5. // Variables of the "bool" type are used to pass on information.
  6. // A single "previousMillis" could be used, they do not overlap.
  7. //
  8. // A push button is connected between pin 2 and GND.
  9.  
  10.  
  11. // An 'enum' is the best way to for a list of the states.
  12. enum
  13. {
  14. STATE_IDLE_INIT,
  15. STATE_IDLE,
  16. STATE_ON_INIT,
  17. STATE_ON_BEFORE_BUTTON,
  18. STATE_ON_BUTTON_2_SECONDS,
  19. STATE_OFF_INIT,
  20. STATE_OFF,
  21. STATE_BLINK_INIT,
  22. STATE_BLINK,
  23. } state;
  24.  
  25. const int buttonPin = 2;
  26. int last_button_value = HIGH; // pin 2 is HIGH when button is not pressed
  27. unsigned long previousMillisButton;
  28.  
  29. const int ledPin = 13;
  30. int led_value;
  31.  
  32. unsigned long previousMillisBlinking;
  33. bool blinking; // boolean variable, should the led blink or not.
  34.  
  35.  
  36. void setup()
  37. {
  38. Serial.begin( 9600);
  39. Serial.println( "The sketch has started.");
  40. pinMode( buttonPin, INPUT_PULLUP);
  41. pinMode( ledPin, OUTPUT);
  42. }
  43.  
  44.  
  45. void loop()
  46. {
  47. unsigned long currentMillis = millis();
  48.  
  49.  
  50. // ---------------------------------------------------
  51. // BUTTON
  52. // ---------------------------------------------------
  53. // Detect if the button changes, and pass
  54. // that information on, to the finite state machine.
  55. // Only the event of the change is passed on, because
  56. // this sketch did not need to know the state
  57. // of the button, only a change.
  58. // ---------------------------------------------------
  59. bool buttonPressed = false;
  60. bool buttonReleased = false;
  61.  
  62. int button_value = digitalRead( buttonPin);
  63.  
  64. if( button_value != last_button_value)
  65. {
  66. if( button_value == LOW) // low is button pressed
  67. {
  68. buttonPressed = true;
  69. }
  70. else
  71. {
  72. buttonReleased = true;
  73. }
  74. last_button_value = button_value;
  75. }
  76.  
  77.  
  78. // ---------------------------------------------------
  79. // FINITE STATE MACHINE
  80. // ---------------------------------------------------
  81. // The final state machine uses information to
  82. // make decisions.
  83. // ---------------------------------------------------
  84.  
  85. switch( state)
  86. {
  87. case STATE_IDLE_INIT:
  88. Serial.println( "Press the button to turn the led on.");
  89. state = STATE_IDLE;
  90. break;
  91. case STATE_IDLE:
  92. if( buttonPressed)
  93. {
  94. state = STATE_ON_INIT;
  95. }
  96. break;
  97. case STATE_ON_INIT:
  98. digitalWrite( ledPin, HIGH);
  99. Serial.println( "Press button longer than two seconds to turn it off.");
  100. state = STATE_ON_BEFORE_BUTTON;
  101. break;
  102. case STATE_ON_BEFORE_BUTTON:
  103. if( buttonPressed)
  104. {
  105. previousMillisButton = currentMillis;
  106. state = STATE_ON_BUTTON_2_SECONDS;
  107. }
  108. break;
  109. case STATE_ON_BUTTON_2_SECONDS:
  110. if( buttonReleased)
  111. {
  112. state = STATE_ON_BEFORE_BUTTON;
  113. }
  114. else
  115. {
  116. if( currentMillis - previousMillisButton >= 2000)
  117. {
  118. state = STATE_OFF_INIT;
  119. }
  120. }
  121. break;
  122. case STATE_OFF_INIT:
  123. digitalWrite( ledPin, LOW);
  124. Serial.println( "Press button to blink the led.");
  125. state = STATE_OFF;
  126. break;
  127. case STATE_OFF:
  128. if( buttonPressed)
  129. {
  130. state = STATE_BLINK_INIT;
  131. }
  132. break;
  133. case STATE_BLINK_INIT:
  134. previousMillisBlinking = currentMillis;
  135. blinking = true;
  136. Serial.println( "Press button to stop blinking.");
  137. state = STATE_BLINK;
  138. break;
  139. case STATE_BLINK:
  140. if( buttonPressed)
  141. {
  142. blinking = false;
  143. digitalWrite( ledPin, LOW); // be sure to turn led off
  144. state = STATE_IDLE_INIT;
  145. }
  146. break;
  147. }
  148.  
  149.  
  150. // ---------------------------------------------------
  151. // BLINK WITHOUT DELAY
  152. // ---------------------------------------------------
  153. // Blinking the led is outside the final state machine.
  154. // The final state machine turns it on and off.
  155. // ---------------------------------------------------
  156.  
  157. if( blinking)
  158. {
  159. if( currentMillis - previousMillisBlinking >= 300)
  160. {
  161. previousMillisBlinking = currentMillis;
  162.  
  163. if( led_value == LOW)
  164. {
  165. led_value = HIGH;
  166. }
  167. else
  168. {
  169. led_value = LOW;
  170. }
  171. digitalWrite( ledPin, led_value);
  172. }
  173. }
  174.  
  175. delay( 10); // a delay as a simple way to debounce the button
  176. }
Add Comment
Please, Sign In to add comment