[TUTO] Arduino Bluetooth RC Car

Arduino Bluetooth RC Car [BT,Android]


Voici un petit exemple de voiture Rc commander en bluetooth via une application mobile Android que vous pouvez trouvez ici:  https://play.google.com/store/apps/details?id=braulio.calle.bluetoothRCcontroller&hl=fr

Le nécessaire pour la réalisation:

- Arduino Uno
- Arduino Motor Shield R3
- TTL Bluetooth Module (Slave or Master-Slave)
- 9V battery
- Car's battery (needs to be greater than 5V)
- 4 LEDs
- 2 NPN transistors (2N2222)
- 2 1kΩ resistors
- 2 120Ω resistors
- 1 SPST switch
- An RC car with two DC motors. One for steering and one for moving forward/back
- Jumper wires (all lengths and rigidity)
- Solder and Soldering Iron


On commence par le Schéma des Leds, 2 Leds à l'avant et 2 à l'arrières





Le Schéma complet



Et le plus Important le code Arduino !!


________________________________________________________________________
char dataIn = 'S';        //data comming from the phone
int pinLeft = 10;     
int pinRight = 11;       
int pinEnableLeftRight = 9;
int pinForward = 5;
int pinBack = 6;
int pinSpeedForwardBack = 3;
int pinfrontLights = 8;
int pinbackLights = 7;
char determinant;         //used in the check function
char det;                 //used in the loop function
int velocity = 0;

void setup()  
{        
Serial.begin(9600);  //If you are using MDFLY module use 9600, for Bluetooth Mate use 115200.
pinMode(pinLeft, OUTPUT);
pinMode(pinRight, OUTPUT);
pinMode(pinForward, OUTPUT);
pinMode(pinBack, OUTPUT);
pinMode(pinEnableLeftRight, OUTPUT);
pinMode(pinSpeedForwardBack, OUTPUT);
pinMode(pinfrontLights, OUTPUT);
pinMode(pinbackLights, OUTPUT);
}

void loop()
{  
    det = check();
      while (det == 'F')   //if incoming data is a F, move foward
      {      
          digitalWrite(pinEnableLeftRight, LOW);  
          analogWrite(pinSpeedForwardBack, velocity); 
          digitalWrite(pinLeft,LOW);
          digitalWrite(pinRight,LOW);   
          digitalWrite(pinForward,HIGH);
          digitalWrite(pinBack,LOW);         
          det = check();           
      }   
      while (det == 'B')   //if incoming data is a B, move back 
      {     
          digitalWrite(pinEnableLeftRight, LOW);  
          analogWrite(pinSpeedForwardBack, velocity);
          digitalWrite(pinLeft,LOW);
          digitalWrite(pinRight,LOW);   
          digitalWrite(pinForward,LOW);
          digitalWrite(pinBack,HIGH);         
          det = check();           
      }  

      while (det == 'L')   //if incoming data is a L, move wheels left
      {      
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, 0);
          digitalWrite(pinLeft,HIGH);
          digitalWrite(pinRight,LOW);   
          digitalWrite(pinForward,LOW);
          digitalWrite(pinBack,LOW);        
          det = check();           
      }   
      while (det == 'R')   //if incoming data is a R, move wheels right
      {     
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, 0); 
          digitalWrite(pinLeft,LOW);
          digitalWrite(pinRight,HIGH);   
          digitalWrite(pinForward,LOW);
          digitalWrite(pinBack,LOW);         
          det = check();           
      } 
      
      while (det == 'I')   //if incoming data is a I, turn right forward
      {      
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, velocity);
          digitalWrite(pinLeft,LOW);
          digitalWrite(pinRight,HIGH);   
          digitalWrite(pinForward,HIGH);
          digitalWrite(pinBack,LOW);                 
          det = check();           
      }   
      while (det == 'J')   //if incoming data is a J, turn right back 
      {       
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, velocity);
          digitalWrite(pinLeft,LOW);
          digitalWrite(pinRight,HIGH);   
          digitalWrite(pinForward,LOW);
          digitalWrite(pinBack,HIGH);         
          det = check();           
      }           
      while (det == 'G')   //if incoming data is a G, turn left forward
      {  
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, velocity);
          digitalWrite(pinLeft,HIGH);
          digitalWrite(pinRight,LOW);   
          digitalWrite(pinForward,HIGH);
          digitalWrite(pinBack,LOW);           
          det = check();           
      }     
      while (det == 'H')   //if incoming data is a H, turn left back
      { 
          digitalWrite(pinEnableLeftRight, HIGH);  
          analogWrite(pinSpeedForwardBack, velocity);
          digitalWrite(pinLeft,HIGH);
          digitalWrite(pinRight,LOW);   
          digitalWrite(pinForward,LOW);
          digitalWrite(pinBack,HIGH);           
          det = check();                                               
      }    
      while (det == 'S')   //if incoming data is a S, stop
      { 
          digitalWrite(pinEnableLeftRight, LOW);  
          analogWrite(pinSpeedForwardBack, 0);
          digitalWrite(pinLeft,LOW);
          digitalWrite(pinRight,LOW);   
          digitalWrite(pinForward,LOW);
          digitalWrite(pinBack,LOW);        
          det = check();  
      }
      while (det == 'U')   //if incoming data is a U, turn ON front lights
      { 
          digitalWrite(pinfrontLights, HIGH);           
          det = check();  
      }
      while (det == 'u')   //if incoming data is a u, turn OFF front lights
      { 
          digitalWrite(pinfrontLights, LOW);           
          det = check();  
      }
      while (det == 'W')   //if incoming data is a W, turn ON back lights
      { 
          digitalWrite(pinbackLights, HIGH);           
          det = check();  
      }
      while (det == 'w')   //if incoming data is a w, turn OFF back lights
      { 
          digitalWrite(pinbackLights, LOW); 
          det = check();  
      }
}

