skizziks_53

Reddit arduino stepper led blinker v1.0

May 26th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. /*
  2.    Reddit arduino help --- May 26, 2019
  3.   Version 1.0
  4.  
  5.   ???
  6.  
  7.   Blink a LED and run a stepper motor manually at the same time, but at different blink rates.
  8.  
  9. */
  10.  
  11. // I dunno what pin 8 is for???
  12.  
  13. // int stepper_motor_1_enable_pin = x; // normally you would have three pins for each stepper motor...
  14. // int stepper_motor_1_direction_pin = y;
  15. int stepper_motor_1_step_pin = 9;
  16.  
  17. // Below is timing code variables for stepper_motor_1
  18. bool stepper_motor_1_enabled = true; // This lets you turn the motor on or off some other way in code.
  19. int stepper_motor_1_state = 0;
  20. int stepper_motor__step_interval = 50; // This is the milliseconds time for the stepper motor pin to blink [on] or [off]. It uses the same time for on and off.
  21. unsigned long stepper_motor_1_begin_time = 0;
  22. unsigned long stepper_motor_1_current_time = 0;
  23.  
  24.  
  25. bool led_blinker_enabled = true; // This lets you turn the blinking led on or off some other way in code.
  26. int led_blinker_state = 0;
  27. int led_blinker__blink_on_time = 50; // This is the time that the LED will blink on, in milliseconds.
  28. int led_blinker__blink_off_time = 950; // This is the time that the LED will blink off, in milliseconds.
  29. int led_blinker__time_in_use = 0;
  30. unsigned long led_blinker_begin_time = 0;
  31. unsigned long led_blinker_current_time = 0;
  32.  
  33.  
  34.  
  35. void setup() {
  36.  
  37.   pinMode(LED_BUILTIN, OUTPUT);
  38.   pinMode(8, OUTPUT);
  39.   pinMode(stepper_motor_1_step_pin, OUTPUT);
  40.   digitalWrite(8, LOW);
  41.   digitalWrite(stepper_motor_1_step_pin, LOW);
  42.  
  43. }
  44.  
  45.  
  46.  
  47. void loop() {
  48.  
  49.   if (led_blinker_enabled == true) {
  50.     check_led_blinker();
  51.   }
  52.  
  53.   if (stepper_motor_1_enabled == true) {
  54.     check_stepper_motor_1();
  55.   }
  56.  
  57. } // end of main loop()
  58.  
  59.  
  60.  
  61.  
  62. void check_led_blinker() {
  63.   led_blinker_current_time = millis();
  64.   if (led_blinker_current_time >= led_blinker_begin_time) {
  65.     if (led_blinker_state == 0) { // if the LED is currently turned off:
  66.       led_blinker__time_in_use = led_blinker__blink_off_time;
  67.     }
  68.     else { // if the LED is currently turned on:
  69.       led_blinker__time_in_use = led_blinker__blink_on_time;
  70.     }
  71.     if (led_blinker_current_time >= (led_blinker_begin_time + led_blinker__time_in_use)) {
  72.       if (led_blinker_state == 0) {
  73.         led_blinker_state = 1;
  74.       }
  75.       else {
  76.         led_blinker_state = 0;
  77.       }
  78.       digitalWrite(LED_BUILTIN, led_blinker_state);
  79.       led_blinker_begin_time = millis();
  80.     }
  81.   }
  82.   else {
  83.     led_blinker_begin_time = millis();
  84.   }
  85. }
  86.  
  87.  
  88.  
  89. void check_stepper_motor_1() {
  90.   stepper_motor_1_current_time = millis();
  91.   if (stepper_motor_1_current_time >= stepper_motor_1_begin_time) {
  92.     if (stepper_motor_1_current_time >= (stepper_motor_1_begin_time + stepper_motor__step_interval)) {
  93.       if (stepper_motor_1_state == 0) {
  94.         stepper_motor_1_state = 1;
  95.       }
  96.       else {
  97.         stepper_motor_1_state = 0;
  98.       }
  99.       digitalWrite(stepper_motor_1_step_pin, stepper_motor_1_state);
  100.       stepper_motor_1_begin_time = millis();
  101.     }
  102.   }
  103.   else {
  104.     stepper_motor_1_begin_time = millis();
  105.   }
  106. }
  107.  
  108.  
  109.  
  110. // -the end-
Add Comment
Please, Sign In to add comment