Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit arduino help --- May 26, 2019
- Version 1.0
- ???
- Can somebody help me do this.
- when the pushbutton has been pressed it will start the motor
- then when the pir sensor senses something the buzzer and led light up and the motor stops
- and it will monitor again when the pushbutton is pressed.
- */
- int program_state = 0; // This is an example of a "state flag" variable.
- // zero = waiting for the first button press. Change to 1 when that happens.
- // 1 = start the motor and then change program_state to 2
- // 2 = check PIR sensor. When a suitable input level is detected, change program_state to 3.
- // 3 = turn motor off and light LED and sound buzzer for some period (?) of time. Then change program_state to 4.
- // 4 = waiting for second button press, to change program_state back to zero.
- // you still need other variables too, I just didn't repeat them here.
- void setup() {
- Serial.begin(9600);
- // -more stuff goes here that I just didn't write for you-
- }
- void loop() {
- switch (program_state) {
- case 0:
- check_program_state_0();
- break;
- case 1:
- check_program_state_1();
- break;
- case 2:
- check_program_state_2();
- break;
- case 3:
- check_program_state_3();
- break;
- case 4:
- check_program_state_4();
- break;
- }
- } // end of main loop
- void check_program_state_0() {
- // This function is only for checking if the button is pressed.
- // When the button is pressed, this function needs to change program_state to 1.
- // When the button is pressed, also print a serial message: Serial.println("Button first press: program_state = 1");
- // You can put a short delay(100) here to de-bounce the button input.
- }
- void check_program_state_1() {
- // This function needs to:
- // --- start the motor
- // --- print a serial message: Serial.println("Motor on: program_state = 2");
- // --- change program_state to 2.
- }
- void check_program_state_2() {
- // This function needs to check the PIR detector for input.
- // When a high enough PIR input is found, this needs to change program_state to 3.
- // Also when that happens, print a serial message: Serial.println("PIR input detected: program_state = 3");
- // You can also put a short delay(100) here if re-reading the PIR sensor quickly causes any issues.
- }
- void check_program_state_3() {
- // This function does these things:
- // --- turn the motor off
- // --- turn the buzzer on
- // --- turn the LED on
- // --- run a short delay(1000) while the buzzer is on
- // --- turn the buzzer off
- // --- turn the LED off
- // --- then change program_state to 4.
- // You can also put a serial message in here if you want: Serial.println("PIR input detected: program_state = 4");
- }
- void check_program_state_4() {
- // This function is only for checking if the button is pressed.
- // When the button is pressed, this function needs to change program_state to zero.
- // Also show a serial message: Serial.println("Button reset press: program_state = 0");
- // You can also put a short delay(100) here to de-bounce the button.
- }
- // -the end-
Add Comment
Please, Sign In to add comment