Skip to main content
4 of 4
added 12 characters in body; edited title
ocrdu
  • 1.8k
  • 3
  • 11
  • 24

Blink LED without delay with stepper motor

Based on a previous question I modified the script to use blink without delay.

Unfortunately, it does not work, any idea why?

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

Modified code (based on @Michel Keijzers, see below):

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "Qwiic_LED_Stick.h" // Click here to get the library: http://librarymanager/All#SparkFun_Qwiic_LED_Stick

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 500;           // interval at which to blink (milliseconds)

LED LEDStick; //Create an object of the LED class

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

void setup() {
  Wire.begin();
  Serial.begin(115200);
  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(50);  // 50 rpm


  //Start up communication with the LED Stick
  if (!LEDStick.begin())  {
    Serial.println("Qwiic LED Stick failed to begin. Please check wiring and try again!");
    while (1);
  }

  Serial.println("Qwiic LED Stick ready!");
//  LEDStick.setLEDColor(255, 0, 0);
  LEDStick.setLEDBrightness(1);
}

void loop() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    ledState = !ledState;
    // set the LED with the ledState of the variable:
    uint8_t intensity = ledState == HIGH ? 255 : 0;
    LEDStick.setLEDColor(intensity, intensity, intensity);
  }

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

Code:

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "Qwiic_LED_Stick.h" // Click here to get the library: http://librarymanager/All#SparkFun_Qwiic_LED_Stick

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)

LED LEDStick; //Create an object of the LED class
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);

void setup() {
  Wire.begin();
  Serial.begin(115200);
  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(50);  // 50 rpm


  //Start up communication with the LED Stick
  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() {
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    LEDStick.setLEDColor(50, 50, 50);
  }

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