Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit - 4 second motor - v1.0
- 19 November 2019
- */
- int led1 = 2;
- int led2 = 3;
- int led3 = 4;
- int motorPin = 9;
- int motor_state = 0;
- long motor_run_time = 4000; // This is the time in milliseconds that you want the motor to run for.
- unsigned long motor_run_begin_time = 0;
- unsigned long motor_run_current_time = 0;
- void setup() {
- Serial.begin(9600);
- pinMode(LED_BUILTIN, OUTPUT);
- pinMode(motorPin, OUTPUT);
- delay(5000);
- digitalWrite(led1, HIGH);
- digitalWrite(motorPin, HIGH);
- motor_run_begin_time = millis();
- Serial.println("Motor timer starting.");
- Serial.println("Exiting setup()");
- }
- void loop() {
- switch (motor_state) {
- case 0:
- check_motor_timer();
- break;
- case 1:
- turn_motor_off();
- break;
- case 2:
- // Nothing is in here!
- // If you wanted something to happen after the motor shut off,
- // you could put the trigger for that in here.
- break;
- }
- }
- void check_motor_timer() {
- motor_run_current_time = millis();
- if (motor_run_current_time >= (motor_run_begin_time + motor_run_time)) {
- motor_state = 1;
- }
- }
- void turn_motor_off() {
- Serial.println("Motor shutting off.");
- digitalWrite(motorPin, LOW);
- motor_state = 2;
- }
- // ~~~~~~~~~ end ~~~~~~~~~~
Add Comment
Please, Sign In to add comment