Car Project

Please help! We're trying to build a car while using a bluetooth module HC-05, ultrasonic sensor and servo to test all the angles and a motor driver. This is my code and it's not working. It might have something to do with the user defined functions?
thanks!

the motors don't work when running this code, so that's why we think it might have something to do with how we pass values. Each part works independently.

//Libraries
#include <Servo.h>

Servo myservo_test; //Class to manipulate servo's

//Variable Initialization
String voice; // initialize the inputs and motors to arduino pins
int pos=0; //initialize angle to 0, this is the position of the servo

//Initialization of Motor Variables

const int enA = 9;
const int enB = 3;

const int in1 = 7;
const int in2 = 6;
const int in3 = 5;
const int in4 = 4;

//Initialization of Ultrasonic Sensor Pins & Variables

const int echo = 10;//Echo pins is responsible to hear the sound
const int trig = 8; //The trigger of sound, basically when it's on low it's off but when it's on high it will emit a sound until off
double distance; //distance will be in millimeters, should be double to allow for decimals
double time; //double to allow for decimals

//USER DEFINED FUNCTIONS

void UltraFunc(String& direct) //Returns values of direct to voice, must be a different variable name because voice doesn't exist in the scope of this void function
{
double sum=0;
double average=0;

for (int i =1; i <=3; i++)
{
digitalWrite(trig, LOW); //Reinitializes trig to LOW which is the value for OFF or false
delay(2); // Turns trig off for two seconds
digitalWrite(trig, HIGH);
delay(20); // turns it back on for 10 seconds
digitalWrite(trig, LOW); // turns trig back off
time = pulseIn(echo, HIGH); // turns on the echo pin which hears for the sound
// Calculating the distance by finding the speed that sound travels and **continue later
distance= time*0.1715; //Because 343m/s is the speed of sound, and the distance is divided by 2 because it is the time it takes for one sound emitted to hit the wall and come back, and it is in mm. The sound is divided by 2 because the sound is emitted by the TRIG pin for 2 seconds, every ten seconds.
Serial.print("Test "); //3 tries
Serial.print (i);
Serial.print(", Distance: ");
Serial.print(distance); // this will print the distance
Serial.println(" mm");
sum=sum+distance;// summation all of distances
average=sum/(double)i;//average to eliminate inaccuracy

}//Tests 3 times to see if there's anything in front in case of error // end of for-loop

if (average <= 1700) //if the distance between the car and some object less than 1700mm, or 17cm (which is the length of the car is 16cm which is the distance that is required to turn without hitting anything with 1 cm room for error)
direct="stop"; //return command stop so the arduino can re-evaluate

} //end of UltraFunc function

void servoFunc(String& change) //new variable that will change direct if there is anything that is scanned.
{

if (change=="right") // voice = direct= change, if the car is going right then the servo must turn right and scan if anything is turning before turning right. It must check in front of it and in the direction of motion
{
for (pos = 25; pos <= 90; pos ++) //Servo scans between 25 degree position to 90 and back, it stops at 50 degrees too
{
myservo_test.write(pos);
delay(15);
UltraFunc(change);
}

for (pos = 90; pos <= 25; pos --) //Servo scans between 25 degree position to 90 and back, it stops at 50 degrees too
{
myservo_test.write(pos);
delay(15);
UltraFunc(change);
}

} // end of if right statement

if (change=="left") // voice = direct= change, if the car is going right then the servo must turn right and scan if anything is turning before turning left. It must check in front of it and in the direction of motion
{
for (pos = 100; pos <= 145; pos ++)
{
myservo_test.write(pos);
delay(15);

UltraFunc(change);
}

} // end of if left
for (pos = 270; pos <= 100; pos --)
{
myservo_test.write(pos);
delay(15);

UltraFunc(change);
}

if (change == "stop") //If at any point during the rotations there was something within 1700mm then the car will stop.
Serial.println("There is something in the way");

}//end of servo void function

void setup()
{
//Sets the motors as output
pinMode(enA, OUTPUT); //set motor control pins to outputs, motor 1
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);

pinMode(enB, OUTPUT); //motor 2
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);

//Sets the Ultrasonic Sensor echo and trig as output and/or input values
pinMode(trig, OUTPUT); //It sets a pin to be either an Input or Output, where here trig is output
pinMode(echo, INPUT); // Sets the echo as an Input

//Initialization of Servo
myservo_test.attach(12); // attach the signal pin of servo to pin 12 of arduino // attach is found in the library for servo so that the servo is connected to those pins
//Wiring: Red wire VCC, Brown GRND, Orange 12

Serial.begin(9600);// Starts the communication with the serial monitor at 9600 bits per second-baud value, must be 9600 because that is the baud value of this arduino Uno kit

}

void loop()
{

//Receives audio input from HC-05 Bluetooth module connected to Android phone, it can also receive commands by a terminal on the phone

if(Serial.available()) // serial.available tests to seee how many bytes are available
{
voice = Serial.readString();
delay(0.01);
Serial.println(voice);
}

//Scans to see if there is anything in the way

myservo_test.write(90); //Reserts the angle directly infront
UltraFunc(voice); // will scan directly in front
servoFunc(voice); //enters servoFunc and will take voice variable with it. It will test to see if there is anything in front of it to the left or to the right and it will stop the car if there is.

if(voice == "forward"){
analogWrite(enA, 255);
analogWrite(enB, 255);

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

}
else if(voice == "reverse"){
analogWrite(enA, 255);
analogWrite(enB, 255);

digitalWrite(in1, HIGH); // Motor A
digitalWrite(in2, LOW); //Motor A
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);

}

if(voice == "right"){
analogWrite(enA, 255);
analogWrite(enB, 255);

digitalWrite(in1, LOW); // Motor A
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);

}
else if(voice == "left") {
analogWrite(enA, 255);
analogWrite(enB, 255);

digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}

if(voice == "stop"){

digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);

}

} //end of loop

What doesn't work?

If it doesn't compile, post compile error.

If it does compile and upload, but doesn't do what you want, describe the expected behavior and what it does.

OK, first things first. :roll_eyes:

If you want people to look at your code, you need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the "More -> Modify" option below the right hand corner of your post - to mark up your code as such using the "</>" icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a "copy for forum" link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don't forget to use the "Auto-Format" (Ctrl-T) option first to make it easy to read. If you do not post it as "code" it can frequently be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a ".ino" file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.

See Servo reference. Use of the Servo library disables PWM (analogWrite) on pins 9 and 10 in most Arduinos so you'll need to move enA.

Steve

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What model Arduino are you using.
Do you have code that proves you have control of the motors, not by bluetooth?

Tom... :slight_smile: