I am working on a code for an autonomous rd car and I'm having problems with it reading the Ultrasonic sensor when I run the code and check the serial monitor I get back nothing. I have tried the sensor by itself and it work . Does anyone have any ideas?
Here is the code
// autonomous car
//
int motorPin1 = 6; // motor wire connected to digital pin 5
int motorPin2 = 5; // motor wire connected to digital pin 6
int motorPin3 = 7; // steering motor wire connected to digital pin 7
int motorPin4 = 8; // steering motor wire connected to digital pin 8
int steeringMotorSpeed = 150; // Set default power to send to steering motor
int avoidDistance = 80;
int endAvoidDistance = 100;
int goToReverseDistance = 55;
int reverseTime = 2000;
int fullTrottleMinDistance = 250;
int fullTrottleSpeed = 220;
int cruiseSpeed = 160;
int avoidSpeed = 120;
int reverseSpeed = 130;
int sensorDelay = 100; //
long randNumber;
int avoidDirection = 1;
int avoidCirclesBeforeDirectionChange = 10;
int countDown = 1;
int distanceCountDown = 1;
#include "NewPing.h"
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int distance = (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// The setup() method runs once, when the sketch starts
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
randomSeed(analogRead(0));
// initialize the digital pins as an output:
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
}
//////////// LOOP ////////////
void loop()
{
// Serial.println("start loop ");
// Serial.println(ultrasonic.Ranging(CM));
//choseMode();
distance = (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
//////////// REVERSE MODE ////////////
if(distance <= goToReverseDistance){
avoidDirection = avoidDirection * -1;
// while(distance < avoidDistance) {
// Serial.println("start reverse ");
if(avoidDirection == 1){ // turn one way
analogWrite(motorPin3, steeringMotorSpeed);
digitalWrite(motorPin4, LOW);
} else { // ... or the other way
analogWrite(motorPin4, steeringMotorSpeed);
digitalWrite(motorPin3, LOW);
}
analogWrite(motorPin2, reverseSpeed); //rotates motor - backwards
digitalWrite(motorPin1, LOW);
delay(reverseTime); // for time set in reverseTime
// delay(sensorDelay);
distance = (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// }
digitalWrite(motorPin2, LOW); // stop motors
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
avoidDirection = avoidDirection * -1; // switch steering direction
distance = (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
}
////////////////////////////////////////////////
//////////// AVOID MODE ////////////
distance = (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
if(distance < avoidDistance){
// Serial.println("start avoid ");
distance = (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
countDown = avoidCirclesBeforeDirectionChange;
distanceCountDown = distance;
while(distance <= endAvoidDistance && distance > goToReverseDistance){
if(avoidDirection == 1){
analogWrite(motorPin3, steeringMotorSpeed); // turn one way
digitalWrite(motorPin4, LOW);
} else {
analogWrite(motorPin4, steeringMotorSpeed); // or turn the other way
digitalWrite(motorPin3, LOW);
}
analogWrite(motorPin1, avoidSpeed); //rotates motor
digitalWrite(motorPin2, LOW); // set the Pin motorPin2 LOW
delay(sensorDelay);
distance = (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
if(distance < distanceCountDown) { countDown--; } // if obstacle is getting closer - count down to changing direction
if(countDown < 1) {
avoidDirection = avoidDirection * -1; // switch steering direction
countDown = avoidCirclesBeforeDirectionChange;
distanceCountDown = distance;
}
// Serial.println(distance);
// Serial.println(avoidDirection);
} // end while (avoid)
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
////////////////////////////////
//////////// CRUISE MODE ////////////
distance = (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
while(distance > avoidDistance && distance < fullTrottleMinDistance){
analogWrite(motorPin1, cruiseSpeed); //rotates motor
digitalWrite(motorPin2, LOW); // set the Pin motorPin2 LOW
delay(sensorDelay);
distance = (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// Serial.println("cruise");
// Serial.println(distance);
} // end while
//////////// FULL SPEED MODE ////////////
distance = (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
while(distance > fullTrottleMinDistance){
analogWrite(motorPin1, fullTrottleSpeed); //rotates motor
digitalWrite(motorPin2, LOW); // set the Pin motorPin2 LOW
delay(sensorDelay);
distance = (TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
// Serial.println("FULL");
// Serial.println(distance);
} // end while
// Serial.println("REstart loop ");
// Serial.println(distance);
}