Hey yaw, im new to arduino but ive been trying to program my rickshaw to avoid obstacles and just keep walking forever. I have four HC-SR04 sensors attached to the front,back,left,right of the rickshaw to detect any object. I also have two servo motors one for moving forwards and backwards and the other for moving left and right.
I've tried building this code but i am getting a compiling error, If anyone can help me out and see where im going wrong with this that would be greatly appreciated
Here's the code so far
Current error(expected initializer before 'servoDrive'
//In this project, we will be using 4 ultrasonic ranging
//sensors to control the direction of the motor and drive of the motor.
#include <Servo.h>
#include <NewPing.h> // don't forget to include the NewPing library.
#define SONAR_NUM 4
Servo servoDrive; // Define left servo
Servo servoDirection; // Define right servo
NewPing SonarFront(13, 12, 400);// In the project, this was the sensor that
//was used on the right side of the breadboard
NewPing SonarLeft(7, 6, 400);// In the project, this was the sensor that
//was used on the left side of the breadboard
NewPing SonarRight(23, 22, 400);// In the project, this was the sensor that
//was used on the right side of the breadboard
NewPing SonarBack(43, 42, 400);// In the project, this was the sensor that
//was used on the left side of the breadboard
void setup() {
servoDrive.attach(18); // Set left servo to digital pin 10
servoDirection.attach(14); // Set right servo to digital pin 9
}
void loop()
{
unsigned int uS1 = SonarFront.ping(); // activates the first sensor
delay(50); // wait 50ms
unsigned int uS2 = SonarBack.ping(); // activate the fourth sensor
delay(50); // wait 50ms
unsigned int uS4 = SonarLeft.ping(); // activate the second sensor
delay(50); // wait 50ms
unsigned int uS3 = SonarRight.ping(); // activates the third sensor
delay(50); // wait 50ms
// We will declare "if" statements to control the relay module,
// which controls the motor.
if (uS1 / US_ROUNDTRIP_CM < 20) { //if an object is less than 20 cm away
//from the front sensor,
void reverse()
servoDrive.write(0);
servoDirection.write(180);
//the motor will rotate forward.
}
if (uS1 / US_ROUNDTRIP_CM > 20) { //if an object is greater than 20 cm away
//from the back sensor,
void forward()
servoDrive.write(180);
servoDirection.write(0);
//the motor will rotate backwards.
}
if (uS2 / US_ROUNDTRIP_CM < 20) { //if an object is less than 20 cm away
//from the back sensor,
void forward()
servoDrive.write(180);
servoDirection.write(0);
//the motor will rotate backwards.
}
if (uS3 / US_ROUNDTRIP_CM < 20) { //if an object is less than 20 cm away
//from the back sensor,
void turnLeft()
servoDrive.write(0);
servoDirection.write(0);
//the motor will rotate left
}
if (uS4 / US_ROUNDTRIP_CM < 20) { //if an object is less than 20 cm away
//from the back sensor,
void turnRight()
servoLeft.write(180);
servoRight.write(180);
//the motor will rotate right
}
}
I know. That's why I highlighted the error for you.
Here it is again
if (uS1 / US_ROUNDTRIP_CM < 20) { //if an object is less than 20 cm away
//from the front sensor,
void reverse()
servoDrive.write(0);
servoDirection.write(180);
//In this project, we will be using 4 ultrasonic ranging
//sensors to control the direction of the motor and drive of the motor.
#include <Servo.h>
#include <NewPing.h> // don't forget to include the NewPing library.
#define SONAR_NUM 4
Servo servoDrive; // Define left servo
Servo servoDirection; // Define right servo
NewPing SonarFront(13, 12, 400);// In the project, this was the sensor that
//was used on the right side of the breadboard
NewPing SonarLeft(7, 6, 400);// In the project, this was the sensor that
//was used on the left side of the breadboard
NewPing SonarRight(23, 22, 400);// In the project, this was the sensor that
//was used on the right side of the breadboard
NewPing SonarBack(43, 42, 400);// In the project, this was the sensor that
//was used on the left side of the breadboard
void setup() {
servoDrive.attach(18); // Set left servo to digital pin 10
servoDirection.attach(14); // Set right servo to digital pin 9
}
void loop()
{
unsigned int uS1 = SonarFront.ping(); // activates the first sensor
delay(50); // wait 50ms
unsigned int uS2 = SonarBack.ping(); // activate the fourth sensor
delay(50); // wait 50ms
unsigned int uS3 = SonarRight.ping(); // activates the third sensor
delay(50); // wait 50ms
unsigned int uS4 = SonarLeft.ping(); // activate the second sensor
delay(50); // wait 50ms
// We will declare "if" statements to control the relay module,
// which controls the motor.
if (uS1 / US_ROUNDTRIP_CM < 20) { //if an object is less than 20 cm away
//from the front sensor,
servoDrive.write(0);
servoDirection.write(180);
//the motor will rotate forward.
}
if (uS1 / US_ROUNDTRIP_CM > 20) { //if an object is greater than 20 cm away
//from the Front sensor,
servoDrive.write(180);
servoDirection.write(0);
//the motor will rotate backwards.
}
if (uS2 / US_ROUNDTRIP_CM < 20) { //if an object is less than 20 cm away
//from the back sensor,
servoDrive.write(180);
servoDirection.write(0);
//the motor will rotate backwards.
}
if (uS3 / US_ROUNDTRIP_CM < 20) { //if an object is less than 20 cm away
//from the right sensor,
servoDrive.write(0);
servoDirection.write(0);
//the motor will rotate left
}
if (uS4 / US_ROUNDTRIP_CM < 20) { //if an object is less than 20 cm away
//from the left sensor,
servoLeft.write(180);
servoRight.write(180);
//the motor will rotate right
}
}