int check()
{
  if (Serial.available() > 0)    //Check for data on the serial lines. 
  {    
    dataIn = Serial.read();  //Get the character sent by the phone and store it in 'dataIn'.
        if (dataIn == 'F') 
        {      
          determinant = 'F';
        }   
        else if (dataIn == 'B') 
        {  
          determinant = 'B';  
        }
        else if (dataIn == 'L')   
        {  
          determinant = 'L'; 
        }
        else if (dataIn == 'R')   
        {  
          determinant = 'R';
        }  
        else if (dataIn == 'I')   
        {  
          determinant = 'I';  
        }   
        else if (dataIn == 'J')   
        {   
          determinant = 'J'; 
        }           
        else if (dataIn == 'G')  
        { 
          determinant = 'G';  
        }     
        else if (dataIn == 'H')   
        { 
          determinant = 'H';  
        }    
        else if (dataIn == 'S')  
        {
          determinant = 'S';
        } 
        else if (dataIn == '0')  
        {
          velocity = 0;  
        } 
        else if (dataIn == '1')  
        {
          velocity = 25;
        }
        else if (dataIn == '2')  
        {
          velocity = 50;
        }
        else if (dataIn == '3')  
        {
          velocity = 75;
        }
        else if (dataIn == '4')  
        {
          velocity = 100;
        }
        else if (dataIn == '5')  
        {
          velocity = 125;
        }
        else if (dataIn == '6')  
        {
          velocity = 150;
        }
        else if (dataIn == '7')  
        {
          velocity = 175;
        }
        else if (dataIn == '8')  
        {
          velocity = 200;
        }
        else if (dataIn == '9')  
        {
          velocity = 225;
        }
        else if (dataIn == 'q')  
        {
          velocity = 255;
        }        
        else if (dataIn == 'U')  
        {
          determinant = 'U'; 
        }
        else if (dataIn == 'u')  
        {
          determinant = 'u'; 
        }
        else if (dataIn == 'W')  
        {
          determinant = 'W'; 
        }
        
        else if (dataIn == 'w')  
        {
          determinant = 'w'; 
        }
  } 
return determinant; 
}
_____________________________________________________________________

Ou encore:

_____________________________________________________________________


