I am working on a project using the Arduino Duemilanove, make/mshield(motor shield), 2 continuous parallax servos, 1 T pro 9gram 180 degree servo, and a HC-SR04 sonar sensor. The 2 parallax continuous servos are used to make the car move left, right, backward, and forward. The HC-SR04 sonar sensor is mounted on top of the rotating(right to left) T pro 9gram 180 degree servo. I want the car to avoid objects. When detecting an object I would like the car to go backward and then make a left to switch its path. So far I can get the car to move forward until it senses an object on its right hand side(within 25cm), it then moves back and moves to its left. I can't get the sensor to measure values while the T pro 9gram 180 degree servo is rotating, only while it's at degree 0. Another problem is that when it does detect an object on its right hand side, it makes a left for more than the time allotted(In the first code). In the second code I can get the car to do the same task except that now it does return measurements, except that they are all identical to the first measurement. I've tried the first code with many adjustments along the way leading to the second code posted and so far I think that maybe whoever reads this post can work with these codes. I would like for someone to please tell me how to also get the T pro 9gram 180 degree servo to rotate while the HC-SR04 sensor gives readings throughout its rotation, therefore alerting the program that there may be on object at some angle other than 0,90, or 180 all while always moving forward until an object is detected.
//Arduino sonar car code 1
#include <Ultrasonic.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 13
Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);
#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
int pos = 0;
void setup()
{
Serial.begin(9600);
myservo1.attach(10,544,2400);
myservo2.attach(9,544,2400);
myservo3.attach(11);
}
void loop()
{ float cmMsec, inMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
Serial.print(", CM: ");
Serial.print(cmMsec);
delay(200);
for(pos = 0; pos < 180; pos += 1)
{
myservo3.write(pos);
delay(5);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo3.write(pos);
delay(5);
}
if(cmMsec<15)
{
myservo1.writeMicroseconds(1600); //BACKWARD
myservo2.writeMicroseconds(1400);
delay(2000);
myservo1.writeMicroseconds(1400); //LEFT
myservo2.writeMicroseconds(1400);
delay(2000);
}
else
{
myservo1.writeMicroseconds(1400); //FORWARD
myservo2.writeMicroseconds(1600);
delay(50);
}
}
and
//Arduino sonar car
#include <Ultrasonic.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 13
Ultrasonic ultrasonic(TRIGGER_PIN, ECHO_PIN);
#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
int pos = 0;
void setup()
{
Serial.begin(9600);
myservo1.attach(10,544,2400);
myservo2.attach(9,544,2400);
myservo3.attach(11,250,1200);
}
void loop()
{
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo3.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
float cmMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
Serial.print(", CM: ");
Serial.print(cmMsec);
delay(200);
if(cmMsec>25)
{
//FORWARD
myservo1.writeMicroseconds(1400);
myservo2.writeMicroseconds(1600);
delay(500);
}
if(cmMsec<25)
{ //BACKWARD
myservo1.writeMicroseconds(1600);
myservo2.writeMicroseconds(1400);
delay(4000);
myservo1.writeMicroseconds(1400); //LEFT
myservo2.writeMicroseconds(1400);
delay(200);
}
delay(2000);
for(pos = 90; pos < 180; pos += 1) // goes from 180 degrees to 0 degrees
{
myservo3.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
Serial.print(", CM: ");
Serial.print(cmMsec);
delay(200);
if(cmMsec>25)
{ //FORWARD
myservo1.writeMicroseconds(1400);
myservo2.writeMicroseconds(1600);
delay(500);
}
if(cmMsec<25)
{ //BACKWARD
myservo1.writeMicroseconds(1600);
myservo2.writeMicroseconds(1400);
delay(4000);
myservo1.writeMicroseconds(1400); //LEFT
myservo2.writeMicroseconds(1400);
delay(200);
}
delay(2000);
for(pos = 180; pos > 90; pos -= 1) // goes from 180 degrees to 0 degrees
{
myservo3.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
Serial.print(", CM: ");
Serial.print(cmMsec);
delay(200);
// waits 15ms for the servo to reach the position
if(cmMsec>25)
{//FORWARD
myservo1.writeMicroseconds(1400);
myservo2.writeMicroseconds(1600);
delay(500);
}
if(cmMsec<25)
{
myservo1.writeMicroseconds(1600); //BACKWARD
myservo2.writeMicroseconds(1400);
delay(4000);
myservo1.writeMicroseconds(1400); //LEFT
myservo2.writeMicroseconds(1400);
delay(200);
}
delay(2000);
for(pos = 90; pos > 0; pos -= 1) // goes from 180 degrees to 0 degrees
{
myservo3.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
Serial.print(", CM: ");
Serial.print(cmMsec);
delay(200);
if(cmMsec>25)
{
//FORWARD
myservo1.writeMicroseconds(1400);
myservo2.writeMicroseconds(1600);
delay(500);
}
if(cmMsec<25)
{
myservo1.writeMicroseconds(1600); //BACKWARD
myservo2.writeMicroseconds(1400);
delay(4000);
myservo1.writeMicroseconds(1400); //LEFT
myservo2.writeMicroseconds(1400);
delay(200);
}
delay(1000);
}