Hello everyone,
I was trying to build a robot car and I have small problem with BT comunication.
First, this is app which I use
I use this code
#include <AFMotor.h>
//creates two objects to control the terminal 3 and 4 of motor shield
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
char command;
void setup()
{
Serial.begin(9600); //Set the baud rate to your Bluetooth module.
}
void loop(){
if(Serial.available() > 0){
Stop(); //initialize with motors stoped
//Change pin mode only if new command is different from previous.
//Serial.println(command);
switch(command){
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
}
}
}
void forward()
{
motor1.setSpeed(100); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(100); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(100); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(100); //Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void back()
{
motor1.setSpeed(100);
motor1.run(BACKWARD); //rotate the motor counterclockwise
motor2.setSpeed(100);
motor2.run(BACKWARD); //rotate the motor counterclockwise
motor3.setSpeed(100);
motor3.run(BACKWARD); //rotate the motor counterclockwise
motor4.setSpeed(100);
motor4.run(BACKWARD); //rotate the motor counterclockwise
}
void left()
{
motor1.setSpeed(100); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(0);
motor2.run(RELEASE); //turn motor2 off
motor3.setSpeed(100); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(0);
motor4.run(RELEASE); //turn motor2 off
}
void right()
{
motor1.setSpeed(0);
motor1.run(RELEASE); //turn motor1 off
motor2.setSpeed(100); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(0);
motor3.run(RELEASE); //turn motor1 off
motor4.setSpeed(100); //Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void Stop()
{
motor1.setSpeed(0);
motor2.run(RELEASE); //turn motor1 off
motor2.setSpeed(0);
motor2.run(RELEASE); //turn motor2 off
motor3.setSpeed(0);
motor3.run(RELEASE); //turn motor1 off
motor4.setSpeed(0);
motor4.run(RELEASE); //turn motor2 off
}
, and I manage to move the car.
BUT I want to use some buttons from app, (speed adjust, front light and back light), and I can't figure it out how..
I tried this code, but without success..
//*** 1- Documentation
//This program is used to control a robot car using a app that communicates with Arduino trough a bluetooth module
#include <AFMotor.h>
//creates two objects to control the terminal 3 and 4 of motor shield
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
int Speed = 204; // 0 - 255.
int Speedsec;
int fl = 3;
int bl = 2;
char command;
void setup()
{
pinMode(fl,OUTPUT);
pinMode(bl,OUTPUT);
Serial.begin(9600); //Set the baud rate to your Bluetooth module.
}
void loop(){
if(Serial.available() > 0){
command = Serial.read();
Stop(); //initialize with motors stoped
//Change pin mode only if new command is different from previous.
//Serial.println(command);
switch(command){
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
case '0':
Speed = 100;
break;
case '1':
Speed = 140;
break;
case '2':
Speed = 153;
break;
case '3':
Speed = 165;
break;
case '4':
Speed = 178;
break;
case '5':
Speed = 191;
break;
case '6':
Speed = 204;
break;
case '7':
Speed = 216;
break;
case '8':
Speed = 229;
break;
case '9':
Speed = 242;
break;
case 'q':
Speed = 255;
break;
case 'w':
digitalWrite(fl,HIGH);
break;
case 'u':
digitalWrite(bl,HIGH);
break;
}
}
}
void forward()
{
motor1.setSpeed(100); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(100); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(100); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(100); //Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void back()
{
motor1.setSpeed(100);
motor1.run(BACKWARD); //rotate the motor counterclockwise
motor2.setSpeed(100);
motor2.run(BACKWARD); //rotate the motor counterclockwise
motor3.setSpeed(100);
motor3.run(BACKWARD); //rotate the motor counterclockwise
motor4.setSpeed(100);
motor4.run(BACKWARD); //rotate the motor counterclockwise
}
void left()
{
motor1.setSpeed(220); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(0);
motor2.run(RELEASE); //turn motor2 off
motor3.setSpeed(220); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(0);
motor4.run(RELEASE); //turn motor2 off
}
void right()
{
motor1.setSpeed(0);
motor1.run(RELEASE); //turn motor1 off
motor2.setSpeed(220); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(0);
motor3.run(RELEASE); //turn motor1 off
motor4.setSpeed(220); //Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void Stop()
{
motor1.setSpeed(0);
motor2.run(RELEASE); //turn motor1 off
motor2.setSpeed(0);
motor2.run(RELEASE); //turn motor2 off
motor3.setSpeed(0);
motor3.run(RELEASE); //turn motor1 off
motor4.setSpeed(0);
motor4.run(RELEASE); //turn motor2 off
}
IF someone know how to use the buttons, please help me