char dataIn = 'S';        //Character/Data coming from the phone.
int pinForwardBack = 13;     //Pin that controls the car's Forward-Back motor.
int pinLeftRight = 12;       //Pin that controls the car's Left-Right motor.
int pinBrakeLeftRight = 9;  //Pin that enables/disables Left-Right motor.
int pinBrakeForwardBack = 8;  //Pin that enables/disables Forward-Back motor.
int pinLeftRightSpeed = 3;    //Pin that sets the speed for the Left-Right motor.
int pinForwardBackSpeed = 11;  //Pin that sets the speed for the Forward-Back motor.
int pinfrontLights = 4;    //Pin that activates the Front lights.
int pinbackLights = 7;    //Pin that activates the Back lights.
char determinant;         //Used in the check function, stores the character received from the phone.
char det;                 //Used in the loop function, stores the character received from the phone.
int velocity = 0;    //Stores the speed based on the character sent by the phone.

void setup() 
{       
//*************NOTE: If using Bluetooth Mate Silver use 115200 btu
//                   If using MDFLY Bluetooth Module use 9600 btu
Serial.begin(115200);  //Initialize serial communication with Bluetooth module at 115200 btu.
pinMode(pinForwardBack, OUTPUT);
pinMode(pinLeftRight, OUTPUT);
pinMode(pinBrakeLeftRight, OUTPUT);
pinMode(pinBrakeForwardBack, OUTPUT);
pinMode(pinLeftRightSpeed , OUTPUT);
pinMode(pinForwardBackSpeed , OUTPUT);
pinMode(pinfrontLights , OUTPUT);
pinMode(pinbackLights , OUTPUT);
}

void loop()

    det = check();
      while (det == 'F')   //if incoming data is a F, move forward
      {     
          digitalWrite(pinForwardBack, LOW); 
          digitalWrite(pinBrakeForwardBack, LOW);
          analogWrite(pinForwardBackSpeed,velocity);
          analogWrite(pinLeftRightSpeed,0);  //To make the Left/Right Motor not move, you just need to make its speed 0.    
          det = check();          
      }  
      while (det == 'B')   //if incoming data is a B, move back
      {    
          digitalWrite(pinForwardBack, HIGH); 
          digitalWrite(pinBrakeForwardBack, LOW);
          analogWrite(pinForwardBackSpeed,velocity); 
          analogWrite(pinLeftRightSpeed,0);         
          det = check();          
      } 

      while (det == 'L')   //if incoming data is a L, move wheels left
      {     
          digitalWrite(pinLeftRight, HIGH);
          digitalWrite(pinBrakeLeftRight, LOW);
          digitalWrite(pinForwardBack, LOW);
          analogWrite(pinForwardBackSpeed,0);    //To make the Forward/Back Motor not move, you just need to make its speed 0.
          analogWrite(pinLeftRightSpeed,255);        
          det = check();          
      }  
      while (det == 'R')   //if incoming data is a R, move wheels right
      {    
          digitalWrite(pinLeftRight, LOW);
          digitalWrite(pinBrakeLeftRight, LOW);
          digitalWrite(pinForwardBack, LOW);
          analogWrite(pinForwardBackSpeed,0);
          analogWrite(pinLeftRightSpeed,255);         
          det = check();          
      }
     
      while (det == 'I')   //if incoming data is a I, turn right forward
      {     
          digitalWrite(pinLeftRight, LOW);
          digitalWrite(pinForwardBack, LOW);          
          digitalWrite(pinBrakeLeftRight, LOW); 
          digitalWrite(pinBrakeForwardBack, LOW);  
          analogWrite(pinForwardBackSpeed,velocity);
          analogWrite(pinLeftRightSpeed,255);                  
          det = check();          
      }  
      while (det == 'J')   //if incoming data is a J, turn right back
      {      
          digitalWrite(pinLeftRight, LOW);
          digitalWrite(pinForwardBack, HIGH);         
          digitalWrite(pinBrakeLeftRight, LOW); 
          digitalWrite(pinBrakeForwardBack, LOW);
          analogWrite(pinForwardBackSpeed,velocity);
          analogWrite(pinLeftRightSpeed,255);          
          det = check();          
      }          
      while (det == 'G')   //if incoming data is a G, turn left forward
      { 
          digitalWrite(pinLeftRight, HIGH);       
          digitalWrite(pinForwardBack, LOW);
          digitalWrite(pinBrakeLeftRight, LOW); 
          digitalWrite(pinBrakeForwardBack, LOW); 
          analogWrite(pinForwardBackSpeed,velocity);
          analogWrite(pinLeftRightSpeed,255);          
          det = check();          
      }    
      while (det == 'H')   //if incoming data is a H, turn left back
      {
          digitalWrite(pinLeftRight, HIGH);
          digitalWrite(pinForwardBack, HIGH); 
          digitalWrite(pinBrakeLeftRight, LOW); 
          digitalWrite(pinBrakeForwardBack, LOW);
          analogWrite(pinForwardBackSpeed,velocity);
          analogWrite(pinLeftRightSpeed,255);          
          det = check();                                              
      }   
      while (det == 'S')   //if incoming data is a S, stop
      {
          digitalWrite(pinForwardBack, LOW); 
          digitalWrite(pinLeftRight, LOW);
          analogWrite(pinForwardBackSpeed,0);
          analogWrite(pinLeftRightSpeed,0);          
          det = check(); 
      }
      while (det == 'U')   //if incoming data is a U, turn ON front lights
      {
          digitalWrite(pinfrontLights, HIGH);          
          det = check(); 
      }
      while (det == 'u')   //if incoming data is a u, turn OFF front lights
      {
          digitalWrite(pinfrontLights, LOW);          
          det = check(); 
      }
      while (det == 'W')   //if incoming data is a W, turn ON back lights
      {
          digitalWrite(pinbackLights, HIGH);          
          det = check(); 
      }
      while (det == 'w')   //if incoming data is a w, turn OFF back lights
      {
          digitalWrite(pinbackLights, LOW);
          det = check(); 
      }
}

