Hello so basically i started with arduino bought a cheap chinese kit online, and well put it together. After putting together a script, I uploaded it to my creation. And now my robot spins around in circles endlessly but avoid anything i put in front of it. So my question is was the code shoddy as hell or perhaps its a wiring problem.
#include <Servo.h> //Servo motor library. This is standard library
#include <NewPing.h> //Ultrasonic sensor function library. You must install this library
//our L298N control pins
const int LeftMotorForward = 5; // left motor foward
const int LeftMotorBackward = 4;// left motor back
const int RightMotorForward = 3; // right motor foward
const int RightMotorBackward = 2; // right motor backward
//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2
#define maximum_distance 100 // in cm
boolean goesForward = false;
int distance = 50; // changed it to 50 since...well.... my apartment is small
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor; //our servo name
void setup() {
pinMode(RightMotorForward, OUTPUT); // pinmode selecs the pin, then we write watever is the distance
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
servo_motor.attach(11); //our servo pin position on the arduino board
servo_motor.write(90);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop() {
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 20) { // if distance is less than 20
moveStop(); // we stop
delay(300); //wait
moveBackward(); // go backwards
delay(400); // wait
moveStop(); // stop
delay(300); //wait
distanceRight = lookRight(); // look to the right and record distance
delay(300); // wait
distanceLeft = lookLeft(); // look to the left then record distance
delay(300); // wait
if (distance >= distanceLeft) { // if distance (50) is greater than the one on left we turn right then stop
turnRight();
moveStop();
}
else {
turnLeft(); // else we move left
moveStop();
}
}
else {
moveForward(); // if neither we move foward
}
distance = readPing(); // then we ping and that becomes the new distance
}
// =================================================end of main============================================================================================================
int lookRight() { // to look left
servo_motor.write(10); // servo_motor.write(10), where 10 equals a positon (position 10-90-170 or right middle left)
delay(500); // wait
int distance = readPing(); // set the distance to whatever the new ping is
delay(100); // wait
servo_motor.write(90); // then set motor to default location which is 10
return distance; // return distance
delay(100); // wait
}
int lookLeft() { // to look right
servo_motor.write(170); // set it to positon 170 or left
delay(500); // wait
int distance = readPing(); // set ping as new distance
delay(100); // wait
servo_motor.write(90); //set to middle
return distance; // return distance
delay(100); //wait
}
int readPing() { // read ping
delay(70); // wait
int cm = sonar.ping_cm(); // set distance of ping in cm
if (cm == 0) {
cm = 250;
}
return cm;
}
//=========================================Movement===========================================================================================================
// low= nothing, high equals move
void moveStop() {
digitalWrite(RightMotorForward, LOW); // we write for the robot to stop therefore all are low
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
void moveForward() {
if (!goesForward) {
goesForward = true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}
void moveBackward() {
goesForward = false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
void turnRight() {
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
void turnLeft() {
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
The only information we have about your robot is that it is capable of spinning round in circles (not even if it's turning left or right). A little more information might be useful like what motors / motor driver, how it's powered and wired.
But I'd say your best bet is probably to put some Serial.prints in strategic positions and connect the USB cable to see in serial monitor where the code gets to, because it probably isn't where you think. Hold the robot in the air so it doesn't spin round or run away.