Stepper + LED

Hi mates!

i am planning to build a phenakistoscope (https://vimeo.com/78226433#at=0) using a stepper motor and a flashing LED.
I've been messing all day with the stepper, and haven't find the way to use both at the same the blinking led (at independent times for HIGH and LOW states) and the stepper motor rotating at a constant speed.

I can have both functions (led and stepper) working independently, but as soon as i use both at the same time the motor stops rotating continuously and does it synchronized with the led, one step per flash.

#include <AccelStepper.h>
AccelStepper stepper;   // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

unsigned long onTime = 100; 
unsigned long offTime = 1000; 
unsigned long startTime, elapsedTime;

int ledPin=13;

void setup(){
  
   stepper.setMaxSpeed(1000);
   stepper.setSpeed(300);	
  pinMode(ledPin, OUTPUT);
}


void loop(){

blinca(ledPin, 25, 1000);
stepper.runSpeed();

}




//Function for blinking at independant HIGH and LOW times without using delay()
 void blinca(int output, int on, int off){ 
 
//ON
elapsedTime = 0;
startTime = millis();

digitalWrite(output, HIGH);

while (elapsedTime<on){
elapsedTime=millis()-startTime;
}


//OFF
elapsedTime = 0;
startTime = millis();

digitalWrite(output, LOW);

while (elapsedTime<off){
elapsedTime=millis()-startTime;
}
  
}

I used the Accelstepper library as i found it is non-blocking,
but it looks like the problem i have is related to timers... so i'm quite lost right now!

why is this happening and how can i solve it?
any clues?

thanks!!!

while (elapsedTime<on){

elapsedTime=millis()-startTime;
}

Ignoring that I don't see "on" defined in the code, this code is really no different than calling delay(). You're going to sit in that while-loop until enough time has passed. Unless you put other statements inside that while-loop it is effectively a blocking call.

You want to be set up more a state machine using:

if ((unsigned long)(millis() - waitUntil) >= interval)
{ 
// do whatever
}

Determine what "states" your program needs.

That way if the "on time" has elapsed you can decide what to do next (like turn the LED off, setup the next waitUntil, etc) while checking/doing other things.

Hey James!
thanks for the tip, I have it working now!

your help in this other post: Blink without delay with independent on/off times? - Programming Questions - Arduino Forum has also been very helpful for me
:slight_smile:

I attach the final code (added 2 potentiometer for motor speed and strobo freq control)

#include <AccelStepper.h>
AccelStepper stepper;   // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

int ledPin=13;

unsigned long timeLedChanged = millis();
unsigned long period = 1000;
boolean ledOn = true;

int strobe=100;
int velocity=300;




void setup(){
  
  stepper.setMaxSpeed(1000);
  stepper.setSpeed(300);	
  pinMode(ledPin, OUTPUT);
}


void loop(){

strobe = analogRead(A0)/10;
velocity = analogRead(A1)-512;
blinca(13, 1, strobe);
stepper.setSpeed(velocity);
stepper.runSpeed();


}




//Function for blinking at independant HIGH and LOW times without using delay()
void blinca(int output, int ontime, int offtime){
  
  if (millis() - timeLedChanged >= period)
  {
    ledOn = !ledOn;
    timeLedChanged = millis(); 
    digitalWrite(output, ledOn);  
    if (ledOn)
    {
      period = ontime;
    }
    else
    {
      period = offtime; 
    }
  }
  
}