i didnt know waht to name this post
well...
here is my problem
i wanted to do a obstacle avoiding robo a few months back and finished it (using the ping ) just simple turn away
but now i also want toi have a servo hookedup and make it look to theright and left
here is the code i am using
/////obstacle avoiding //////
#include <Servo.h>
const int pingPin = 10;
#define ledpin_right 13
#define ledpin_left 12
int motor_ONE_pin_ONE=3;
int motor_ONE_pin_TWO=4;
int motor_TWO_pin_ONE=5;
int motor_TWO_pin_TWO=6;
int pos = 0 ;
Servo myservo;
long dist_right =0;
long dist_left = 0;
void setup()
{
myservo.attach(9);
Serial.begin(9600);
pinMode(ledpin_right,OUTPUT);
pinMode(motor_ONE_pin_ONE,OUTPUT);
pinMode(motor_ONE_pin_TWO,OUTPUT);
pinMode(motor_TWO_pin_ONE,OUTPUT);
pinMode(motor_TWO_pin_TWO,OUTPUT);
}
////LOOP////
void loop ()
{
long duration, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
///servo neck centering///
myservo.write(80);
if (cm > 10 )
{
digitalWrite(motor_ONE_pin_ONE,HIGH);
digitalWrite(motor_TWO_pin_ONE,LOW);
digitalWrite(motor_ONE_pin_TWO,HIGH);
digitalWrite(motor_TWO_pin_TWO,LOW);
}
else if (cm < 10 )
{
drive_stop();
myservo.write(10);
delay(150);
long duration, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
dist_left = microsecondsToCentimeters(duration);
Serial.print(dist_right);
Serial.print("cm right");
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
dist_right = microsecondsToCentimeters(duration);
myservo.write(180);
delay(150);
Serial.print(dist_left);
Serial.print("cmleft");
if(((dist_right)&&(dist_left))<(10))
{
digitalWrite(ledpin_left,LOW);
digitalWrite(ledpin_right,LOW);
drive_360();
}
else if((dist_right)<(dist_left))
{
digitalWrite(ledpin_left,HIGH);
digitalWrite(ledpin_right,LOW);
drive_left();
}
else ((dist_right)>(dist_left));
{
digitalWrite(ledpin_left,LOW);
digitalWrite(ledpin_right,HIGH);
drive_right();
}
}
}
//DRIVE and other functions//
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
void drive_forward()
{
digitalWrite(motor_ONE_pin_ONE,HIGH);
digitalWrite(motor_TWO_pin_ONE,LOW);
digitalWrite(motor_ONE_pin_TWO,HIGH);
digitalWrite(motor_TWO_pin_TWO,LOW);
return;
}
void drive_stop()
{
digitalWrite(motor_ONE_pin_ONE,LOW);
digitalWrite(motor_TWO_pin_ONE,LOW);
digitalWrite(motor_ONE_pin_TWO,LOW);
digitalWrite(motor_TWO_pin_TWO,LOW);
return;
}
void drive_right()
{
digitalWrite(motor_ONE_pin_ONE,HIGH);
digitalWrite(motor_TWO_pin_ONE,LOW);
digitalWrite(motor_ONE_pin_TWO,LOW);
digitalWrite(motor_TWO_pin_TWO,LOW);
delay(500);
return;
}
void drive_left()
{
digitalWrite(motor_ONE_pin_ONE,LOW);
digitalWrite(motor_TWO_pin_ONE,LOW);
digitalWrite(motor_ONE_pin_TWO,HIGH);
digitalWrite(motor_TWO_pin_TWO,LOW);
delay(500);
return;
}
void drive_360()
{
digitalWrite(motor_ONE_pin_ONE,HIGH);
digitalWrite(motor_TWO_pin_ONE,LOW);
digitalWrite(motor_ONE_pin_TWO,LOW);
digitalWrite(motor_TWO_pin_TWO,LOW);
delay(500);
return;
}
now the problem is that it until it detects a obstacle it keeps quiet i.e dosent move an inch
and even after detecting a obstacle it looks in one direction (180 degrees of servo )
and then rotates one motor only
i dont know what i did wrong but i guess its the coding part