Steppers with Functions misbehaving

So, my goal is to use functions to drive my little car through any number of doglegs.
I'm using a Uno with a pair of 5V, 28BYJ-48 steppers connected to individual ULN2003 driver boards. These are powered by a 7V LiPo, 850mAh batt.

What it IS doing by looking at the serial monitor is:

  1. Uses (straight) but only drives the wheels 7.75 turns instead of 8.3,
  2. Always drives the wheels the same direction. No matter if I use [linesteps] or [-linesteps] for eg.
  3. Performs (straight)function, then led, then (leftturn) function, skips (nextbit)goes to (rightturn) but drives to infinity.

I've tried commenting out functions but it always does the wrong things as above no matter which one(s) I blot out. Now I have bald patches where my hair used to be.

#include <AccelStepper.h>
#define HALFSTEP 8

// motor pins
#define motorPin1  3     // IN1 on the ULN2003 driver 1
#define motorPin2  4     // IN2 on the ULN2003 driver 1
#define motorPin3  5     // IN3 on the ULN2003 driver 1
#define motorPin4  6     // IN4 on the ULN2003 driver 1

#define motorPin5  8     // IN1 on the ULN2003 driver 2
#define motorPin6  9     // IN2 on the ULN2003 driver 2
#define motorPin7  10    // IN3 on the ULN2003 driver 2
#define motorPin8  11    // IN4 on the ULN2003 driver 2
#define ledpin     7

/* Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48*/

AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);

/* variables */
int stepperSpeed = 500; //speed of the stepper (steps per second)
int linesteps=33831; // 8.3 x 4076 steps actually= 33830.8
int turnsteps=1019; // 90deg = a quater turn =4076/4
int sidesteps=4076; // single rev of wheel
int steps1=0;
int steps2=0;
int target=0;

void setup() {
  delay(3000);    //3 secs to put the robot down after switching it on
  stepper1.setMaxSpeed(1000.0);  
  stepper1.setAcceleration(100.0);  
  stepper2.setMaxSpeed(1000.0);
  stepper2.setAcceleration(100.0); 
  Serial.begin(9600);
}

void loop() 
{
  straight();
  Serial.println("straight finished");
  digitalWrite(ledpin, HIGH);
  delay (1000);         //REPRESENTS SOME OTHER FUNCTION
  digitalWrite(ledpin,LOW);
  leftturn();
  Serial.println("leftturn finished");
  digitalWrite(ledpin, HIGH);
  delay (1000);         //REPRESENTS SOME OTHER FUNCTION
  digitalWrite(ledpin,LOW);
  nextbit(); 
  Serial.println("nextbit finished");
  digitalWrite(ledpin, HIGH);
  delay (1000);         //REPRESENTS SOME OTHER FUNCTION
  digitalWrite(ledpin,LOW);
  rightturn();
  Serial.println("rightturn finished");
  }
void straight (void){
    stepper1.move(linesteps);
    stepper2.move(-linesteps);  
while(stepper2.distanceToGo()>0){
    stepper1.setSpeed(1000);
    stepper2.setSpeed(1000);  
    stepper1.run();
    stepper2.run();
}
}
void leftturn (void){
  stepper1.setCurrentPosition(0);
  stepper2.setCurrentPosition(0);
    stepper1.move(turnsteps);
    stepper2.move(turnsteps);  
while(stepper1.distanceToGo()>0){
    stepper1.setSpeed(500);
    stepper2.setSpeed(500);
    stepper1.run();
    stepper2.run();
}
}
void nextbit (void){
  stepper1.setCurrentPosition(0);
  stepper2.setCurrentPosition(0);
    stepper1.move(sidesteps);
    stepper2.move(-sidesteps); 
while(stepper2.distanceToGo()>0){
    stepper1.setSpeed(100);
    stepper2.setSpeed(100);
    stepper1.run();
    stepper2.run();
}
}
void rightturn (void){
  stepper1.setCurrentPosition(0);
  stepper2.setCurrentPosition(0);
    stepper1.move(-turnsteps);
    stepper2.move(-turnsteps);  
while(stepper1.distanceToGo()<0){
    stepper1.setSpeed(500);
    stepper2.setSpeed(500);
    stepper1.run();
    stepper2.run();
}
}

1 revolution of shaft in half step mode is 1026 (513 * 2) steps, setSpeed, setMaxSpeed and setAcceleration should be entered as floats, e.g. setMaxSpeed(1000.0), float stepperSpeed = 500.0;
Here's a test sketch I wrote.

#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(8, 8, 10, 9, 11);

void setup()
{  
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(300.0);
  stepper.setSpeed(20.0);
  stepper.setAcceleration(100.0);

}

void loop()
{
    stepper.runSpeed();
    //stepper.setSpeed(40.0);
     //If at the end of travel go to the other end
   if (stepper.distanceToGo() == 0)
   {
     stepper.moveTo(1026);
     stepper.setSpeed(32.0);
      delay(500);
   }  
   if (stepper.distanceToGo() == 0)
   {
      stepper.moveTo(0);
      stepper.setSpeed(-256.0);
      delay(500);
   }   

}

Thanks. will try that.
Just with the steppers. Lets see if I've got this.... My28BYJ-48's have 64 shaft steps per rev in half steps. With reduction gearing of 64(specs) but more accurately it's 63.684:1, which means one rev = 64*63.684=4076 steps. That was the way I worked it out, is that right?

I don't know where all those different specs on the 28BYJ-48s came from, but what I can say from experimenting is:
The motor has 8 steps per revolution.
513 full steps makes the output shaft turn almost 1 rev, 514 steps, slightly more than 1 rev so the gear ratio is not exactly 64:1.
After 513000 steps (should have been 1000 revs) at low speed (16 steps / second, took almost 9 hours), it still needed 34 steps to get back to start position, so that would make 513.034 steps / rev and gear ratio about 64.12925.
And that's all I know so far. :slight_smile:

Well, seems the motor I was experimenting with (marked 28BYJ-48), was this one from Adafruit:

Which has been modified (I guess) with a different gear train, approximately 16 to 1 instead of 64 to 1, so that's where my confusion originated. :confused:
Anyway, ignore my previous post.