hi could someone please help,
i am making a robot that manourves away from obstacles, my first code which makes the robot move forwards and if anything was in the way it will stop, then rotates the servo from left to right, but now that i want it to turn left or right it wouldn't work, can some on please help me
as the teacher know that i have just started this, i would need a basic code: the last code
the first code is the drive forward and stop if obstacle detected
const int pingPin =8 ;
int motorpin1 = 3; //define digital output pin no.
int motorpin2 = 4; //define digital output pin no.
int motorpin3 = 5;
int motorpin4 = 6;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0;
void setup() // function which runs once
{
Serial.begin(9600); // initialize the data rate in bits per second
myservo.attach(9);
}
void loop() // function which will run continuously
{
long duration, inches, cm; // function from datasheet of ping sensor
pinMode(pingPin,OUTPUT); //
digitalWrite(pingPin,LOW);
delayMicroseconds(2); // delay recomended from datasheet
digitalWrite(pingPin,HIGH);
delayMicroseconds(5); // delay recomended from datasheet
digitalWrite(pingPin,LOW);
pinMode(pingPin,INPUT);
duration =pulseIn(pingPin,HIGH);
inches = microsecondsToInches(duration); // converting time into distance
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
{
if (inches <= 2) // if there is an obstacle at 15 inches or below
{
digitalWrite(motorpin1,LOW);
digitalWrite(motorpin2,LOW);
digitalWrite(motorpin3, LOW);
digitalWrite(motorpin4, LOW);
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
}
else
{
digitalWrite(motorpin1,LOW);
digitalWrite(motorpin2,HIGH);
digitalWrite(motorpin3,HIGH);
digitalWrite(motorpin4,LOW);
}
}
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
and this is meant to be the final code, please help me =(
#include <Servo.h> //includes the servo library
int motorPin1 = 3;
int motorPin2 = 4;
int motorPin3 = 5;
int motorPin4 = 6;
int servoPin = 9;
const int pingPin = 8;
const int dist = 0;
int leftdist = 0;
int rightdist = 0;
int object = 10; //distance at which the robot should look for another route
Servo myservo;
void setup ()
{
Serial.begin(9600); // initialize the data rate in bits per second
myservo.attach(9);
pinMode(motorPin1,OUTPUT);
pinMode(motorPin2,OUTPUT);
pinMode(motorPin3,OUTPUT);
pinMode(motorPin4,OUTPUT);
myservo.attach(servoPin);
myservo.write(90);
delay(700);
}
void loop()
{
if(dist < object) { //if distance is less than 5
forward(); //then move forward
}
if(dist >= object) { //if distance is greater than or equal to 5
findroute();
}
}
void forward() {
digitalWrite(motorPin1,HIGH );
digitalWrite(motorPin2,LOW);
digitalWrite(motorPin3,HIGH);
digitalWrite(motorPin4,LOW);
return;
}
void findroute() {
halt(); // stop
reverse(); //go backwards
lookleft(); //go to subroutine lookleft
lookright(); //go to subroutine lookright
if ( leftdist < rightdist )
{
turnleft();
}
else
{
turnright ();
}
}
void backward() {
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,HIGH);
digitalWrite(motorPin3,LOW);
digitalWrite(motorPin4,HIGH);
delay(500);
halt();
return;
}
void halt () {
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,LOW);
digitalWrite(motorPin3,LOW);
digitalWrite(motorPin4,LOW);
delay(500); //wait after stopping
return;
}
void lookleft() {
myservo.write(150);
delay(700); //wait for the servo to get there
leftdist = digitalRead(pingPin);
myservo.write(90);
delay(700); //wait for the servo to get there
return;
}
void lookright () {
myservo.write(30);
delay(700); //wait for the servo to get there
rightdist = digitalRead(pingPin);
myservo.write(90);
delay(700); //wait for the servo to get there
return;
}
void turnleft () {
digitalWrite(motorPin1,HIGH); //use the combination which works for you
digitalWrite(motorPin2,LOW); //right motor rotates forward and left motor backward
digitalWrite(motorPin3,LOW);
digitalWrite(motorPin4,HIGH);
delay(1000); // wait for the robot to make the turn
halt();
return;
}
void turnright () {
digitalWrite(motorPin1,LOW); //use the combination which works for you
digitalWrite(motorPin2,HIGH); //left motor rotates forward and right motor backward
digitalWrite(motorPin3,HIGH);
digitalWrite(motorPin4,LOW);
delay(1000); // wait for the robot to make the turn
halt();
return;
}
long duration, inches, cm; // function from datasheet of ping sensor
pinMode(pingPin,OUTPUT); //
digitalWrite(pingPin,LOW);
delayMicroseconds(2); // delay recomended from datasheet
digitalWrite(pingPin,HIGH);
delayMicroseconds(5); // delay recomended from datasheet
digitalWrite(pingPin,LOW);
pinMode(pingPin,INPUT);
duration =pulseIn(pingPin,HIGH);
inches = microsecondsToInches(duration); // converting time into distance
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
please please will need some one to help or tell me where im going wrong