Hi,
I am building a autonomus driving car, with one ultrasonic senor(HC-SR04) on the front and one on the back. And this is the code I am using
#define trigPin 12
#define echoPin 11
#define trigPin2 6
#define echoPin2 5
#define motorIn1 9
#define motorIn2 10
#define max_distance 200
long duration, distance, frontdis, backdis;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
SonarSensor(trigPin, echoPin);
frontdis = distance;
SonarSensor(trigPin2,echoPin2);
backdis = distance;
Serial.print(frontdis);
Serial.println(backdis);
/**
if (frontdis < 2)
{
analogWrite(motorIn1, 500);
analogWrite(motorIn2, 0);
Serial.print("Going back");
}else{
analogWrite(motorIn1, 0);
analogWrite(motorIn2, 500);
Serial.print("Everything all right!!");
}
**/
}
void SonarSensor(int trigPinSensor,int echoPinSensor)//it takes the trigPIN and the echoPIN
{
digitalWrite(trigPinSensor, LOW);
delayMicroseconds(2);
digitalWrite(trigPinSensor, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinSensor, LOW);
duration = pulseIn(echoPinSensor, HIGH);
distance= (duration/2) * 0.0314;
}
void front()
{
analogWrite(motorIn1, 500);
analogWrite(motorIn2, 0);
}
void back()
{
analogWrite(motorIn1, 0);
analogWrite(motorIn2, 500);
}
In the serial monitor I get 0 for both of the. Here is a picture of the error(it should be somewhere here)
I know that using NewPing Library. But I get a zero for one of them, and I have no idea where to store them. If you are answering this it would be great to provode some code examples, I am using a L293D motor driver for this project. Thanks in advance.