Hello, noob here...I have hacked together some of the example code with a small amount of success. I can individually get the functionality I want but when I put it together (obviously incorrectly) I get bad results.
I am trying to control the following:
Ping sensor
servo to rotate the Ping
steering servo
Novak ESC
You can probably guess what I'm trying to do. Here is my code so far.
#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
int pos = 0;
int pingPin = 6; // Ping Pin
const int ledPin = 13; // On-board LED
void setup()
{
Serial.begin(9600);
pinMode (ledPin, OUTPUT);
myservo1.attach(3); // Steering Servo
myservo2.attach(5); // ESC Output Pin
myservo3.attach(9); // Ping Servo
}
void loop()// run over and over again
{
{
myservo2.writeMicroseconds(1490); // PWM to ESC...adjust this number between 1475 and 2100 for speed
}
{
for(pos = 50; pos < 110; pos += 1) // goes from 50 degrees to 110 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
}
for(pos = 110; pos>=50; pos-=1) // goes from 110 degrees to 50 degrees
{
myservo3.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
long duration, inches, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
if (inches >= 24) { // Distance to trigger Left Steer
digitalWrite (ledPin, HIGH);
pos=90; // Steer Straight
myservo1.write(pos);
}
else {
digitalWrite (ledPin, LOW);
pos=135; // Steer Left
myservo1.write(pos);
}
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 73.746 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
I know it's crude but I'm learning.
The problem is the Ping only "pings" as it turns the Ping Servo to the 50 degree posistion (instead of it's normal 10 times a sec). I'm sure it has to do with the brackets or formatting.
Eventually I want the steering servo to turn the oposite direction of a detected object. I haven't gotten that sophisticated yet.
Any help or suggestions are appreciated.