skizziks_53

Reddit 4-second motor

Nov 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. /*
  2.   Reddit - 4 second motor - v1.0
  3.   19 November 2019
  4.  
  5. */
  6.  
  7. int led1 = 2;
  8. int led2 = 3;
  9. int led3 = 4;
  10. int motorPin = 9;
  11.  
  12. int motor_state = 0;
  13. long motor_run_time = 4000; // This is the time in milliseconds that you want the motor to run for.
  14. unsigned long motor_run_begin_time = 0;
  15. unsigned long motor_run_current_time = 0;
  16.  
  17.  
  18. void setup() {
  19.   Serial.begin(9600);
  20.   pinMode(LED_BUILTIN, OUTPUT);
  21.   pinMode(motorPin, OUTPUT);
  22.   delay(5000);
  23.   digitalWrite(led1, HIGH);
  24.   digitalWrite(motorPin, HIGH);
  25.   motor_run_begin_time = millis();
  26.   Serial.println("Motor timer starting.");
  27.   Serial.println("Exiting setup()");
  28. }
  29.  
  30.  
  31. void loop() {
  32.   switch (motor_state) {
  33.     case 0:
  34.       check_motor_timer();
  35.       break;
  36.     case 1:
  37.       turn_motor_off();
  38.       break;
  39.     case 2:
  40.       // Nothing is in here!
  41.       // If you wanted something to happen after the motor shut off,
  42.       //   you could put the trigger for that in here.
  43.       break;
  44.   }
  45. }
  46.  
  47.  
  48. void check_motor_timer() {
  49.   motor_run_current_time = millis();
  50.   if (motor_run_current_time >= (motor_run_begin_time + motor_run_time)) {
  51.     motor_state = 1;
  52.   }
  53. }
  54.  
  55.  
  56. void turn_motor_off() {
  57.   Serial.println("Motor shutting off.");
  58.   digitalWrite(motorPin, LOW);
  59.   motor_state = 2;
  60. }
  61.  
  62.  
  63. // ~~~~~~~~~ end ~~~~~~~~~~
Add Comment
Please, Sign In to add comment