Step #10-3 : bluetooth receiver (HC-06) tests (3)

arduino4fun
Site Admin
Posts: 37
Joined: Mon Jun 06, 2016 1:16 pm

Step #10-3 : bluetooth receiver (HC-06) tests (3)

Postby arduino4fun » Thu Jun 09, 2016 12:52 pm

Bluetooth receiver (HC-06) tests connected to the robot; it's connected to the serial port of the Arduino Sensor Shield V5; I use the MOBOT application to test it until I release the mine; the settings of the app is : forward (f); backward (b); left (l); right (r); stop (s); all others (x)

NOTA : don't forget to remove the power signal of the bluetooth module while you are uploading your sketch as the uploader uses the serial port to get the programm, reconnect after the upload is finished.

Code: Select all

// ---------------------------------------------------------------------------------------------------------------
 // This sketch allows to check the bluetooth sensor HC-06 and the mobile phone to pilot the robot
 //
 // HC-06 -- Sensor Shield V5
 // VCC -- + - red
 // GND -- - - black
 // TXD -- RX - yellow
 // RXD -- TX - orange
 //
 //
 // https://www.facebook.com/arduino4fun
 // http://www.arduino4fun.com
 // ---------------------------------------------------------------------------------------------------------------

‪#‎include‬ <Wire.h>

// ************ L298 & motor control section **********************************************
 int pinLB=2; // Steering wheel control ---- motor driver board IN1
 int pinLF=4; // Steering wheel control ---- motor driver board IN2
 int pinRB=7; // Steering wheel control ---- motor driver board IN3
 int pinRF=8; // Steering wheel control ---- motor driver board IN4

// Movements
 int Lpwm_pin = 10; // Motor vehicle speed control pin driver board ENB
 int Rpwm_pin = 5; // Motor vehicle speed control pin driver board ENA

// car state
 int Car_state=0;

‪#‎define‬ _FORWARD 1
 #define _BACKWARD 2
 #define _TURNRIGHT 3
 #define _TURNLEFT 4
 #define _STOP 5

// Bluetooth
 String buffer; // buffer containing the message receibed
 char blueToothVal; //
 char lastcmd; // last state

char goBTAdvance = 'f'; // go forward
 char goBTBack = 'b'; // go backward
 char goBTLeft = 'l'; // go left
 char goBTRight = 'r'; // go right
 char goBTStop = 's'; // stop
 char goBTNull = 'x'; // null command for MOBOT BTCar

// other
 bool on = false;
 unsigned long last = millis();

// ****************************************************************************************
 // ****************************************************************************************

void forward(float val=0) // Forward
 {
 digitalWrite(pinRB,HIGH); // The motor ( right rear ) Action
 digitalWrite(pinRF,LOW);
 digitalWrite(pinLB,HIGH); // The motor ( rear left ) Action
 digitalWrite(pinLF,LOW);
 delay( val * 100);
 Car_state = _FORWARD;
 show_state();
 }

void turnL(float val=0) // Turn left
 {
 digitalWrite(pinRB,LOW); // The motor ( right rear ) Action
 digitalWrite(pinRF,HIGH);
 digitalWrite(pinLB,HIGH);
 digitalWrite(pinLF,LOW); // The motor (front left ) Action
 delay( val * 100);
 Car_state = _TURNLEFT;
 show_state();
 }

void turnR(float val=0) // Turn right
 {
 digitalWrite(pinRB,HIGH);
 digitalWrite(pinRF,LOW); // The motor ( front right ) action
 digitalWrite(pinLB,LOW); // The motor ( rear left ) Action
 digitalWrite(pinLF,HIGH);
 delay( val * 100);
 Car_state = _TURNRIGHT;
 show_state();
 }

void stopp(float val=0) // Stop
 {
 digitalWrite(pinRB,HIGH);
 digitalWrite(pinRF,HIGH);
 digitalWrite(pinLB,HIGH);
 digitalWrite(pinLF,HIGH);
 delay( val * 100);
 Car_state = _STOP;
 show_state();
 }

void backward(float val=0) // Backward
 {
 digitalWrite(pinRB,LOW); // The motor ( right rear ) Action
 digitalWrite(pinRF,HIGH);
 digitalWrite(pinLB,LOW); // The motor ( rear left ) Action
 digitalWrite(pinLF,HIGH);
 delay( val * 100);
 Car_state = _BACKWARD;
 show_state() ;
 }

void ConfigMotorsPins()
 {
 pinMode(pinLB,OUTPUT); // Pin 2
 pinMode(pinLF,OUTPUT); // Pin 4
 pinMode(pinRB,OUTPUT); // Pin 7
 pinMode(pinRF,OUTPUT); // Pin 8
 pinMode(Lpwm_pin,OUTPUT); // Pin 10 (PWM)
 pinMode(Rpwm_pin,OUTPUT); // Pin 5 (PWM)
 }

void Set_Speed(unsigned char Left,unsigned char Right)
 {
 analogWrite(Lpwm_pin,Left);
 analogWrite(Rpwm_pin,Right);
 }

bool ExecuteBTCmd( char cmd=0, float le=10 )
 {
 if (cmd == goBTAdvance)
 {
 forward(le);
 return true;
 }
 if (cmd == goBTBack)
 {
 backward(le);
 return true;
 }
 if (cmd == goBTLeft)
 {
 turnL(le);
 return true;
 }
 if (cmd == goBTRight)
 {
 turnR(le);
 return true;
 }
 if (cmd == goBTStop)
 {
 stopp();
 return true;
 }
 return false;
 }

void setup()
 {
 Serial.begin(9600);
 lastcmd = goBTStop;
 ConfigMotorsPins();
 delay(500);
 Set_Speed(3000,3000);
 ExecuteBTCmd( goBTAdvance, 2 ); // for test pourpose only
 stopp();
 }

void loop()
 {
 char val = Serial.read();
 if (val == -1)
 return;

Serial.write(val);

if( val == goBTNull ) // specifically for the MOBOT BTCar which sends stop command when another cmd is not sent
 return;
 if( val == lastcmd )
 {
 ExecuteBTCmd( goBTStop );
 lastcmd = goBTStop;
 }
 else
 {
 ExecuteBTCmd( goBTStop );
 lastcmd = val;
 //stopp();
 ExecuteBTCmd( val );
 //delay(500);
 }
 }
 


Image
Image
Image
Image
Image
Image

Return to “Multifunction Bluetooth Controlled Robot Smart Car Kits For Arduino”

Who is online

Users browsing this forum: No registered users and 1 guest