Program Problem When Adding a for loop

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)

What is the point of those for loops? They aren’t doing anything useful. If you took them out then the same code would loop thanks to the loop function.

Yeah, you had some nesting there that really wasn't necessary. Is this better?

``````// constants won't change. Used here to set a pin number :
const int pinMtr1Step = 5;      // the number of the step pin
const int pinMtr2Step = 8;       // the number of the step pin

const int pinMtr1En = 6;
const int pinMtr1Dir = 12;
const int pinMtr2En = 10;
const int pinMtr2Dir = 9;

// 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;        // 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( pinMtr1Step, OUTPUT );
pinMode( pinMtr2Step, OUTPUT );
previousMillis = 0;

//Serial.begin(BAUD);

//Motor 1
pinMode( pinMtr1En,OUTPUT ); // Enable
pinMode( pinMtr1Dir,OUTPUT ); // Dir (pin 4 was damaged - switched to 12)
digitalWrite( pinMtr1Dir, HIGH ); //Direction
digitalWrite( pinMtr1En,LOW ); // Set Enable low

//Motor 2
pinMode( pinMtr2En,OUTPUT ); // Enable
pinMode( pinMtr2Dir,OUTPUT ); // Dir  Motor  2
digitalWrite( pinMtr2Dir, HIGH );// Direction
digitalWrite( pinMtr2En,LOW ); // Set Enable low

}//setup

void loop()
{
unsigned long
currentMillis = millis();

if( (currentMillis - previousMillis) >= interval )
{
previousMillis = currentMillis;  // save the last time you blinked the LED
motor1State = (motor1State==LOW)?HIGH:LOW;
motor2State = (motor2State==LOW)?HIGH:LOW;

digitalWrite( pinMtr1Step, motor1State );
digitalWrite( pinMtr2Step, motor2State );

}//if

}//loop
``````

I’ve been working on a program to control two stepper motors using micros instead of delay and have a problem I can’t solve. The program will run the stepper motors but as soon as I add to the program to run the motors forward and reverse, all that happens is the motors run in reverse. I’ve tried this same programming using the example of Blink Without Delay by modifying the program to blind slow than fast. All it does is blink slow. What am I missing?

Chuck

*/

// constants won’t change. Used here to set a pin number :
const int ledPin = 13; // the number of the LED pin

// Variables will change :
int ledState = LOW; // ledState used to set the LED

// 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 LED was updated

// constants won’t change :
const long interval = 1000; // interval at which to blink (milliseconds)
const long intervalFaster = 500;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}

void loop() {

unsigned long currentMillis = millis();
for(int x = 0; x<=4; x++)
{
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}

for(int x = 0; x<=4; x++)
{
if (currentMillis - previousMillis >= intervalFaster) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
}

Print the index of the for loop, in the body. It will become blindingly obvious what the problem is.

Thanks for the responses. First I have to apologize, the file I attached was the wrong one, it was for the stepper motors not the Blink Without Delay. I’ve attached the correct file. The “for(x = 0; x < 3200*r; x++) // Loop 3200 times” is vital to running the stepper motor. The timed LOW HIGH makes the shaft rotate. Please look at the BlinkWithoutDelayFastAndSlow.ino file, because if I get that working, it will help to get the stepper motor programming to work, because it duplicates the result.
Chuck

The “for(x = 0; x < 3200*r; x++) // Loop 3200 times” is vital to running the stepper motor.

No it isn’t. And when you realize that you’ll see the light and fix your progra.

The loop function is already repeating itself. You can decide what it needs to do on each pass. You don’t need to trap the execution in a for loop to get something to repeat.

You’re thinking like a story. Where the loop function reads out all the steps from beginning to end in a linear fashion. Think more like a checklist where the loop function simply checks to see what one tiny step it is time to perform and performs it and ends.

Delta_G,

I’m sequencing two events in either program. The stepper motor program is supposed to rotate the stepper motor one revolution. The " for(int x = 0; x < 3200; x++) " loop makes the shaft rotate one revolution. When the one revolution is completed then the next sequence is suppose to happen, ie the shaft rotates in the opposite direction. Similarly, the blink program is suppose to blink the light at one rate, then blink the second light at a different rate.

Chuck

Yes but you're still thinking of doing those two separate things by writing one and then writing the other. And that's the mistake in your thinking. Instead, think of loop having variables to keep track of what it is supposed to do.

``````void loop(){

Are we taking steps forward?
Is it time to take a step?
take a step forward and add one to step count
have we taken 3200 steps
change to other direction

Are we taking steps backwards
Is it time to take a step
take a step backwards and add one to step count
have we taken 3200 steps
Change direction to forward.
``````

Each pass of loop only takes one step. Most of the time loop does nothing because it isn't time to do anything.

Delta_G

I'm some what of a beginner at this programming and appreciate your guidance, but I'm having a problem putting together the program. Learning this mythology is important to what I'm trying to accomplish. I've put together a program that runs the robot in a square, however it uses the delay function. Would put together a program that I can learn from? It would be much appreciated.

Chuck