im workin on rc car project. and ive make a code to control the car by mobile via bluetooth module.
ive made a simple code and it worked well but when i tried to make it like a function and call it in void loop. it didnt work and i tried to read alot of examples but didnt solve anythin ...... i need help on that
sketch_feb09a_bluetooth.ino (1.6 KB)
Your code like I should have been posted. (Please have a look at Read this before posting a programming question.)
char junk;
String state = "";
void setup() {
Serial.begin(9600); // set the baud rate to 9600, same should be of your Serial Monitor
pinMode(13, OUTPUT); //A1
pinMode(12, OUTPUT); //A2
pinMode(11, OUTPUT); //B1
pinMode(10, OUTPUT); //B2
}
void loop()
{
if (Serial.available())
{
while (Serial.available())
{
char inChar = (char)Serial.read(); //read the input
state = inChar; //make a string of the characters coming on serial
}
Serial.println(state);
while (Serial.available() > 0)
{
junk = Serial.read() ; // clear the serial buffer
}
if (state == "f")
{ //in case of 'f' turn DC motor forward
car_forward();
}
else if (state == "b") //in case of 'b' turn DC motor backword
{
car_backword();
}
else if (state == "l") //in case of 'l' turn wheels to left
{
car_left();
}
else if (state == "r") //in case of 'r' turn wheels to right
{
car_right();
}
else if (state == "s")
{
motor_stop();
}
}
}
void motor_stop()
{
digitalWrite(13, 0);
digitalWrite(12, 0);
digitalWrite(11, 0);
digitalWrite(10, 0);
}
void car_forward()
{
digitalWrite(13, 1);
digitalWrite(12, 0);
}
void car_backword()
{
digitalWrite(13, 0);
digitalWrite(12, 1);
}
void car_right()
{
digitalWrite(11, 1);
digitalWrite(10, 0);
}
void car_left()
{
digitalWrite(11, 0);
digitalWrite(10, 1);
}
The comment does not match the code.
while (Serial.available())
{
char inChar = (char)Serial.read();
state = inChar; //make a string of the characters coming on serial
}
it didnt work
Can you expand on that? I loaded your code to an Uno, put prints in each of the functions and it seems to work fine as far as executing the proper functions when b, f, s, l and r are sent from serial monitor. I don't have anything connected to the output pins, though. Serial monitor set with no line endings.
char junk;
String state = "";
void setup() {
Serial.begin(9600); // set the baud rate to 9600, same should be of your Serial Monitor
pinMode(13, OUTPUT); //A1
pinMode(12, OUTPUT); //A2
pinMode(11, OUTPUT); //B1
pinMode(10, OUTPUT); //B2
}
void loop()
{
if (Serial.available())
{
while (Serial.available())
{
char inChar = (char)Serial.read(); //read the input
state = inChar; //make a string of the characters coming on serial
}
Serial.println(state);
while (Serial.available() > 0)
{
junk = Serial.read() ; // clear the serial buffer
}
if (state == "f")
{ //in case of 'f' turn DC motor forward
car_forward();
}
else if (state == "b") //in case of 'b' turn DC motor backword
{
car_backword();
}
else if (state == "l") //in case of 'l' turn wheels to left
{
car_left();
}
else if (state == "r") //in case of 'r' turn wheels to right
{
car_right();
}
else if (state == "s")
{
motor_stop();
}
}
}
void motor_stop()
{
Serial.println("stop");
digitalWrite(13, 0);
digitalWrite(12, 0);
digitalWrite(11, 0);
digitalWrite(10, 0);
}
void car_forward()
{
Serial.println("fwd");
digitalWrite(13, 1);
digitalWrite(12, 0);
}
void car_backword()
{
Serial.println("back");
digitalWrite(13, 0);
digitalWrite(12, 1);
}
void car_right()
{
Serial.println("rt");
digitalWrite(11, 1);
digitalWrite(10, 0);
}
void car_left()
{
Serial.println("lf");
digitalWrite(11, 0);
digitalWrite(10, 1);
}
wat means by Serial monitor set with no line endings.
To the left of the baud rate setting box is a box where you can set serial monitor to send a carriage return/line feed or just line feed or nothing at the end of characters sent when the send button is pushed to send data from serial monitor.