Hi guys, I really need your help! I have a project to do for school and if I do not succed it, I won't get my diploma...
So my project, is to make a car that can follow an other vehicule and stops when it detects it in a range of 30 cm. If the vehicule turns, my car will have to turn also. When the car turns, a LED flash and when it stops, it ony lights.
So right know, my car is finished. The motor has been tested with arduino,the servomotor and the sensors too. Only the code is remaining. However, we are Thursday and I won't be able to test my car until next Monday... and I really don't have much time left!
Can you please check my code and see if my car will do what I want it to do?
I used two sensors, a Ping and a MaxSonar EZ2. I also used a servomotor.
My idea was that there's a sensor on each side of the car, and if one stops detecting something infront and the other still does, well, the car will turn. If they both don't detect anything in a range of 30 cm, the car advances. If they both detect someting,the car stops.
So, here's my code and tell me what you think about it. I just want to say that arduino didn't detected any errors. Little precison: a bit of my code is in French. moteur= Motor and DEL=LED. And since I'm not very good with Arduino, I used the example of the Ping and of the servo (sweep).
const int pingPin = 7;
int moteur= 3;
#include <Servo.h>
int DEL = 6;
Servo myservo;
int pos = 0;
const int maxsonar = 1;
long anVolt, inches, cm2;
int sum=0;
int avgrange=60;
long duration,cm;
void setup() {
pinMode(moteur, OUTPUT);
pinMode(DEL, OUTPUT);
myservo.attach(9);
pinMode(maxsonar,INPUT);
}
void loop()
{
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
for(int i = 0; i < avgrange ; i++)
{
anVolt = analogRead(maxsonar)/2;
sum += anVolt;
delay(10);
}
inches = sum/avgrange;
cm2 = inches * 2.54;
sum = 0;
delay(500);
if (cm2>30,cm>30 )
{
digitalWrite(moteur,HIGH);
delay(1000);
digitalWrite(moteur,LOW);
delay(500);
digitalWrite(moteur,HIGH);
delay(1000);
digitalWrite(moteur,LOW);
delay(500);
digitalWrite(moteur,HIGH);
delay(1000);
digitalWrite(moteur,LOW);
delay(500);
}
if (cm < 30, cm2 <30 )
{
digitalWrite(moteur,LOW);
delay(500);
digitalWrite(DEL,HIGH);
delay(500);
}
if(cm2>30,cm<30)
{
digitalWrite(moteur, HIGH);
delay(500);
digitalWrite(moteur,LOW);
delay(500);
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(150);
}
digitalWrite(DEL,HIGH);
delay(500);
digitalWrite(DEL,LOW);
delay(500); }
if(cm2<30,cm>30)
{
digitalWrite(moteur, HIGH);
delay(500);
digitalWrite(moteur,LOW);
delay(500);
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}
digitalWrite(DEL,HIGH);
delay(500);
digitalWrite(DEL,LOW);
delay(500);
}
}}
Will it work? Thank you very much! If you have questions, just ask