[SOLVED] using multiple 'if' loops one at a time

Hello

I am trying to program a simple robot to move in a figure-8 pattern. I am using a DUO board with an osepp motor shield. I am using two 28BLY-48 motors to drive the wheels. It seems to me that my code is executing both my 'if' statements simultaneously, causing both wheels to turn at the same time. I would like one wheel to turn 3 revolutions, then the other to turn 3 revolutions, then repeat. I have done some troubleshooting;

I have commented out each 'if' loop and run them successfully independently by changing my variable

I have nested my if statements but get the same behavior, or the first loop completes then they both run simultaneously.

I have tried 'while' loops and had the same results.

I have tried enclosing each loop in curly braces to isolate them with no success.

I have tried 'if...else if' and 'do...while' loops. I can get the same results with any of them.

I read through the reference pages for the different types of loops.

In each case, I can get the code to verify and upload, but I am not getting the intended results.

I think I need something at the end of each loop that tells the program to go back to the beginning of the loop rather than going to the next loop. I tried adding 'break' and 'continue' unsuccessfully. Here is the simplest and cleanest version of my code.

// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!
// This program is intended to make a robot move in a figure-8 pattern

#include <AFMotor.h>

// Connect a stepper motor with 512 steps per revolution (28bjy-48 motor)
// to motor port #2 (M3 and M4) and to motor port #1 (M1 and M2)

AF_Stepper motorl(512, 1);
AF_Stepper motorr(512, 2);

// create a variable to use to keep track of steps

int StepCount = 1536; // this is equal to three wheel revolutions, or one spin of the robot

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  motorl.setSpeed(400);  // 400 rpm   
  motorr.setSpeed(400);  // 400 rpm
}

void loop() {

  Serial.println("Double coil steps");
  
  // rotate the robot one spin to the right
  
  if (StepCount < 1536) {
    motorl.step(1, FORWARD, DOUBLE);
      StepCount++;                            // increment StepCount variable by +1 for each step
     }
  
  // rotate the robot one spin to the left
  
  if (StepCount > 0 ) {
    motorr.step(1, FORWARD, DOUBLE); 
     StepCount--;                               // increment StepCount variable by -1 for each step
    }

 
}

Is there some command I am missing to tell the code to execute the loops independently?

Thanks
Steve

Your two IF statements are not mutually exclusive

if (StepCount < 1536) {
if (StepCount > 0 ) {

so for any value between 0 and 1536 both will be true.

Without seeing how the variable StepCount is updated I can't tell you any more.

Best thing would be to describe what you want in plain language (i.e. not code)

...R

Seeing as the count is initialised to 1536, first time through the first if is ignored, but the second one is fired. It decrements the count, so next time through, the first if fires and increments the counter again. Then the second one fires, decrements, and the first one fires and increments.

So by my admittedly quick look, seems the counter oscillates around 1536?

edit.... now would be a good time to print the count value every time you (for instance) enter and exit an "if" so you can see what's happening. Also print something to indicate which "if" you're currently in

JimboZA:
It decrements the count, so next time through, the first if fires and increments the counter again. Then the second one fires, decrements, and the first one fires and increments.

OOPS, I missed that.

...R

Please don't use the term IF LOOP the if statment does something if a condition is true then the program moves on.. It's not a loop.

Thank you for the responses.

I agree with JimboZA, I think what is happening is what you are describing. I'm not sure how to print out the counter value, but I am reasonably certain that is exactly what is going on.

What I want the program to do is to repeat the first 'if' statement 1536 times, causing the left motor to rotate three times and incrementing the counter to 1536 while the left motor stays still. Then I want the program to run the second 'if' statement 1536 times causing the right motor to turn 3 times and decrement the counter to 0 while the left motor stays still. Then repeat so the robot does a figure-8.

My question is: is there a syntax that would make my program complete the first 'if' condition before moving on to the second 'if' condition?

@KenF: Sorry if I got that term wrong. Maybe let me know what I should call it so I can fix the title?

stkenned:
@KenF: Sorry if I got that term wrong. Maybe let me know what I should call it so I can fix the title?

No worries, it's just something that jangles my nerves. They're statements not loops :slight_smile:

stkenned:
What I want the program to do is to repeat the first 'if' statement 1536 times, causing the left motor to rotate three times and incrementing the counter to 1536 while the left motor stays still. Then I want the program to run the second 'if' statement 1536 times causing the right motor to turn 3 times and decrement the counter to 0 while the left motor stays still. Then repeat so the robot does a figure-8.

That description suggests a pair of FOR loops

for (n = 0; n < 1536; n++) {
  // step one motor
}
for (n = 0; n < 1536; n++) {
  // step the other motor
}

although I can't help feeling your real requirement is going to be more complex.

...R

To get something to happen "x" number of times, you need a for... this one really is a loop :wink:

A million thank you's Robin2! That did the trick. The only strange thing is that I had to add a delay in the statement. The wheels would buzz alternately, so I knew the 'for' statement was right. I decided to slow it down to see what happened. I suspect the processor was working faster than the armature of the motor could mechanically move. 5 millisecs wasn't enough, 10 millisecs works great. Here is the final code I used if anyone else needs it.

  for (StepCount = 0; StepCount < 1536; StepCount++) {
  // step one motor
  motorL.step(1, FORWARD, DOUBLE);
  delay (10);
  }
  for (StepCount = 1536; StepCount > 0; StepCount--) {
  // step the other motor
  motorR.step(1, FORWARD, DOUBLE);
  delay (10);
  }

Moderators can change title to [SOLVED]

stkenned:
The only strange thing is that I had to add a delay in the statement.

Nothing strange about it. I had assumed suitable timing associated with the steps. You are quite right about the Arduino repeating far too fast for a motor.

Glad it's working.

...R