/*********************************************************
* LED Strip Demo with Stepper Motor
* Written by Andy Weese
* March 21, 2023
*********************************************************/
//add libraries for nonblocking timing and LED control
#include <elapsedMillis.h>
#include <FastLED.h>
#include <AccelStepper.h>
#define MotorInterfaceType 4
//assign LED pin and number of LEDs
const int LED_PIN = 7;
const int NUM_LEDS = 100;
//set amount of time diffrent patterns will be held
unsigned long time_slide = 5;
CRGB leds[NUM_LEDS]; // tells LED library number of LEDs on strip
elapsedMillis LEDms; // sets up timer for use with the LEDs
AccelStepper stepMtr(MotorInterfaceType, 8, 10, 9, 11); //pin definitions for IN1-IN3-IN2-IN4
void setup() {
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
stepMtr.setMaxSpeed(1000.0);
stepMtr.setAcceleration(50.0);
stepMtr.setSpeed(200);
stepMtr.moveTo(2038);
}
void loop() {
moveMotor(); // move the motor
FastLED.setBrightness(50); // sets/resets LED brightness each loop
colorsrand(255,0,0,100);
colorsrand(255,255,0,100);
colorsrand(0,255,0,100);
colorsrand(0,255,255,100);
colorsrand(0,0,255,100);
}
// the function for the random LED lighting
void colorsrand(int redval, int greenval, int blueval, int lednum){
for (int i = 0; i < lednum; i++){
if (LEDms > time_slide){
leds[i] = CRGB(redval, greenval, blueval);
FastLED.show();
LEDms = 0;
}
// resets LED timer each time while loop is exited and in the for loop lights led # that i is currently at
}
}
// the function to move the motor
void moveMotor(){
if (stepMtr.distanceToGo() == 0) { // check if it's time to change direction
stepMtr.moveTo(-stepMtr.currentPosition());
}
stepMtr.run(); // move the motor one step
}