Guys,am new in programming and i want to make an obstacle avoidance robot.Am using 12V 28BYJ-48 motors and as i have read through different sources,the motors work very fine with accelStepper library.Can anyone help me on how to proceed to make the moveforward,left,right,stop and backward functions.I have attached the codes up to a point where the only thing left are the five functions.I have tried some few ways but since am new to programming,nothing works.I need help please because the deadline of my project is approaching real fast.May God bless you.
#include <Servo.h>
#include <NewPing.h>
#define USTrigger 13
#define USEcho 12
#define MaxDistance 100
#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
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
Servo servo;
NewPing sonar(USTrigger, USEcho, MaxDistance);
unsigned int duration;
unsigned int distance;
unsigned int FrontDistance;
unsigned int LeftDistance;
unsigned int RightDistance;
unsigned int Time;
const int buttonpin = 2;
int buttonval = 1;
/*int motor1Pin1 = 11;
int motor1Pin2 = 10;
int motor1Pin3 = 9;
int motor1Pin4 = 8;
int motor2Pin1 = 7;
int motor2Pin2 = 6;
int motor2Pin3 = 5;
int motor2Pin4 = 4;*/
void setup(){
/*pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor1Pin3, OUTPUT);
pinMode(motor1Pin4, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(motor2Pin3, OUTPUT);
pinMode(motor2Pin4, OUTPUT);*/
delay(3000); //sime time to put the robot down after swithing it on
stepper1.setMaxSpeed(2000.0);
stepper1.move(1); // I found this necessary
stepper1.setSpeed(stepperSpeed);
stepper2.setMaxSpeed(2000.0);
stepper2.move(-1); // I found this necessary
stepper2.setSpeed(stepperSpeed);
pinMode(buttonpin, INPUT);
digitalWrite(buttonpin, HIGH);
servo.attach(3);
//Serial.begin(9600);
}
void loop(){
buttonval = digitalRead(buttonpin);
if (buttonval == 1){
delay(500);
servo.write(90); //Rotate the servo to face the front
scan(); //Go to the scan function
FrontDistance = distance; //Set the variable FrontDistance to the value of the distance returned from the scan function
if(FrontDistance > 40 || FrontDistance == 0) //If there is nothing infront of the robot within 40cm or the distance value is 0 (which for the newping libary means no ping was returned) then...
{
moveForward(); //Go to the moveForward function
delay(500);
}
else //Else (if there is something infront of the robot within 40cm) then...
{
moveStop(); //Go to the moveStop function
servo.write(180); //Move the servo to the left (my little servos didn't like going to 180 so I played around with the value until it worked nicely)
delay(500); //Wait half a second for the servo to get there
scan(); //Go to the scan function
LeftDistance = distance; //Set the variable LeftDistance to the distance on the left
servo.write(0); //Move the servo to the right
delay(500); //Wait half a second for the servo to get there
scan(); //Go to the scan function
RightDistance = distance; //Set the variable RightDistance to the distance on the right
if(RightDistance < LeftDistance) //If the distance on the right is less than that on the left then...
{
moveLeft(); //Go to the moveLeft function
delay(500); //Pause the program for half a second to let the robot turn
}
else if(LeftDistance < RightDistance) //Else if the distance on the left is less than that on the right then...
{
moveRight(); //Go to the moveRight function
delay(500); //Pause the program for half a second to let the robot turn
}
else //If the distance on the left and right are equal (highly unlikely but put in none the less) then...
{
moveBackward(); //Go to the moveBackward function
delay(500); //Pause the program for 200 milliseconds to let the robot reverse
moveRight(); //Go to the moveRight function
delay(500); //Pause the program for 200 milliseconds to let the robot turn right
}
}
}
else
moveStop();
}