Programmation Remote Control car with Arduino

We have a RC Car controlled with “Arduino Uno” and we have “motor shield L298N” + “Bluetooth HC-05” + “Ultra sonic SFR05” so, we need a programming Arduino + the android application for control the car (left, right, up, down). THX :slight_smile:
This my programming and I hope you help me to correct it if you will get an error:

#include <Servo.h>
#define echoPin 8
#define trigPin 7
Servo leftservo;
Servo rightservo;
 //L298n Connection   
  const int motorA1  = (11,1); // avant 
  const int motorA2  = (8,1); //arriere
  const int motorB1  = (13,1); // left
  const int motorB2  = (12,1); // right 
  const int motorA  = (9,1); // motorA
  const int motorB  = (10,1); // Motorb
//Leds connected to Arduino UNO Pin 12
  const int lights  = 12;
//Buzzer / Speaker to Arduino UNO Pin 3
  const int buzzer = 3 ;   
//Bluetooth (HC-06 JY-MCU) State pin on pin 2 of Arduino
  const int BTState = 2;
//Calculate Battery Level
  const float maxBattery = 8.0;// Change value to your max battery voltage level! 
  int perVolt;                 // Percentage variable 
  float voltage = 0.0;         // Read battery voltage
  int level;
// Use it to make a delay... without delay() function!
  long previousMillis = -1000*10;// -1000*10=-10sec. to read the first value. If you use 0 then you will take the first value after 10sec.  
  long interval = 1000*10;       // interval at which to read battery voltage, change it if you want! (10*1000=10sec)
  unsigned long currentMillis;   //unsigned long currentMillis;
//Useful Variables
  int i=0;
  int j=0;
  int state;
  int vSpeed=200;     // Default speed, from 0 to 255

void setup() 
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
leftservo.attach(9);
rightservo.attach(10);

    
    pinMode (motorB1,OUTPUT); // pin13 set as output(INPUT_M2+)B+
    pinMode (motorB2,OUTPUT); // pin12 set as output(INPUT_M2-)B-
    pinMode (motorA1,OUTPUT); // pin11 set as output(INPUT_M1+)A+
    pinMode (motorA2,OUTPUT); // pin8 set as output(INPUT_M1-)A-
    pinMode (motorB,OUTPUT); // pin10 set as output(INPUT_M2)B
    pinMode (motorA,OUTPUT); // pin9 set as output(INPUT_M1)A
    pinMode(lights, OUTPUT); 
    pinMode(BTState, INPUT);    
    // Initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}
 
void loop()

