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:
- Uses (straight) but only drives the wheels 7.75 turns instead of 8.3,
- Always drives the wheels the same direction. No matter if I use [linesteps] or [-linesteps] for eg.
- 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();
}
}