hello
i'm joining to a robot race competition which the robot go on the maze . i'm using simple two motors , motor drive shield L298 , arduino uno and two ultrasonic sensors HC-SR04
i have wrote this code but i can't turn right and left . can you help me to solve the maze
const int IN1=3;
const int IN2=5;
const int IN3=6;
const int IN4=9;
const int trigPin1 = 13;
const int echoPin1 = 12;
const int trigPin2 = 2;
const int echoPin2 = 4;
void setup() {
Serial.begin(115200);
pinMode( IN1 ,OUTPUT);
pinMode( IN2 ,OUTPUT);
pinMode( IN3 ,OUTPUT);
pinMode( IN4 ,OUTPUT);
}
void go(){
analogWrite(IN1,60);
analogWrite(IN2,0);
analogWrite(IN3,65);
analogWrite(IN4,0);
}
void ClockWise(){
analogWrite(IN1,0);
analogWrite(IN2,100);
analogWrite(IN3,100);
analogWrite(IN4,0);
}
void counterClockWise(){
analogWrite(IN1,100);
analogWrite(IN2,0);
analogWrite(IN3,0);
analogWrite(IN4,100);
}
void Stop(){
analogWrite(IN1,0);
analogWrite(IN2,0);
analogWrite(IN3,0);
analogWrite(IN4,0);
}
void back (){
analogWrite(IN1,0);
analogWrite(IN2,100);
analogWrite(IN3,0);
analogWrite(IN4,100);
}
void loop() {
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration1 , duration2 , cm1 , cm2;
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trigPin1, OUTPUT);
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin1, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin1, INPUT);
duration1 = pulseIn(echoPin1, HIGH);
if (microsecondsToCentimeters(duration1)<=20&µsecondsToCentimeters(duration1)>0){
go();
}
else if(microsecondsToCentimeters(duration1)>20 ){
ClockWise();
}
else if (microsecondsToCentimeters(duration1)>40){
Stop();
}
// convert the time into a distance
cm1 = microsecondsToCentimeters(duration1);
Serial.print(cm1);
Serial.print("cm");
Serial.println();
delay(100);
pinMode(trigPin2, OUTPUT);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin2, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);
if (microsecondsToCentimeters(duration2)<=20&µsecondsToCentimeters(duration2)>0){
go();
}
else if (microsecondsToCentimeters(duration2)>20 && microsecondsToCentimeters(duration1)<=20){
counterClockWise();
}
else if (microsecondsToCentimeters(duration2)>40){
Stop();
}
// convert the time into a distance
cm2 = microsecondsToCentimeters(duration2);
Serial.print(cm2);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}