Greetings, So im building an arduino robotic car which senses an object an avoid
ID: 660968 • Letter: G
Question
Greetings,
So im building an arduino robotic car which senses an object an avoid obsticles also, it can be controlled remotely using an Bluetooth slave HC-06. Everything works but the complete code. Inteads of going straight it keeps stopping after a certain distance and going another directions without me pressing anything continuing doing the same thing over and over. I want it to go forward how is suppose to without stopping. Any help would be graceful.
// value used to control car from android app
char val;
// pin declaration for hardware
const int trigPin = A0;
const int echoPin = A1;
//Arduino PWM Speed Control?
int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
// setup function that runs once on bootup
void setup()
{
// set baudrate to 9600 (rate of communication from terminal)
Serial.begin(9600);
// setup of digital pins to either INPUT or OUTPUT
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
// function that consistantly runs after setup
void loop()
{
// variables for time and distance long because they can be high numbers
long timeLength, distance;
// sends noise to detect distance from object with the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds( 5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// calculate distance with time
timeLength = pulseIn(echoPin, HIGH);
// converts to inches cause we in america
distance = msInches(timeLength);
// prints distance to terminal (trouble shooting)
Serial.print(distance);
Serial.print("in, ");
Serial.println();
if (distance<5)
dodge();
else
drive();
delay(100);
}
// function that calculates and returns distance in inches
long msInches(long ms)
{
// According to Parallax's datasheet for the Ping sensor, there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second).
return ms / 74 / 2;
}
// function used to drive the car remotely
void drive()
{
// turn on green led
digitalWrite(13,HIGH);
// read value from terminal
if( Serial.available() > 0) // if data is available to read
{
val = Serial.read(); // read it and store it in 'val'
}
// go up
if (val == 'i')
{
digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1,200);
analogWrite(E2,200);
Serial.print("up ");
}
// go down
if (val == 'k')
{
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
analogWrite(E1,200);
analogWrite(E2,200);
Serial.print("DOWN ");
}
// go left
if (val == 'j')
{
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1,120);
analogWrite(E2,120);
Serial.print("left ");
}
// go right
if (val == 'l')
{
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
analogWrite(E1,120);
analogWrite(E2,120);
Serial.print("right ");
}
// stop
if (val == 's')
{
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1,0);
analogWrite(E2,0);
Serial.print("stop");
}
}
// function used to dodge objects
void dodge()
{
// turn off green led
digitalWrite(13,LOW);
// turn on red led
digitalWrite(12,HIGH);
// stop car
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1,0);
analogWrite(E2,0);
Serial.print("stop");
delay(1000);
// go back
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
analogWrite(E1,200);
analogWrite(E2,200);
Serial.print("DOWN ");
delay(500);
// go left
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1,150);
analogWrite(E2,150);
Serial.print("left ");
//turn off red
digitalWrite(12,LOW);
delay(500);
// variables for time and distance long because they can be high numbers
}
Explanation / Answer
char val;
// pin declaration for hardware
const int trigPin = A0;
const int echoPin = A1;
//Arduino PWM Speed Control?
int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
// setup function that runs once on bootup
void setup()
{
// set baudrate to 9600 (rate of communication from terminal)
Serial.begin(9600);
// setup of digital pins to either INPUT or OUTPUT
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
// function that consistantly runs after setup
void loop()
{
// variables for time and distance long because they can be high numbers
long timeLength, distance;
// sends noise to detect distance from object with the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds( 5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// calculate distance with time
timeLength = pulseIn(echoPin, HIGH);
// converts to inches cause we in america
distance = msInches(timeLength);
// prints distance to terminal (trouble shooting)
Serial.print(distance);
Serial.print("in, ");
Serial.println();
if (distance<25)
dodge();
else
drive();
delay(100);
}
// function that calculates and returns distance in inches
long msInches(long ms)
{
// According to Parallax's datasheet for the Ping sensor, there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second).
return ms / 74 / 2;
}
// function used to drive the car remotely
void drive()
{
// turn on green led
digitalWrite(13,HIGH);
// read value from terminal
if( Serial.available() > 0) // if data is available to read
{
val = Serial.read(); // read it and store it in 'val'
}
// go up
if (val == 'i')
{
digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1,200);
analogWrite(E2,200);
Serial.print("up ");
}
// go down
if (val == 'k')
{
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
analogWrite(E1,200);
analogWrite(E2,200);
Serial.print("DOWN ");
}
// go left
if (val == 'j')
{
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1,120);
analogWrite(E2,120);
Serial.print("left ");
}
// go right
if (val == 'l')
{
digitalWrite(M1,LOW);
digitalWrite(M2, HIGH);
analogWrite(E1,120);
analogWrite(E2,120);
Serial.print("right ");
}
// stop
if (val == 's')
{
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1,0);
analogWrite(E2,0);
Serial.print("stop");
}
}
// function used to dodge objects
void dodge()
{
// turn off green led
digitalWrite(13,LOW);
// turn on red led
digitalWrite(12,HIGH);
// stop car
digitalWrite(M1,LOW);
digitalWrite(M2, LOW);
analogWrite(E1,0);
analogWrite(E2,0);
Serial.print("stop");
delay(1000);
// go back
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
analogWrite(E1,200);
analogWrite(E2,200);
Serial.print("DOWN ");
delay(500);
// go left
digitalWrite(M1,HIGH);
digitalWrite(M2, LOW);
analogWrite(E1,150);
analogWrite(E2,150);
Serial.print("left ");
//turn off red
digitalWrite(12,LOW);
delay(500);
// variables for time and distance long because they can be high numbers
}
this program is used to get the output what you what
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.