I modify the program to follow:
#include <Servo.h>
Servo myservo;
int pos1 = 90;
int pos2 = 150;
int pos3 = 30;
int number = 0;
int decrease =0;
void setup() {
myservo.attach(10);
pinMode(7, OUTPUT);
pinMode(11, OUTPUT);
pinMode(13, OUTPUT);
pinMode(12, INPUT);
}
void loop()
{
long front;
// pinMode(pingPin, OUTPUT);
digitalWrite(13, LOW);
delayMicroseconds(2);
digitalWrite(13, HIGH);
delayMicroseconds(10);
digitalWrite(13, LOW);
// pinMode(inPin, INPUT);
front = pulseIn(12, HIGH);
delay(100);
if (front <= 2900)
{
analogWrite(11, 0);
delay(50);
digitalWrite(7, HIGH);
delay(50);
analogWrite(11, 90);
delay(600);
myservo.write(pos2);
delay(100);
delay(600);
myservo.write(pos1);
delay(100);
number = 1;
decrease = 0;
}
else if (front > 2900 && front <= 11600)
{
if (number = 1)
{ digitalWrite(7, LOW);
analogWrite(11,95);
delay(30);
}
if (decrease >= 50)
{
digitalWrite(7, LOW);
analogWrite(11,45);
delay(15);
myservo.write(pos1);
}
else
{
digitalWrite(7, LOW);
analogWrite(11,75);
delay(15);
myservo.write(pos1);
decrease = decrease + 1;
}
}
else
{
digitalWrite(7, LOW);
analogWrite(11, 130);
delay(15);
myservo.write(pos1);
decrease = decrease + 1;
}
}
First, There are two ultrasonic sensor, i call them front ultrasonic and back ultrasonic.
Pin 12,13 Front Ultrasonic
Pin 8,9 Back Ultrasonic
This program only use the front ultrasonic
But i don't know why there are some problems
1. When i connect both ultrasonic sensor without changing the program. The back ultrasonic work same as the front ultrasonic.
That means when back ultrasonic detect object, the car move backward.
2. When i connect only the back ultrasonic vcc and ground to same voltage source, same problem
3. When i connect the back ultrasonic vcc and ground to an external voltage source, same problem
Maybe you would ask me why use this program rather than two ultrasonic program
Because i wanna step by step, If only connect front ultrasonic, it works perfect,
But i really don't understand why if i connect the other ultrasonic sensor to it, it also works
I would be happy if anyone can help me to fix this problem