skizziks_53

Reddit Arduino state flag example v1.0

May 26th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. /*
  2.    Reddit arduino help --- May 26, 2019
  3.   Version 1.0
  4.  
  5.   ???
  6.  
  7.   Can somebody help me do this.
  8.  
  9.   when the pushbutton has been pressed it will start the motor
  10.   then when the pir sensor senses something the buzzer and led light up and the motor stops
  11.   and it will monitor again when the pushbutton is pressed.
  12.  
  13. */
  14.  
  15. int program_state = 0; // This is an example of a "state flag" variable.
  16. // zero = waiting for the first button press. Change to 1 when that happens.
  17. // 1 = start the motor and then change program_state to 2
  18. // 2 = check PIR sensor. When a suitable input level is detected, change program_state to 3.
  19. // 3 = turn motor off and light LED and sound buzzer for some period (?) of time. Then change program_state to 4.
  20. // 4 = waiting for second button press, to change program_state back to zero.
  21.  
  22.  
  23. // you still need other variables too, I just didn't repeat them here.
  24.  
  25.  
  26. void setup() {
  27.   Serial.begin(9600);
  28.   // -more stuff goes here that I just didn't write for you-
  29.  
  30. }
  31.  
  32.  
  33. void loop() {
  34.   switch (program_state) {
  35.     case 0:
  36.       check_program_state_0();
  37.       break;
  38.     case 1:
  39.       check_program_state_1();
  40.       break;
  41.     case 2:
  42.       check_program_state_2();
  43.       break;
  44.     case 3:
  45.       check_program_state_3();
  46.       break;
  47.     case 4:
  48.       check_program_state_4();
  49.       break;
  50.   }
  51. } // end of main loop
  52.  
  53.  
  54. void check_program_state_0() {
  55.   // This function is only for checking if the button is pressed.
  56.   // When the button is pressed, this function needs to change program_state to 1.
  57.   // When the button is pressed, also print a serial message: Serial.println("Button first press: program_state = 1");
  58.   // You can put a short delay(100) here to de-bounce the button input.
  59. }
  60.  
  61. void check_program_state_1() {
  62.   // This function needs to:
  63.   // --- start the motor
  64.   // --- print a serial message: Serial.println("Motor on: program_state = 2");
  65.   // --- change program_state to 2.
  66. }
  67.  
  68. void check_program_state_2() {
  69.   // This function needs to check the PIR detector for input.
  70.   // When a high enough PIR input is found, this needs to change program_state to 3.
  71.   // Also when that happens, print a serial message: Serial.println("PIR input detected: program_state = 3");
  72.   // You can also put a short delay(100) here if re-reading the PIR sensor quickly causes any issues.
  73. }
  74.  
  75. void check_program_state_3() {
  76.   // This function does these things:
  77.   // --- turn the motor off
  78.   // --- turn the buzzer on
  79.   // --- turn the LED on
  80.   // --- run a short delay(1000) while the buzzer is on
  81.   // --- turn the buzzer off
  82.   // --- turn the LED off
  83.   // --- then change program_state to 4.
  84.   // You can also put a serial message in here if you want: Serial.println("PIR input detected: program_state = 4");
  85. }
  86.  
  87. void check_program_state_4() {
  88.   // This function is only for checking if the button is pressed.
  89.   // When the button is pressed, this function needs to change program_state to zero.
  90.   // Also show a serial message: Serial.println("Button reset press: program_state = 0");
  91.   // You can also put a short delay(100) here to de-bounce the button.
  92. }
  93.  
  94.  
  95.  
  96.  
  97. // -the end-
Add Comment
Please, Sign In to add comment