Hey Arduino fans, i am completely new to Arduino and so far have only accomplished the small task of flashing 8 LEDs in certain formations, but now its time for the big stuff.
My goal is to build the classic obstacle avoidance robot with the following servos and sensor:
https://www.sparkfun.com/products/9347 (X2)
https://www.sparkfun.com/products/9065 (For the sensor)
https://www.sparkfun.com/products/242 (The sensor)
In the attached files i have included the code i want to use for my robot, this is not my code i found it on another forum page, but it looks like a safe bet and is very straight forward. I do not know how else to attach the code so that it is inside that scroll box thingy help appreciated. Although because i want to be creative-ish; the twist that i want to add to my bot is a 3-axis gyro most likely this one (Triple-Axis Digital-Output Gyro ITG-3200 Breakout - SEN-09801 - SparkFun Electronics) suggestions welcome. This will control the two servos while driving to keep the bot stable and balanced and avoid the dreaded third wheel stabilizer thingy. Only problem is i have no idea how to adjust my code for this and actual circuit suggestions would help as well.
Thanks for taking a look at my post!
#include <Servo.h> //include Servo library
const int RForward = 0;
const int RBackward = 180;
const int LForward = RBackward;
const int LBackward = RForward;
const int RNeutral = 90;
const int LNeutral = 90; //constants for motor speed
const int pingPin = 7;
const int irPin = 0; //Sharp infrared sensor pin
const int dangerThresh = 10; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;
Servo leftMotor;
Servo rightMotor; //declare motors
long duration; //time it takes to recieve PING))) signal
void setup()
{
rightMotor.attach(11);
leftMotor.attach(10);
panMotor.attach(6); //attach motors to proper pins
panMotor.write(90); //set PING))) pan to center
}
void loop()
{
int distanceFwd = ping();
if (distanceFwd>dangerThresh) //if path is clear
{
leftMotor.write(LForward);
rightMotor.write(RForward); //move forward
}
else //if path is blocked
{
leftMotor.write(LNeutral);
rightMotor.write(RNeutral);
panMotor.write(0);
delay(500);
rightDistance = ping(); //scan to the right
delay(500);
panMotor.write(180);
delay(700);
leftDistance = ping(); //scan to the left
delay(500);
panMotor.write(90); //return to center
delay(100);
compareDistance();
}
}
void compareDistance()
{
if (leftDistance>rightDistance) //if left is less obstructed
{
leftMotor.write(LBackward);
rightMotor.write(RForward); //turn left
delay(500);
}
else if (rightDistance>leftDistance) //if right is less obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn right
delay(500);
}
else //if they are equally obstructed
{
leftMotor.write(LForward);
rightMotor.write(RBackward); //turn 180 degrees
delay(1000);
}
}
long ping()
{
// Send out PING))) signal pulse
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//Get duration it takes to receive echo
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
//Convert duration into distance
return duration / 29 / 2;
}
oh thats how
sketch_aug25a.ino (2.15 KB)