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.
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!
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);
}