stepper motor won't spin while LED code is running

Thread Starter

Killerbee65

Joined May 15, 2017
256
Hello all! recently I have completed a milestone in my project, the last touch (and frankly the most annoying one) is having a stepper motor spin while at the same time a separate code (integrated into the main code that includes the code to spin the stepper motor) is being executed.

Code:
#include <Stepper.h>
//Written By Nikodem Bartnik - nikodembartnik.pl
#define STEPPER_PIN_1 8
#define STEPPER_PIN_2 7
#define STEPPER_PIN_3 6
#define STEPPER_PIN_4 5
#define motorSteps 400

int pinArray[] = {13, 12};
int count = 0;
float minDelay = 125;
float maxDelay = 250;
const int redPin = 11;
const int bluePin = 10;
int step_number = 0;

void setup() {
  int head = 0;
  int light = 0;
  while( light = 0 ){
    for (count=0;count<2;count++) {
    pinMode(pinArray[count], OUTPUT);
 // Start off with the LED off.
 setColourRgb(0,0,0);
    }
  }
  while( head = 0 ){
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
  }
}

void loop() {
  int light1 = 1;
  int head1 = 1;
  while( light1 = 1 ){
    delay(random(minDelay, maxDelay));
 unsigned int rgbColour[3];
 // Start off with red.
 rgbColour[0] = 255;
 rgbColour[1] = 0;
 rgbColour[2] = 0;  
 // Choose the colours to increment and decrement.
 for (int decColour = 0; decColour < 3; decColour += 1) {
   int incColour = decColour == 2 ? 0 : decColour + 1;
   // cross-fade the two colours.
   for(int i = 0; i < 255; i += 1) {                                                                                                                                                                                                                                                                                                                                                                          
     rgbColour[decColour] -= 1;
     rgbColour[incColour] += 1;
     setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
     delay(5);
   }
}
 for (count=0;count<1;count++) {
   digitalWrite(pinArray[count], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count + 1], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count], LOW);
   delay(random(minDelay, maxDelay));
  }
  for (count=1;count>0;count--) {
   digitalWrite(pinArray[count], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count - 1], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count], LOW);
   delay(random(minDelay, maxDelay));
  }
 }
  while( head1 = 1 ){
 for(int a = 0; a < 14000; a++){
  OneStep(false);
  delay(2);
 }
 for(int a = 0; a < 14000; a++){
  OneStep(true);
  delay(2);
    }
  }
}


void OneStep(bool dir){
    if(dir){
switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
} 
  }else{
    switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
 
  
} 
  }
step_number++;
  if(step_number > 3){
    step_number = 0;
    }
  }

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
 analogWrite(redPin, red);
 analogWrite(bluePin, blue);
}
the code uploads just fine and in application the lights work perfectly (a little dim but that is expected) but the stepper motor doesn't even start. I can tell the motor is essentially waiting for the led code to finish (it is on a loop so never) is because in a earlier code that actually ended, the motor only started when the code ended but I might be wrong. Any and all advice is appreciated!
 

AlbertHall

Joined Jun 4, 2014
12,344
Line 38: How will 'light1' ever change within this loop? If it doesn't, the program will run this loop forever.
Line 73: as above except for 'head1'.
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
Line 38: How will 'light1' ever change within this loop? If it doesn't, the program will run this loop forever.
Line 73: as above except for 'head1'.
I did this on purpose because I wanted both functions to stay in a constant loop no matter what other code was in the program. Learned from the last experiment that the servo motor starts when one code ends so to prevent this I made a never ending loop statement as a precaution. I want to be able to spin the motor WHILE the LEDs are doing thier own thing. I did it with two seperate sets of LEDs, one being a rgb led and another being two blue LEDs (rgb led hues between blue and red while blue led lights up at random).
 

Thread Starter

Killerbee65

Joined May 15, 2017
256
did some digging and found a method using milli()

Code:
#include <Stepper.h>
//Written By Nikodem Bartnik - nikodembartnik.pl
#define STEPPER_PIN_1 8
#define STEPPER_PIN_2 7
#define STEPPER_PIN_3 6
#define STEPPER_PIN_4 5
#define motorSteps 400

int pinArray[] = {13, 12};
int count = 0;
float minDelay = 125;
float maxDelay = 250;
const int redPin = 11;
const int bluePin = 10;
int step_number = 0;
unsigned long blinkStartMillis;
unsigned long headStartMillis;
unsigned long currentMillis;
const unsigned long blinkperiod = 1000;  //the value is a number of milliseconds, ie 1 second
const unsigned long headperiod = 1000;  //the value is a number of milliseconds, ie 1 second
void setup() {
  blinkstartMillis = millis();  //initial start time
  headstartMillis = millis();  //initial start time
    for (count=0;count<2;count++) {
    pinMode(pinArray[count], OUTPUT);
 // Start off with the LED off.
 setColourRgb(0,0,0);
    }
pinMode(STEPPER_PIN_1, OUTPUT);
pinMode(STEPPER_PIN_2, OUTPUT);
pinMode(STEPPER_PIN_3, OUTPUT);
pinMode(STEPPER_PIN_4, OUTPUT);
}

void loop() {
  currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
  Blink();
  Head();
}

