Rafa Juárez
Published © GPL3+

Rover Robot Controlled by a Blynk App from a Mobile

The idea is to upgrade my rover to be controlled by a Blynk app in my mobile. The Blynk is talking to my Arduino UNO using Bluetooth HC06.

BeginnerShowcase (no instructions)16 hours7,209
Rover Robot Controlled by a Blynk App from a Mobile

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
HC-06 Bluetooth Module
×1

Software apps and online services

Blynk
Blynk

Story

Read more

Code

CODE

Arduino
/*************************************************************
  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Blynk community:            http://community.blynk.cc
    Social networks:            http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example shows how to use Arduino with HC-06/HC-05
  Bluetooth 2.0 Serial Port Profile (SPP) module
  to connect your project to Blynk.

  Note: This only works on Android! iOS does not support SPP :(
        You may need to pair the module with your smartphone
        via Bluetooth settings. Default pairing password is 1234

  Feel free to apply it to any other example. It's simple!

  NOTE: Bluetooth support is in beta!

  You can receive x and y coords for joystick movement within App.

  App project setup:
    Two Axis Joystick on V1 in MERGE output mode.
    MERGE mode means device will receive both x and y within 1 message
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "65176043c54744dbb5615ff0a722898a";

SoftwareSerial SerialBLE(10, 11); // RX, TX

//*************************************************************
int Trig=5;
int Echo=3;
int MARCHA;
int INA=2;
int INB=4;
int ENA=6;

int INC=7;
int IND=8;
int ENB=9;

float LMotor_offset,RMotor_offset;
float Turn_Speed = 0, Turn_Speed_K = 0;
float Run_Speed = 0, Run_Speed_K = 0, Run_Speed_T = 0;
float LOutput=0;float ROutput=0;
//Variable en la que se va a almacenar el valor correspondiente a la distancia
int Dist,Dist1,Dist2;
// APP DEL MVIL
 int x,y;

BLYNK_WRITE(V10) {
  x = param[0].asInt(); // It extracts the x value of the Joystick inlcuded in the Mobile APP Blynk
  y = param[1].asInt(); // It extracts the y value of the Joystick inlcuded in the Mobile APP Blynk
}


void setup()
{
  // Debug console
  pinMode(13,OUTPUT);
  Serial.begin(9600);
  Serial.println("FICHERO coche10_with_blynk.ino");
  delay(2000);
  SerialBLE.begin(9600);
  Blynk.begin(SerialBLE, auth);
  Serial.println("Waiting for connections...");
  pinMode (Trig, OUTPUT);
  pinMode (Echo, INPUT);
  pinMode(INA,OUTPUT);
  pinMode(INB,OUTPUT);
  pinMode(INC,OUTPUT);
  pinMode(IND,OUTPUT);
  x=0;
  y=0;
  LOutput=0;
  ROutput=0;

}

void loop()
{
  Blynk.run(); // To Run Blynk
  XY_TO_RUN_TURN_SPEEDS(); //X of the Joystick represent the turn speed and Y the RUN speed
  PWMControl();// It controls the logic for the Motors H bridge
}

void XY_TO_RUN_TURN_SPEEDS(){
if(x!=128){
    Turn_Speed=map(x,0,255,160,-160);
}else{
  Turn_Speed=0;
}
if(y!=128){
  Run_Speed=map(y,0,255,-300,300);
}else{
  Run_Speed=0;
}
  LOutput = Run_Speed + Turn_Speed;
  ROutput = Run_Speed - Turn_Speed;  
  LOutput= -LOutput;
  }

  void PWMControl(){
  if(LOutput > 0){
    digitalWrite(INA, HIGH);
    digitalWrite(INB, LOW);
  }
  else if(LOutput < 0){
    digitalWrite(INA, LOW);
    digitalWrite(INB, HIGH);
  }
  else{
    digitalWrite(INA, HIGH);
    digitalWrite(INB, HIGH);
  }
  if(ROutput > 0){
    digitalWrite(INC, HIGH);
    digitalWrite(IND, LOW);
  }
  else if(ROutput < 0){   
    digitalWrite(INC, LOW);
    digitalWrite(IND, HIGH);
  }
  else{
    digitalWrite(INC, HIGH);
    digitalWrite(IND, HIGH);
  }
    ultrasonido (Dist);
    Dist1=Dist;
    Serial.println(Dist1);
    if (Dist1>20) {
    analogWrite(ENA, min(255, abs(LOutput)));
    analogWrite(ENB, min(255, abs(ROutput)));
    }else{
       analogWrite(ENA, 0);
       analogWrite(ENB, 0);
       
      
      
    }
   
}

//Este mdulo calcula y devuelve la distancia en cm.
/*
Puedes poner el cdigo del mdulo directamente en el loop o utilizar el mdulo
para reducir el nmero de lneas de cdigo del loop o reutilizar el cdigo
*/
void ultrasonido (int &Distancia){
 
//Para estabilizar el valor del pin Trig se establece a LOW
digitalWrite (Trig, LOW);
delay(10);
 
//Se lanzan los 8 pulsos
digitalWrite (Trig, HIGH);
delay(10);
digitalWrite (Trig, LOW);
 
/*
Se mide el tiempo que tarda la seal en regresar y se calcula la distancia.
 
Observa que al realizar pulseIn el valor que se obtiene es tiempo, no distancia
 
Se est reutilizando la variable Distancia.
*/
 
Distancia= pulseIn (Echo, HIGH);
Distancia=Distancia/58;
 
delay(10);
  
}

Credits

Rafa Juárez
18 projects • 39 followers
Very interested in prototyping of new ideas. 30 years experience in electronics.
Contact

Comments

Please log in or sign up to comment.