I been going over the code in my head and I thought that this should end up working.
//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;
float cmMsec;
void setup()
{
myservo1.attach(10,400,2400);
myservo2.attach(9,400,2400);
myservo3.attach(11);
}
void loop()
{
do
{
float cmMsec;
long microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert(microsec, Ultrasonic::CM);
Serial.print(", CM: ");
Serial.print(cmMsec);
delay(200);
}
while(cmMsec<15);
{
myservo1.writeMicroseconds(1400); //FORWARD
myservo2.writeMicroseconds(1600);
delay(1000);
for(pos = 0; pos < 180; 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(100); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo3.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 15ms for the servo to reach the position
}
if(cmMsec>=15);
{
myservo1.writeMicroseconds(1600); //BACKWARD
myservo2.writeMicroseconds(1400);
delay(2000);
myservo1.writeMicroseconds(1400); //LEFT
myservo2.writeMicroseconds(1400);
delay(2000);
}}}
My servos aren't rotating at all now. I have no idea why. This code seems like it should work.