autonomous toy car
Basically my project is a car with a distance finder attached to the front. it is rotated side to side and finds the distance of the closest object. When said crossest object comes within 8 inches of the car it will reverse and turn away from the object.
My car works with a distance sensor which is attached to a servo motor which rotates to allow maximum field of view for the sensor. The code for the motors is basic, it says if object< 8 inches then reverse and turn if not continue to go forwards. I also added an if statement to if object< 8 which reads what direction the object detected is and then reverses and turns away from that side.
The servo motor which rotates the distance senor is set to only rotate between 130 degrees and 40 degrees. I also have the serial monitor set up to give the exact distance of the closest object to the distance sensor.
This is a video of my car in action
https://www.youtube.com/watch?v=6dU_tAIY9S4
This is a video of my code
https://www.youtube.com/watch?v=pjddUQKHJnw
Problems I faced
There were many problems I faced. The motors did not run at the same speed when not on a full battery. My first 3D printed holder for my distance finder didn't work. I used a different motor shield at first and had to switch over to this new one so i could use my encoders. Then i didn't end up using the encoders. Many more problems happened but i always got past them.
What I would have done with more time.
I would have setup and got my encoders working so even on a low battery my car would drive in a strait line.
This is my code
//MOTORS
int RF = 8;
int RB = 7;
int LF = 6;
int LB = 5;
int speedr = 9;
int speedl = 10;
//DISTANCE FINDER
const int pwPin = 13;
long pulse, inches, cm;
//SERVO
int servo = 2;
int heading = 90;
int dir = -5;
void setup() {
//DISTANCE
Serial.begin(9600);
//SERVO
pinMode(2, OUTPUT);
//MOTOR
pinMode(RF,OUTPUT);
pinMode(RB,OUTPUT);
pinMode(LF,OUTPUT);
pinMode(LB,OUTPUT);
pinMode(speedr,OUTPUT);
pinMode(speedl,OUTPUT);
digitalWrite(speedr,HIGH);
digitalWrite(speedl,HIGH);
analogWrite (speedl, 255);
analogWrite (speedr, 255);
}
void loop() {
//DISTANCE
pinMode(pwPin, INPUT); //uses distance finder as imput
pulse = pulseIn(pwPin, HIGH);
inches = pulse / 147; //147uS per inch
cm = inches * 2.54; //change inches to centimetres
Serial.print(inches);// prints data from distance sensor
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(1);
servoPulse(heading);
delay(5);
// read sensor
heading+=dir;
if (heading > 130 || heading <40)// the angle the servo monator is restricted to
{
dir=dir*-1;
}
if(inches <= 8){ //runs if distase is less than 8 inches
if(heading <= 90){ //if servo is pointing right then run this
backwardsR (700);
}
else{ //if servo is pointing left then run this
backwardsL (700);
}
}
else{
forwards(); //what runs if distance is more than 8 inches
}
}
void servoPulse (int angle) // controls servo
{
int pwm = (angle*11) + 500; // Convert angle to microseconds
digitalWrite(2, HIGH);
delayMicroseconds(pwm);
digitalWrite(2, LOW);
delay(50); // Refresh cycle of servo
}
void backwardsR (int sec){// runs if less than 8 inches on left side
digitalWrite(RF,LOW);
digitalWrite(LF, LOW);
digitalWrite(RB,HIGH);
digitalWrite(LB,HIGH);
delay(sec);
digitalWrite(RB,LOW);
delay(sec);
digitalWrite(LB,LOW);
}
void forwards(){
digitalWrite(RF,HIGH);
digitalWrite(LF,HIGH);
}
void backwardsL(int sec){ // rus if less than 8 inches on right side
digitalWrite(RF,LOW);
digitalWrite(LF, LOW);
digitalWrite(LB,HIGH);
digitalWrite(RB,HIGH);
delay(sec);
digitalWrite(LB,LOW);
delay(sec);
digitalWrite(RB,LOW);
}
No comments:
Post a Comment