0

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);
}
ocrdu
1,7953 gold badges12 silver badges24 bronze badges
asked Jul 25, 2022 at 6:35
3
  • 1
    it does not work is a useless description of a problem ... what actually happens? Commented Jul 25, 2022 at 6:42
  • It does not blink properly Commented Jul 25, 2022 at 7:28
  • that is just as cryptic ... what were you expecting to happen? ... what actually happened? Commented Jul 25, 2022 at 7:45

1 Answer 1

0

You only set (assign) ledState, but you never use it.

I think the following fragment

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

Should be something like:

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

This 'converts' the ledState into white (RGB values 255) when HIGH or black/off (RGB values 0) when LOW.

Also you can change the line

if (LEDStick.begin() == false) {

into

if (!LEDStick.begin()) {

and change the lines

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

into

// if the LED is off turn it on and vice-versa:
ledState = ledState == LOW ? HIGH : LOW;

or even

ledState = !ledState;
answered Jul 25, 2022 at 6:42
2
  • Thanks a lot. I did the modified code and updated it in the question, it seems to blink but longer than 1sec. Can it be related to the board frequency. How can I control different light colors based on this code: uint8_t intensity = ledState == HIGH ? 255 : 0; LEDStick.setLEDColor(intensity, intensity, intensity); Commented Jul 25, 2022 at 7:38
  • Please upvote and accept the answer if it helped and is the solution for your problem. I don't see directly why it blinks longer than 1 sec, you can print out variables to find out the exact times. About the colors, you can transform LOW/HIGH into any value between 0 and 255 for both red, green and blue, just play a bit with the arguments inside the LEDStick.setLEDColor statement. Commented Jul 25, 2022 at 7:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.