Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Reddit arduino help --- May 26, 2019
- Version 1.0
- ???
- Blink a LED and run a stepper motor manually at the same time, but at different blink rates.
- */
- // I dunno what pin 8 is for???
- // int stepper_motor_1_enable_pin = x; // normally you would have three pins for each stepper motor...
- // int stepper_motor_1_direction_pin = y;
- int stepper_motor_1_step_pin = 9;
- // Below is timing code variables for stepper_motor_1
- bool stepper_motor_1_enabled = true; // This lets you turn the motor on or off some other way in code.
- int stepper_motor_1_state = 0;
- 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.
- unsigned long stepper_motor_1_begin_time = 0;
- unsigned long stepper_motor_1_current_time = 0;
- bool led_blinker_enabled = true; // This lets you turn the blinking led on or off some other way in code.
- int led_blinker_state = 0;
- int led_blinker__blink_on_time = 50; // This is the time that the LED will blink on, in milliseconds.
- int led_blinker__blink_off_time = 950; // This is the time that the LED will blink off, in milliseconds.
- int led_blinker__time_in_use = 0;
- unsigned long led_blinker_begin_time = 0;
- unsigned long led_blinker_current_time = 0;
- void setup() {
- pinMode(LED_BUILTIN, OUTPUT);
- pinMode(8, OUTPUT);
- pinMode(stepper_motor_1_step_pin, OUTPUT);
- digitalWrite(8, LOW);
- digitalWrite(stepper_motor_1_step_pin, LOW);
- }
- void loop() {
- if (led_blinker_enabled == true) {
- check_led_blinker();
- }
- if (stepper_motor_1_enabled == true) {
- check_stepper_motor_1();
- }
- } // end of main loop()
- void check_led_blinker() {
- led_blinker_current_time = millis();
- if (led_blinker_current_time >= led_blinker_begin_time) {
- if (led_blinker_state == 0) { // if the LED is currently turned off:
- led_blinker__time_in_use = led_blinker__blink_off_time;
- }
- else { // if the LED is currently turned on:
- led_blinker__time_in_use = led_blinker__blink_on_time;
- }
- if (led_blinker_current_time >= (led_blinker_begin_time + led_blinker__time_in_use)) {
- if (led_blinker_state == 0) {
- led_blinker_state = 1;
- }
- else {
- led_blinker_state = 0;
- }
- digitalWrite(LED_BUILTIN, led_blinker_state);
- led_blinker_begin_time = millis();
- }
- }
- else {
- led_blinker_begin_time = millis();
- }
- }
- void check_stepper_motor_1() {
- stepper_motor_1_current_time = millis();
- if (stepper_motor_1_current_time >= stepper_motor_1_begin_time) {
- if (stepper_motor_1_current_time >= (stepper_motor_1_begin_time + stepper_motor__step_interval)) {
- if (stepper_motor_1_state == 0) {
- stepper_motor_1_state = 1;
- }
- else {
- stepper_motor_1_state = 0;
- }
- digitalWrite(stepper_motor_1_step_pin, stepper_motor_1_state);
- stepper_motor_1_begin_time = millis();
- }
- }
- else {
- stepper_motor_1_begin_time = millis();
- }
- }
- // -the end-
Add Comment
Please, Sign In to add comment