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