int check()
{
  if (Serial.available() > 0)    //Check for data on the serial lines.
  {   
    dataIn = Serial.read();  //Get the character sent by the phone and store it in 'dataIn'.
        if (dataIn == 'F')
        {     
          determinant = 'F';
        }  
        else if (dataIn == 'B')
        { 
          determinant = 'B'; 
        }
        else if (dataIn == 'L')  
        { 
          determinant = 'L';
        }
        else if (dataIn == 'R')  
        { 
          determinant = 'R';
        } 
        else if (dataIn == 'I')  
        { 
          determinant = 'I'; 
        }  
        else if (dataIn == 'J')  
        {  
          determinant = 'J';
        }          
        else if (dataIn == 'G') 
        {
          determinant = 'G'; 
        }    
        else if (dataIn == 'H')  
        {
          determinant = 'H'; 
        }   
        else if (dataIn == 'S') 
        {
          determinant = 'S';
        }
        else if (dataIn == '0') 
        {
          velocity = 0;    //"velocity" does not need to be returned.
        }
        else if (dataIn == '1') 
        {
          velocity = 25;
        }
        else if (dataIn == '2') 
        {
          velocity = 50;
        }
        else if (dataIn == '3') 
        {
          velocity = 75;
        }
        else if (dataIn == '4') 
        {
          velocity = 100;
        }
        else if (dataIn == '5') 
        {
          velocity = 125;
        }
        else if (dataIn == '6') 
        {
          velocity = 150;
        }
        else if (dataIn == '7') 
        {
          velocity = 175;
        }
        else if (dataIn == '8') 
        {
          velocity = 200;
        }
        else if (dataIn == '9') 
        {
          velocity = 225;
        }
        else if (dataIn == 'q') 
        {
          velocity = 255;
        }
        else if (dataIn == 'U') 
        {
          determinant = 'U';
        }
        else if (dataIn == 'u') 
        {
          determinant = 'u';
        }
        else if (dataIn == 'W') 
        {
          determinant = 'W';
        }
       
        else if (dataIn == 'w') 
        {
          determinant = 'w';
        }
  }
return determinant;
}

 __________________________________________________________________

Aucun commentaire:

Enregistrer un commentaire