Arduino DIY | How to control a Arduino car by bluebooth | 如何用蓝牙控制Arduino小车

Before reading:

I write it on my PC typora ,but I find the passage with something wrong when I copy the passage to Utopian post editor.So I upload to the github.It is recommended to read on the below url.Thanks.
The more readable version

1.Have a look at my achievement first

2.Module compoents

  • Arduino UNO minichip * 1

  • L298N Motor driven module * 1

  • Arduino Bluetooth Bee module * 1

  • Arduino Bluetooth expansion board * 1

  • Car chassis * 1

  • Power supply (7~9V) * 1

  • Wires * several

INSTRUCTIONS:

  • My car is two wheels driven so I choose the L298N motor driven module.I recommend it to you if you also want to make a two wheels driven car.
  • The Bee bluetooth module I choose is really convient for me ,which can directly insert the Bluetooth into the expansion board,then connect the bluetooth expansion board with Arduino.
  • The 7-9V power can drive two motors better.If you want more than two motors , you should use a more poweful battery.

3.How to connect modules with Arduino

L298NArduino OR Battery
GNDArduino GND
VCCBattery +
5VArduino 5V
IN1Arduino D4
IN2Arduino D5
IN3Arduino D6
IN4Arduino D7
MOTOR1 OUTPUTMOTOR1
MOTOR2 OUTPUTMOTOR2

ENAIN1IN2Status
0XXSTOP
100BRAKE
101FORWARD
110BACKWARD
111BRAKE

IN1 and IN2 control motor 1. IN3 and IN4 control motor2

The form above is the same for the motor1 as well as the motor2


4.Run the motors

  • First I simply designed a program to implete running the left wheel forwardly for 2 seconds ,then stopping for 2seconds , and running backwardly for 2 seconds ,then stopping for 2 seconds.

    #define IN1 4 
    #define IN2 5     
    void setup()
    {
      pinMode(IN1,OUTPUT);
      pinMode(IN2,OUTPUT);
    }
    void loop()
    {
      digitalWrite(IN1,HIGH);      
      digitalWrite(IN2,LOW);         //forward
      delay(2000);
      digitalWrite(IN1,LOW);      
      digitalWrite(IN2,LOW);         //stop
      delay(2000);
      digitalWrite(IN1,LOW);      
      digitalWrite(IN2,HIGH);         //backward
      delay(2000);
      digitalWrite(IN1,LOW);      
      digitalWrite(IN2,LOW);         //stop
      delay(2000);
    }
    
  • Second I simply designed a program to implete running the right wheel forwardly for 2 seconds ,then stopping for 2seconds , and running backwardly for 2 seconds ,then stopping for 2 seconds.

    #define IN3 6 
    #define IN4 7     
    void setup()
    {
      pinMode(IN3,OUTPUT);
      pinMode(IN4,OUTPUT);
    }
    void loop()
    {
      digitalWrite(IN3,HIGH);      
      digitalWrite(IN4,LOW);         //forward
      delay(2000);
      digitalWrite(IN3,LOW);      
      digitalWrite(IN4,LOW);         //stop
      delay(2000);
      digitalWrite(IN3,LOW);      
      digitalWrite(IN4,HIGH);         //backward
      delay(2000);
      digitalWrite(IN3,LOW);      
      digitalWrite(IN4,LOW);         //stop
      delay(2000);
    }
    

REMEMBER:

Before uploading the code ,you should disconnect Arduino with the bluetooth module.


5. Simple motion of the car

WHEELFORWARDBACKWARDSTOP
LEFTFORWARDBACKWARDSTOP
RIGHTFORWARDBACKWARDSTOP

WHEELTURN LEFT FORWARDLYTURN LEFT BACKWARDLYTURN LEFT ON THE SPOT
LEFTSTOPSTOPBACKWARD
RIGHTFORWARDBACKWARDFORWARD

WHEELTURN RIGHT FORWARDLYTURN RIGHT BACKWARDLYTURN RIGHT ON THE SPOT
LEFTFORWARDBACKWARDFORWARD
RIGHTSTOPSTOPBACKWARD

According to the form above ,we can write the code easily

#define IN1 4 
#define IN2 5     
#define IN3 6 
#define IN4 7    
void setup()
{
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);

}
void loop()
{
  digitalWrite(IN1,HIGH);      
  digitalWrite(IN2,LOW);         //forward left wheel 
  digitalWrite(IN3,HIGH);      
  digitalWrite(IN4,LOW);         //forward right wheel 
  delay(2000);
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,LOW);         //stop left wheel 
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,LOW);         //stop right wheel 
  delay(1000);
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,HIGH);        //backward left wheel 
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,HIGH);        //backward right wheel
  delay(2000);
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,LOW);         //stop left wheel 
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,LOW);         //stop right wheel 
  delay(3000);
}

6. Function writing

void forward( )
{
  digitalWrite(IN1,HIGH);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,HIGH);      
  digitalWrite(IN4,LOW);         
}

void back( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,HIGH);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,HIGH);       
}


void turnLeft( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,HIGH);      
  digitalWrite(IN4,LOW);         
}


void turnbackLeft( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,HIGH);        
}

void turnRight( )
{
  digitalWrite(IN1,HIGH);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,LOW);         
}

void turnbackRight( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,HIGH);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,LOW);        
}

void turnLeftOrigin( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,HIGH);        
  digitalWrite(IN3,HIGH);      
  digitalWrite(IN4,LOW);        
}

