PROJECT Nº 7: Control the Code&Drive via Bluetooth with an app

Create a program to control the Code&Drive from your smartphone or tablet Android via Bluetooth using an app.

DIFFICULTY LEVEL: Advanced.

DURATION OF THE EXERCISE:  60 min.

MATERIALS:

  • 1 USB – Micro USB cable
  • Computer
  • Android smartphone or tablet with the App needed for the project (Arduino Bluetooth RC Car)

The Code&Drive will have to be built and connected according to the instructions manual.

How does the Build&Code board function on the Code&Drive?

For this activity you will use the digital and analog pins, DC motors connections and the Code&Drive Bluetooth communication.

  1. Digital Pins: They are located on the left side of the Build&Code 4in1 board. The color code is Black is (GND-ground), Red (VCC) and Yellow (Digital I/O signal). You will find 12 connectors numbered from 2 to 13. 7 of them can provide PWM output signal. These connectors are indicated with a ~ symbol besides the number.
  2. Analog pins: They are located on the right side of the Build&Code 4in1 board. The color code is Black is (GND ground), Red (VCC) and Blue (Analog input).
    You will find 5 connectors numbered from A0 to A5.
  3. Data transmission from computer to Build&Code 4in1 board: Connect the board to the computer using the USB-Micro USB cable. Make sure the BLT/USB switch is on USB mode.
  4. Data transmission from Build&Code 4in1 to Android smartphone:Set the BLT/USB switch on BTL mode to allow the communication between the Build&Code 4in1 board and the Android device.
  5. Configuration of the Bluetooth communication with the Android smartphone or tablet: Feed and connect the Build&Code 4in1 board. Turn on the Bluetooth on your mobile device, search for the signal named “HC-06” and select it. If your device asks you for a password, enter 1234 or 0000.
  6. Powering the Build&Code 4in1 board: The Build&Code 4in1 board can be powered by the computer using a USB cable. Once you connect it, you will see it turns on automatically, and there will be power for the sensors and the Bluetooth connection, but not for the DC motors. You can also turn on the Build&Code 4in1 board with a Jack 5.5 mm input, using the external adapter connected to a 12VDC maximum electrical outlet
  7. Powering the DC motors: You will have to use the input of the battery holder, to which the Jack input is connected. You have to turn on the Build&Code 4in1 board manually by using the ON/OFF switch located on the top of the board.

How do the sensors of the Code&Drive function?

  • Blue and green LEDs: A LED is a Light Emitting Diode. In this case, the colors are blue and green. In this exercise you will turn On/Off the front and rear lights of the Code&Drive using the App. These components can be connected to any digital pins of the Build&Code 4in1 board except for the number 4, 5, 6 and 7, which are reserved for the motors. To program the LEDs functioning, you have to configure the digital pins where they are connected to as outputs pins.
  • Buzzer: It’s an electric audio signal device that makes a “buzz” sound. This component can be connected to any digital pin of the Build&Code 4in1 board except 4, 5, 6 and 7 which are reserved for the motors. To program its functioning, you have to configure the digital pins where they are connected to as outputs.
  • DC motor: They are machines that transform electric energy into mechanical energy, that triggers a rotatory movement. The rotation direction will be controlled with the app cursor, and the speed will be controlled with the slide bar. In order for the motors to work correctly, you have to power the Build&Code 4in1 board with AA batteries using the batteries holder. Make sure that the DIP switch (4 white switches) is on.

To program the DC motors you will use the digital pins 4, 5, 6 and 7, but it doesn’t mean that the motors will be connected to this pins.

Pins 4 and 7 will be programmed as a digital outputs (high and low), to control the direction on which the motors will rotate.

Pins 5 and 6 will be programmed as PWM signal outputs, with values from 0 to 255.

CONNECTIONS:

  1. Connect the DC motors to the digital pins as indicated below:
  2. Connect the DC motors to the ports indicated below:

Control the Code&Drive with the Arduino Bluetooth RC Car App

The app we recommend to use for this exercise is Arduino Bluetooth RC CAR. You can download it from Google Play Store.

