I'm working on a program to control two stepper motors using millis instead of delay to control the speed of the motors. As soon as the "for(int w = 0; w<=4; ++w)" statement was added right after the void loop(), the speed of the motors decreased significantly. The statement "const long interval = 300;" is used to control the speed of the motors. Changing the 300 value no longer will change the speed of the motors with the for statement added. Any help would be appreciated.
// constants won't change. Used here to set a pin number :
const int motor1 = 5; // the number of the step pin
const int motor2 = 8; // the number of the step pin
// Variables will change :
int motor1State = LOW; // Set the initial state
int motor2State = LOW; // Set the initial state
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time Motor states was updated
// constants won't change :
const long interval = 300; // interval controls the speed of the motors
void setup() {
// set the digital pin as output:
pinMode(motor1, OUTPUT);
pinMode(motor2, OUTPUT);
int previousMillis = 0;
//Serial.begin(BAUD);
//Motor 1
pinMode(6,OUTPUT); // Enable
// pinMode(5,OUTPUT); // Step
pinMode(12,OUTPUT); // Dir (pin 4 was damaged - switched to 12)
digitalWrite(6,LOW); // Set Enable low
//Motor 2
pinMode(10,OUTPUT); // Enable
digitalWrite(10,LOW); // Set Enable low
pinMode(9,OUTPUT); // Dir Motor 2
//pinMode(8,OUTPUT); // Step Motor 2
//int y = 0;
//Motor1
digitalWrite(12, HIGH); //Direction
//Motor2
digitalWrite(9, HIGH);// Direction
}
void loop() {
for(int w = 0; w<=4; ++w)
{
unsigned long currentMillis = millis();
long previousMillis;
for(int x = 0; x < 3200; x++) // Loop 3200 times
{
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis; // save the last time you blinked the LED
if (motor1State == LOW && motor2State==LOW) // if the LED is off turn it on and vice-versa:
{
motor1State = HIGH;
motor2State = HIGH;
}
else
{
motor1State = LOW;
motor2State = LOW;
}
digitalWrite(motor1, motor1State);
digitalWrite(motor2, motor2State);
} //end of " if (motor1State == LOW && motor2State==LOW) // if the LED is off turn it on and vice-versa:"
}//end of "if (currentMillis - previousMillis >= interval)"
} //end of "for(int x = 0; x < 3200; x++) // Loop 3200 times"
}
Motor1_withoutdelayJan24.ino (2.42 KB)