void turnRightOrigin( )
{
  digitalWrite(IN1,HIGH);      
  digitalWrite(IN2,LOW);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,HIGH);      
}


void _stop()
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,LOW);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,LOW);        
}

7. Function using

Movement process:

forward for 2S ------> backward for 2S ------> forwardly left turn 2S ------> backwardly left turn for 2S ------> forwardly Turn right turn for 2S ------> backwardly turn right for 2S ------> turn left on the spot for 2S ------> turn right on the spot for 2S ------> stop 3S

#define IN1 4 
#define IN2 5    
#define IN3 6 
#define IN4 7     

void forward( );
void back( );
void turnLeft( );
void turnRight( );
void turnbackLeft( );
void turnbackRight( );
void turnLeftOrigin( );
void turnRightOrigin( );
void _stop();          

void setup()
{
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
}

void loop()
{
    forward( );
    delay(2000);
    back( );
    delay(2000);
    turnLeft( );
    delay(2000);
    turnbackLeft( );
    delay(2000);
    turnRight( );
    delay(2000);
    turnbackRight( );
    delay(2000);
    turnLeftOrigin( );
    delay(2000);
    turnRightOrigin( );
    delay(2000);
    _stop();
    delay(3000);    
}

void forward( )
{
  digitalWrite(IN1,HIGH);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,HIGH);      
  digitalWrite(IN4,LOW);         
}

void back( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,HIGH);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,HIGH);       
}


void turnLeft( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,HIGH);      
  digitalWrite(IN4,LOW);         
}


void turnbackLeft( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,HIGH);        
}

void turnRight( )
{
  digitalWrite(IN1,HIGH);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,LOW);         
}

void turnbackRight( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,HIGH);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,LOW);        
}

void turnLeftOrigin( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,HIGH);        
  digitalWrite(IN3,HIGH);      
  digitalWrite(IN4,LOW);        
}

void turnRightOrigin( )
{
  digitalWrite(IN1,HIGH);      
  digitalWrite(IN2,LOW);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,HIGH);      
}


void _stop()
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,LOW);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,LOW);        
}


8. Using the bluetooth module

This step is the key of our DIY project.

There are several points about that:

  • We can use 'Serial.read()' to read the Bluetooth order .
  • All the order will be conversed to ASCII code
  • When we receive the order ,we can use ASCII code or single quotation mark to judge what the order is.For example If the mobile terminal sends a "1", then we receive ASCII code of '1' on the side, so when we want to judge which number we receive we need to use case '1' or case49 to decide .(Note: ASCII code of 1 is 49).
  • Test the bluetooth demo

    whenever receiving the number '1', LED on Arduino D13 will power on.

    void setup()
    {
      pinMode(13,OUTPUT);
      Serial.begin(9600);
    }
    int i;
    void loop()
    {
      if(Serial.available()>0)
      {
          i=Serial.read();
          switch(i)
          {
            case'1':
                {digitalWrite(13,HIGH);break;}
            case'2':
                {digitalWrite(13,LOW);break;}
          }
      }
    }
    

  • Use the number one to nine to control the car movement.

    ORDERMOVEMENT
    1turnLeft
    2forward
    3turnRight
    4turnLeftOrigin
    5NONE
    6turnLeftOrigin
    7turnbackLeft
    8back
    9turnbackRight

#define IN1 4 
#define IN2 5    
#define IN3 6 
#define IN4 7    

void forward( );
void back( );
void turnLeft( );
void turnRight( );
void turnbackLeft( );
void turnbackRight( );
void turnLeftOrigin( );
void turnRightOrigin( );
void _stop();          

void setup()
{
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
  Serial.begin(9600);
}
int i;
void loop()
{
    if(Serial.available())
    {
      i=Serial.read();
      
      switch(i)
      {
        case'1':
          {turnLeft( );  break; }
        case'2':
          {forward( );   break;}
        case'3':
          {turnRight( );   break;}
        case'4':
          {turnLeftOrigin( );   break;}
        case'5':
          {_stop();   break;}
        case'6':
          {turnRightOrigin( );   break;}
        case'7':
          {turnbackLeft( );   break;}
        case'8':
          {back( );   break;}
        case'9':
          {turnbackRight( );   break;}
      }
    }    
}
void forward( )
{
  digitalWrite(IN1,HIGH);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,HIGH);      
  digitalWrite(IN4,LOW);         
}

void back( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,HIGH);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,HIGH);       
}


void turnLeft( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,HIGH);      
  digitalWrite(IN4,LOW);         
}


void turnbackLeft( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,HIGH);        
}

void turnRight( )
{
  digitalWrite(IN1,HIGH);      
  digitalWrite(IN2,LOW);         
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,LOW);         
}

void turnbackRight( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,HIGH);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,LOW);        
}

void turnLeftOrigin( )
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,HIGH);        
  digitalWrite(IN3,HIGH);      
  digitalWrite(IN4,LOW);        
}

void turnRightOrigin( )
{
  digitalWrite(IN1,HIGH);      
  digitalWrite(IN2,LOW);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,HIGH);      
}


void _stop()
{
  digitalWrite(IN1,LOW);      
  digitalWrite(IN2,LOW);        
  digitalWrite(IN3,LOW);      
  digitalWrite(IN4,LOW);        
}


That is all .Thanks for visiting and reading patiently.



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
8 Comments