I have two stepper motors that I am trying to move independent of each other. If I use the delayMicroseconds() function, they move at half speed until one is finished and then the second one goes very fast. I am trying to implement the millis() delay without delay(), but I am having a few issues.
- The motors are much louder at the same speed as with the delayMicroseconds() function
- If I use 1 as my interval (speedRight and speedLeft) value, it's quieter, but too fast. a value of 2 gets very loud. Is there a way to use decimal values with this?
Here is the part of my code where I have tried this.
void opening() {
//door OPENING stepper motor operation ////////////////////////////////////////////////////////////////////////////
//set by app door open button
if (openStop == 1) {
unsigned long currentMillis1 = millis();
unsigned long currentMillis2 = millis();
digitalWrite(LEFT_ENABLE, HIGH); //enable needs to be low for stepper to move
digitalWrite(RIGHT_ENABLE, LOW); //enable needs to be low for stepper to move
sensorRightOpen = digitalRead (STOP_SENSOR_RIGHT_CLOSED); //check status of the sensors
sensorLeftOpen = digitalRead (STOP_SENSOR_LEFT_CLOSED);
digitalWrite(RIGHT_DIRECTION, HIGH); //sets clockwise motor direction for right door
digitalWrite(LEFT_DIRECTION, LOW); //sets counterclockwise motor direction for left door
if (sensorRightOpen == LOW) { //checks sensor state of right door open sensor
ledStatusFlagOpen = 0;
if (currentMillis1 - previousMillis1 >= speedRight) {
// save the last time you blinked the LED
previousMillis1 = currentMillis1;
digitalWrite(RIGHT_STEP, HIGH); //right stepper motor moves clockwise
} else {
digitalWrite(RIGHT_STEP, LOW);
}
} else {
ledStatusFlagOpen = 1;
overunFlagRightOpen = 1;
}
if (sensorLeftOpen == LOW) { //checks sensor state of left door open sensor
ledStatusFlagOpen = 0;
if (currentMillis2 - previousMillis2 >= speedLeft) {
// save the last time you blinked the LED
previousMillis2 = currentMillis2;
digitalWrite(LEFT_STEP, HIGH); //left stepper motor moves counter clockwise
} else {
digitalWrite(LEFT_STEP, LOW);
}
} else {
ledStatusFlagOpen = 1;
overunFlagLeftOpen = 1;
}
if (ledStatusFlagOpen == 1) {
if (sensorLeftOpen == HIGH && sensorRightOpen == HIGH) {
if (overunFlagLeftOpen == 1 && overunFlagRightOpen == 1) {
digitalWrite(LEFT_ENABLE, LOW); //disables stepper
digitalWrite(RIGHT_ENABLE, HIGH); //disables stepper
overunFlagLeftOpen = 0;
overunFlagRightOpen = 0;
overunFlagOpen = 1;
}
doorButtonOpen = 0;
}
}
} else { //stop the door if open button is pressed a second time before the close button
}
}