Pair your smartphone with the Code&Drive using the app. Search and pair with the HC-06 Bluetooth signal. When it is correctly paired, the red indicator will turn green. Now you can control the Code&Drive with the App.

PROGRAMMING CODE

You can do this project using the Arduino software. Below you will find the necessary code.

Arduino Code

  1. Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
  2. Open the Arduino program and copy the following program in it:
    char data = 0;  //VARIABLE FOR STORING BLUETOOTH DATA
    int directionA = 4, speedA = 5, directionB = 7, speedB = 6, Speed ; // SPEED AND DIRECTION PINS TO MOTOR A AND B
    int LedV = 9, LedB = 10; // BLUE AND GREEN LED PINS
    int pinBuzzer = 8; // BUZZER PIN
    void setup()
    {
        Serial.begin(9600);   //Sets the baud for serial data transmission 
        // CONFIGURATION OF BUILD&CODE 4IN1 PINS 
        pinMode ( directionA, OUTPUT);
        pinMode ( speedA, OUTPUT);
        pinMode ( directionB, OUTPUT);
        pinMode ( speedB, OUTPUT);
        pinMode (LedV, OUTPUT);
        pinMode (LedB, OUTPUT);
    }
    void loop()
    {
       if(Serial.available() > 0)      // SEND DATA ONLY WHEN YOU RECEIVE DATA
    
       {
          data = Serial.read();        //READ THE INCOMING DATA AND STORE INTO DATA 
    
          // INTERPRETATION OF BLUETOOTH ENTRY DATA
    
          switch (data)
          {
            case 'F':   // FORWARD
            //MOTOR A
            analogWrite (speedA, Speed);
            digitalWrite ( directionA,LOW);
    
             //MOTOR B 
             analogWrite (speedB, Speed);
             digitalWrite ( directionB,HIGH);
             break;
    
             case 'B':  // BACK
             //MOTOR A
              analogWrite (speedA, Speed);
              digitalWrite ( directionA,HIGH);
    
              //MOTOR B
              analogWrite (speedB, Speed);
              digitalWrite ( directionB,LOW);
              break;
    
              case 'S':   // STOP
             //MOTOR A
              analogWrite (speedA, 0);
              digitalWrite ( directionA,LOW);
    
              //MOTOR B
              analogWrite (speedB, 0);
              digitalWrite ( directionB,HIGH);
              break;
    
              case 'L':   // LEFT
             //MOTOR A
              analogWrite (speedA, Speed);
              digitalWrite ( directionA,LOW);
    
         
    
              case 'R':   // RIGHT
    
              //MOTOR B
              analogWrite (speedB, Speed);
              digitalWrite ( directionB,HIGH);
              break;
    
              // CHANGE SPEED
              case '1':
              Speed = 25;
              break;
    
              case '2':
              Speed = 25*2;
              break;
    
              case '3':
              Speed = 25*3;
              break;
    
              case '4':
              Speed = 25*4;
              break;
    
              case '5':
              Speed = 25*5;
              break;
    
              case '6':
              Speed = 25*6;
              break;
    
              case '7':
              Speed = 25*7;
              break;
    
              case '8':
              Speed = 25*8;
              break;
    
              case '9':
              Speed = 25*9;
              break;
    
              case 'q':
              Speed = 255;
              break;
    
              // FORWARD LIGHT
              case'W':
              digitalWrite (LedV, HIGH);
              break;
    
              case'w':
              digitalWrite (LedV, LOW);
              break;
    
              // BACK LIGHT
              case'U':
              digitalWrite (LedB, HIGH);
              break;
    
              case'u':
              digitalWrite (LedB, LOW);
              break;
    
              // HORN
              case'V':
              tone(pinBuzzer,523.25,0);
              break;
    
              case'v':
              tone(pinBuzzer,0,100);
              break;       
          }
       }
    }
    
  3. Configure and upload the code, following the indications on the Code&Drive First Steps guide.
  4. Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.

RESULT OF THE EXERCISE

With this program you will control the Code&Drive with the App, using your smartphone or tablet. With the arrows you will be able to control the direction, with the slide bar you will control the speed, with the front/rear lights buttons you will control the LEDs, and with the horn button you will make the horn sound.

1 0

0