0

I connected a LED stick to a stepper motor. Is it possible to blink the LED without stopping the motor rotation?

I am using SparkFun RedBoard Plus, Qwiic LED Stick - APA102C, Adafruit Stepper motor - NEMA-17 size - 200 steps/rev and Adafruit Motor Shield V2

Thanks

Script:

#include <Adafruit_MotorShield.h>
#include <Wire.h>
#include "Qwiic_LED_Stick.h"

LED LEDStick;
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void setup() {
  Wire.begin();
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Stepper test!");

  if (!AFMS.begin()) {
    // if (!AFMS.begin(1000)) {
    Serial.println("Could not find Motor Shield. Check wiring.");
    while (1);
  }
  Serial.println("Motor Shield found.");
  myMotor->setSpeed(25);  // 25 rpm

  if (LEDStick.begin() == false) {
    Serial.println("Qwiic LED Stick failed to begin. Please check wiring and try again!");
    while (1);
  }
  Serial.println("Qwiic LED Stick ready!");
}

void loop() {
  Serial.println("Single coil steps");
  myMotor->step(200, FORWARD, SINGLE);

  LEDStick.setLEDColor(10, 10, 10);
  delay(1000);
  LEDStick.LEDOff();
  delay(1000);
}
3

1 Answer 1

2

Often, the solution to most simultaneous software delay problems is a state machine. Generally, a state machine is an infinite loop with no or few delays. The state machine's job is to keep track of all the possible state the software is to control.

In this case we might try to implement:

  1. LED on & motor active.
  2. LED off & motor active.
  3. LED on & motor is not active.
  4. LED off & motor is not active.

The library Adafruit_StepperMotor you have chosen might be blocking. This text from the Arduino reference step library admits this similar function is blocking:

This function is blocking; that is, it will wait until the motor has finished moving to pass control to the next line in your sketch.

If so, and the call to step() blocks the LED action for longer then is tolerable, consider calling step() with fewer step such that the blocking delay is tolerable. For example, 100 steps are desired, consider calling step 10 times only advancing the motor 10 steps each time.

Back to the state machine. To keep track of the above 4 state you need 2 flags. An LED flag and a Motor flag. Control the LED flag using mills() not delay().

mills():

Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.

Consider writing the state machine code to immediately turn the LED on and save the current value returned from mills(). Each subsequent time through the state machine check if mills() returns a value 1000 more than the value saved (do consider that mills() will eventually overflow and start over at zero). When it does toggle the LED, save the new mills() returned value and start checking each subsequent time through the state machine.

As we are running the state machine over and over again as fast as the processor can manage, we can also test an externally set flag to run the motor. We need to determine if the call to step() is blocking. If it is we need to determine the maximum number of steps / delay tolerable by the LED feature. If we discover this is, say, 10 step, we need to devise code to divide the desired number of steps into individual calls of 10 or less steps. If this approach is desired, control the state machine's motor flag by activating it when the number of steps is > 0. Subtracting 10 from the number of steps every iteration through the state machine. And setting the state machine's motor flag to deactivate when steps is = 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.