Go Down

Topic: Bluetooth comunication 4wd car (Read 732 times) previous topic - next topic

yulyann9

Jun 27, 2019, 12:59 am Last Edit: Jun 27, 2019, 01:16 am by yulyann9
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
Code: [Select]
#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..
 
Code: [Select]
//*** 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 :)



slipstick

A link to some documentation of that app would be a lot more useful than just a picture of it. For example something to tell you what commands those "buttons" send.

Steve

Michele61

you can look at this project

https://create.arduino.cc/projecthub/DANNY003/bluetooth-controlled-car-with-arduino-mega-616a4f?ref=search&ref_id=car bluetooth&offset=2

Go Up