void Blink(){
   if (currentMillis - blinkstartMillis >= blinkperiod)  //test whether the period has elapsed
  {
 delay(random(minDelay, maxDelay));
 unsigned int rgbColour[3];
 // Start off with red.
 rgbColour[0] = 255;
 rgbColour[1] = 0;
 rgbColour[2] = 0;  
 // Choose the colours to increment and decrement.
 for (int decColour = 0; decColour < 3; decColour += 1) {
   int incColour = decColour == 2 ? 0 : decColour + 1;
   // cross-fade the two colours.
   for(int i = 0; i < 255; i += 1) {                                                                                                                                                                                                                                                                                                                                                                          
     rgbColour[decColour] -= 1;
     rgbColour[incColour] += 1;
     setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
     delay(5);
   }
}
 for (count=0;count<1;count++) {
   digitalWrite(pinArray[count], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count + 1], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count], LOW);
   delay(random(minDelay, maxDelay));
  }
  for (count=1;count>0;count--) {
   digitalWrite(pinArray[count], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count - 1], HIGH);
   delay(random(minDelay, maxDelay));
   digitalWrite(pinArray[count], LOW);
   delay(random(minDelay, maxDelay));
  }
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
 analogWrite(redPin, red);
 analogWrite(bluePin, blue);
  }
   blinkstartMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }
}
 void Head(){
  if (currentMillis - headstartMillis >= headperiod)  //test whether the period has elapsed
  {
 for(int a = 0; a < 14000; a++){
  OneStep(false);
  delay(2);
 }
 for(int a = 0; a < 14000; a++){
  OneStep(true);
  delay(2);
    }
void OneStep(bool dir){
    if(dir){
switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
} 
  }else{
    switch(step_number){
  case 0:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, HIGH);
  break;
  case 1:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, HIGH);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 2:
  digitalWrite(STEPPER_PIN_1, LOW);
  digitalWrite(STEPPER_PIN_2, HIGH);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
  break;
  case 3:
  digitalWrite(STEPPER_PIN_1, HIGH);
  digitalWrite(STEPPER_PIN_2, LOW);
  digitalWrite(STEPPER_PIN_3, LOW);
  digitalWrite(STEPPER_PIN_4, LOW);
 
  
} 
  }
step_number++;
  if(step_number > 3){
    step_number = 0;
    }
  }
  headstartMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
 }
 }
this is was the code looks like using the new method but of course it comes with a dictionary of errors.


Arduino: 1.8.10 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"


final_head_spin:22:3: error: 'blinkstartMillis' was not declared in this scope

blinkstartMillis = millis(); //initial start time

^~~~~~~~~~~~~~~~

C:\Users\99123114\Documents\Arduino\final_head_spin\final_head_spin.ino:22:3: note: suggested alternative: 'blinkStartMillis'

blinkstartMillis = millis(); //initial start time

^~~~~~~~~~~~~~~~

blinkStartMillis

final_head_spin:23:3: error: 'headstartMillis' was not declared in this scope

headstartMillis = millis(); //initial start time

^~~~~~~~~~~~~~~

C:\Users\99123114\Documents\Arduino\final_head_spin\final_head_spin.ino:23:3: note: suggested alternative: 'headStartMillis'

headstartMillis = millis(); //initial start time

^~~~~~~~~~~~~~~

headStartMillis

final_head_spin:27:2: error: 'setColourRgb' was not declared in this scope

setColourRgb(0,0,0);

^~~~~~~~~~~~

C:\Users\99123114\Documents\Arduino\final_head_spin\final_head_spin.ino: In function 'void Blink()':

final_head_spin:42:24: error: 'blinkstartMillis' was not declared in this scope

if (currentMillis - blinkstartMillis >= blinkperiod) //test whether the period has elapsed

^~~~~~~~~~~~~~~~

C:\Users\99123114\Documents\Arduino\final_head_spin\final_head_spin.ino:42:24: note: suggested alternative: 'blinkStartMillis'

if (currentMillis - blinkstartMillis >= blinkperiod) //test whether the period has elapsed

^~~~~~~~~~~~~~~~

blinkStartMillis

final_head_spin:57:6: error: 'setColourRgb' was not declared in this scope

setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);

^~~~~~~~~~~~

C:\Users\99123114\Documents\Arduino\final_head_spin\final_head_spin.ino:57:6: note: suggested alternative: 'decColour'

setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);

^~~~~~~~~~~~

decColour

final_head_spin:77:76: error: a function-definition is not allowed here before '{' token

void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {

^

C:\Users\99123114\Documents\Arduino\final_head_spin\final_head_spin.ino: In function 'void Head()':

final_head_spin:85:23: error: 'headstartMillis' was not declared in this scope

if (currentMillis - headstartMillis >= headperiod) //test whether the period has elapsed

^~~~~~~~~~~~~~~

C:\Users\99123114\Documents\Arduino\final_head_spin\final_head_spin.ino:85:23: note: suggested alternative: 'headStartMillis'

if (currentMillis - headstartMillis >= headperiod) //test whether the period has elapsed

^~~~~~~~~~~~~~~

headStartMillis

final_head_spin:88:3: error: 'OneStep' was not declared in this scope

OneStep(false);

^~~~~~~

final_head_spin:92:3: error: 'OneStep' was not declared in this scope

OneStep(true);

^~~~~~~

final_head_spin:95:23: error: a function-definition is not allowed here before '{' token

void OneStep(bool dir){

^

Multiple libraries were found for "Stepper.h"
Used: C:\Program
Using library Stepper at version 1.1.3 in folder: C:\Program Files
exit status 1
'blinkstartMillis' was not declared in this scope
 

MrChips

Joined Oct 2, 2009
30,706
The way you have written your while loops your code will never exit the while statements.
Lines 20 - 25 will never exit.
Lines 27 - 32 will never exit.
Lines 38 - 54 will never exit.
Lines 73 - 82 will never exit.
 
Top