11
PROJECT 7: Control th 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

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

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

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

PROJECT Nº 7: Control theCode&Drive via Bluetooth with anapp

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

DIFFICULTY LEVEL: Advanced.

DURATION OF THE EXERCISE: 60 min.

MATERIALS:

1 USB – Micro USB cableComputer

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

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 instructionsmanual.

How does the Build&Code board functionon the Code&Drive?For this activity you will use the digital and analog pins, DC motors connectionsand the Code&Drive Bluetooth communication.

Digital Pins: They are located on the left side of the Build&Code 4in11.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. 7of them can provide PWM output signal. These connectors are indicatedwith a ~ symbol besides the number.Analog pins: They are located on the right side of the Build&Code 4in12.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.Data transmission from computer to Build&Code 4in1 board:3.Connect the board to the computer using the USB-Micro USB cable. Makesure the BLT/USB switch is on USB mode.Data transmission from Build&Code 4in1 to Android4.smartphone:Set the BLT/USB switch on BTL mode to allow thecommunication between the Build&Code 4in1 board and the Androiddevice.Configuration of the Bluetooth communication with the Android5.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 1234or 0000.Powering the Build&Code 4in1 board: The Build&Code 4in1 board can6.be powered by the computer using a USB cable. Once you connect it, youwill see it turns on automatically, and there will be power for the sensorsand the Bluetooth connection, but not for the DC motors. You can also

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

turn on the Build&Code 4in1 board with a Jack 5.5 mm input, using theexternal adapter connected to a 12VDC maximum electrical outletPowering the DC motors: You will have to use the input of the battery7.holder, to which the Jack input is connected. You have to turn on theBuild&Code 4in1 board manually by using the ON/OFF switch located onthe 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, thecolors are blue and green. In this exercise you will turn On/Off the frontand rear lights of the Code&Drive using the App. These components canbe connected to any digital pins of the Build&Code 4in1 board except forthe number 4, 5, 6 and 7, which are reserved for the motors. To programthe LEDs functioning, you have to configure the digital pins where theyare 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&Code4in1 board except 4, 5, 6 and 7 which are reserved for the motors. Toprogram its functioning, you have to configure the digital pins where theyare connected to as outputs.DC motor: They are machines that transform electric energy intomechanical energy, that triggers a rotatory movement. The rotationdirection will be controlled with the app cursor, and the speed will becontrolled 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 thebatteries 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’tmean that the motors will be connected to this pins.

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

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

CONNECTIONS:

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

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

Connect the DC motors to the ports indicated below:2.

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. Youcan download it from Google Play Store.

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

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

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

PROGRAMMING CODE

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

Arduino Code

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

Download and install the Arduino IDE program. It is available for1.Windows, Mac OS and Linux.Open the Arduino program and copy the following program in it:2.

char data = 0; //VARIABLE FOR STORING BLUETOOTH DATAint directionA = 4, speedA = 5, directionB = 7,speedB = 6, Speed ; // SPEED AND DIRECTION PINS TOMOTOR A AND Bint LedV = 9, LedB = 10; // BLUE AND GREEN LED PINSint pinBuzzer = 8; // BUZZER PINvoid setup(){ Serial.begin(9600); //Sets the baud for serialdata 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 ONLYWHEN YOU RECEIVE DATA

{ data = Serial.read(); //READ THEINCOMING 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;

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

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;

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

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;

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

case'u': digitalWrite (LedB, LOW); break;

// HORN case'V': tone(pinBuzzer,523.25,0); break;

case'v': tone(pinBuzzer,0,100); break; } }}

Configure and upload the code, following the indications on the3.Code&Drive First Steps guide.Check that the BTL/USB switch on the Build&Code 4in1 board is set to4.USB, to upload the code correctly.

RESULT OF THE EXERCISE

With this program you will control the Code&Drive with the App, using yoursmartphone 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 youwill control the LEDs, and with the horn button you will make the horn sound.