{
  //Stop car when connection lost or bluetooth disconnected
     if(digitalRead(BTState)==LOW) { state='S'; }

  //Save income data to variable 'state'
    if(Serial.available() > 0){     
      state = Serial.read();   
    }
  
  //Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
    if (state == '0'){
      vSpeed=0;}
    else if (state == '1'){
      vSpeed=100;}
    else if (state == '2'){
      vSpeed=180;}
    else if (state == '3'){
      vSpeed=200;}
    else if (state == '4'){
      vSpeed=255;}
 	  
  /***********************Forward****************************/
  //If state is equal with letter 'F', car will go forward!
    if (state == 'F') {
    	analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
        analogWrite(motorB1, 0);      analogWrite(motorB2, 0); 
    }
  /**********************Forward Left************************/
  //If state is equal with letter 'G', car will go forward left
    else if (state == 'G') {
    	analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);  
        analogWrite(motorB1, 200);    analogWrite(motorB2, 0); 
    }
  /**********************Forward Right************************/
  //If state is equal with letter 'I', car will go forward right
    else if (state == 'I') {
      	analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0); 
        analogWrite(motorB1, 0);      analogWrite(motorB2, 200); 
    }
  /***********************Backward****************************/
  //If state is equal with letter 'B', car will go backward
    else if (state == 'B') {
    	analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, 0); 
    }
  /**********************Backward Left************************/
  //If state is equal with letter 'H', car will go backward left
    else if (state == 'H') {
    	analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed); 
        analogWrite(motorB1, 200); analogWrite(motorB2, 0); 
    }
  /**********************Backward Right************************/
  //If state is equal with letter 'J', car will go backward right
    else if (state == 'J') {
    	analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, 200); 
    }
  /***************************Left*****************************/
  //If state is equal with letter 'L', wheels will turn left
    else if (state == 'L') {
    	analogWrite(motorA1, 0);   analogWrite(motorA2, 0); 
        analogWrite(motorB1, 200); analogWrite(motorB2, 0); 
    }
  /***************************Right*****************************/
  //If state is equal with letter 'R', wheels will turn right
    else if (state == 'R') {
    	analogWrite(motorA1, 0);   analogWrite(motorA2, 0); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, 200); 		
    }
  /************************Lights*****************************/
  //If state is equal with letter 'W', turn leds on or of off
    else if (state == 'W') {
      if (i==0){  
         digitalWrite(lights, HIGH); 
         i=1;
      }
      else if (i==1){
         digitalWrite(lights, LOW); 
         i=0;
      }
      state='n';
    }
  /**********************Horn sound***************************/
  //If state is equal with letter 'V', play (or stop) horn sound
    else if (state == 'V'){
      if (j==0){  
         tone(buzzer, 1000);//Speaker on 
         j=1;
      }
      else if (j==1){
         noTone(buzzer);    //Speaker off 
         j=0;
      }
      state='n';  
    }
  /************************Stop*****************************/
  //If state is equal with letter 'S', stop the car
    else if (state == 'S'){
        analogWrite(motorA1, 0);  analogWrite(motorA2, 0); 
        analogWrite(motorB1, 0);  analogWrite(motorB2, 0);
    }
  /***********************Battery*****************************/
  //Read battery voltage every 10sec.
    currentMillis = millis();
    if(currentMillis - (previousMillis) > (interval)) {
       previousMillis = currentMillis; 
       //Read voltage from analog pin A0 and make calibration:
       voltage = (analogRead(A0)*5.015 / 1024.0)*11.132;
       //Calculate percentage...
       perVolt = (voltage*100)/ maxBattery;
       if      (perVolt<=75)               { level=0; }
       else if (perVolt>75 && perVolt<=80) { level=1; }    //        Battery level
       else if (perVolt>80 && perVolt<=85) { level=2; }    //Min ------------------------   Max
       else if (perVolt>85 && perVolt<=90) { level=3; }    //    | 0 | 1 | 2 | 3 | 4 | 5 | >
       else if (perVolt>90 && perVolt<=95) { level=4; }    //    ------------------------
       else if (perVolt>95)                { level=5; }   
       Serial.println(level);    
    }
    {
int distance,duration;
digitalWrite(trigPin, HIGH);
delay(200);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance=(duration/2)/29.1;
Serial.println(distance);
if(distance >7) // for going forward
{
leftservo.write(360); // left wheel forward
rightservo.write(-360); // right wheel forward
}
else // obstacle detected turn left
{
leftservo.write(-360); //left wheel backward
rightservo.write(-360); // right wheel forward
}
}
}

2016-04-14_16h41_57.png

2016-04-14_16h11_21.png

leftservo.write(360); // left wheel forward
rightservo.write(-360); // right wheel forward

The write() function of the Servo library normally takes positive values in the range 0 to 180.

From the comments it looks like the servos are used to drive the wheels. If so, where do the motors come into it ?

How is your project, in particular the servos and motors powered ?

Does the program do what you want ?

Please watch the attachments below and try to re-correct my programming Arduino because I'm beginner :slight_smile: thank you.

The attachments to your original post are just pictures of the devices that you are using. As such there are no details of how they are interconnected so it is unlikely that anyone will be able to help beyond pointing out obvious general mistakes in your program such as

const int motorA1  = (11, 1); // avant
const int motorA2  = (8, 1); //arriere
const int motorB1  = (13, 1); // left
const int motorB2  = (12, 1); // right
const int motorA  = (9, 1); // motorA
const int motorB  = (10, 1); // Motorb

What is this supposed to do ?

 if (Serial.available() > 0) {
    state = Serial.read();
  }

Where is the serial input coming from ?

Which pin(s) is the BT interface connected to ?

Can you please provide answers to the questions in post #1

i wanna this program . can i get it correct plz there s some problemes with me too ... my motor shiled L298N v1.0 didnt activet my 2 motor
theres probleme in code plz correct it
thnx

We can't see your code, so the chances of correcting it are slim.

i dont have code bro but i wanna it (code of kimov) correct :confused: i have a projet this year

i have a projet this year

So, where is your code?

:confused: dont have i look for it im beginner in arduino sorry !
i have: 2 motor
1 arduino uno
1 motor shield red one
1 ultrasonic sfr 05
1 bluetooth hc 05
4 diode for light
1 batterie 10000 ma 5v
1 buzzer

i look for program plz help me ! :cry:

i look for program

Have you considered the possibility that such a program does not exist, and that you may have to expend some effort and write it?