Im working on an arduino robot that needs to follower a leader robot infront of it. The leader robot can accelerate, decelerate , and stop and my robot needs to be able to follow the leader without crashing into it.
I have my two sensors working however Im having a harder time integrating the motors to ramp up or down. Currently only one wheel turns on at a time for the right and left commands. How can I appropriatly ramp up or down the wheel speed.
int SpRMotor = 5; //speed Right Motor
int MotorA1 = 7; //Right Motor = +
int MotorA2 = 6; //Right Motor = -
int MotorB1 = 8; //Left Motor = -
int MotorB2 = 9; //Left Motor = +
int SpLMotor = 10; //speed Left Motor
int TriggerPIN1 = 3;//right
int EchoPIN1 = 2;
int TriggerPIN2 = 12;//left
int EchoPIN2 = 11;
void setup(){
pinMode(TriggerPIN1,OUTPUT);
pinMode(EchoPIN1,INPUT);
pinMode(TriggerPIN2,OUTPUT);
pinMode(EchoPIN2,INPUT);
pinMode(MotorB1, OUTPUT);
pinMode(MotorB2, OUTPUT);
pinMode(MotorA1, OUTPUT);
pinMode(MotorA2, OUTPUT);
pinMode(SpLMotor, OUTPUT);
pinMode(SpRMotor, OUTPUT);
analogWrite(SpRMotor, 170);
analogWrite(SpLMotor, 170);
Serial.begin(9600);
}
void loop(){
int limit;
int sampledata1, sampledata2;
sampledata1 = distance(TriggerPIN1, EchoPIN1);
Serial.print("distance1: ");
Serial.print(sampledata1);
delay(100);
sampledata2 = distance(TriggerPIN2, EchoPIN2);
Serial.print(" distance2: ");
Serial.println(sampledata2);
delay(100);
limit = 10;
if((15> sampledata1 >= limit)&&(15> sampledata2 >= limit))
{
Serial.print("True");
forward();
delay(100);
}
if (sampledata2 >= 15 )
{turnRight();}
if (sampledata1 >= 15 )
{turnLeft();}
if ((limit > sampledata1)&&(limit> sampledata2 ))
{
Stop();
}
}
void forward(){
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, HIGH);
digitalWrite(MotorB1, HIGH);
digitalWrite(MotorB2, LOW);
}
void turnRight(){
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, HIGH);
digitalWrite(MotorB2, LOW);
}
void turnLeft(){
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, HIGH);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, LOW);
}
void Stop(){
digitalWrite(MotorA1, LOW);
digitalWrite(MotorA2, LOW);
digitalWrite(MotorB1, LOW);
digitalWrite(MotorB2, LOW);
}
int distance(int trig, int echo)
{ digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
long timedelay = pulseIn(echo,HIGH);
int distance = 0.0343 * (timedelay/2);
return distance;
}