BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

BLE connection with Arduino 101


#1

I make a robot with Arduino 101.Now the problem is the robot will move before I control it with my phone,until l connect it successfully.
how can i make it keep still until l connect it successfully?


#2

Welcome to Blynk

This general topic has been discussed a few times here. The basic consensus is that this isn’t so much a Blynk question as a how to program question.

Essentially you need to code your sketch in such a way that a moving object is in a stopped condition by default, and only moves when given a clear signal to do so. Otherwise you run into rogue robots running amok and causing the end of the world as we know it :stuck_out_tongue_winking_eye:

Then you adapt your code to accept Blynk commands instead of buttons or via other BLE apps.

Next to consider, the Bluetooth and BLE (in Blynk) feature is a link for the Blynk app to connect to a device directly without the usual Cloud or Local server. It is not quite as robust as a WiFi connection through a server, and you do not directly access this serial connection as you would with basic Arduino sketches.

Start off by showing us the sketch you are currently using… And please format it so that is looks proper as per these directions.


#3

Could you offer me some example?

follow is my code:


#define BLYNK_PRINT Serial
#include <BlynkSimpleCurieBLE.h>
#include <CurieBLE.h>
#include <Servo.h>

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

BLEPeripheral  blePeripheral;

WidgetLED highTem(V4);
int x;
int y;
unsigned long nowtime;
unsigned long oldtime;
Servo servo_3;

void setup() {
  Serial.begin(9600);
  delay(1000);
  servo_3.attach(3);
  pinMode(4, OUTPUT); 
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  pinMode(7, OUTPUT); 
  pinMode(9, OUTPUT); 
  blePeripheral.setLocalName("Blynk");
  blePeripheral.setDeviceName("Blynk");
  blePeripheral.setAppearance(384);
  analogWrite(5,0);
  analogWrite(6,0);
  delay(200);
  Blynk.begin(blePeripheral, auth);

  blePeripheral.begin();
  Serial.println("Waiting for connections...");
  
}
BLYNK_WRITE(V1)
{
 x = param[0].asInt();
 y = param[1].asInt();

  // Do something with x and y
  Serial.print("X = ");
  Serial.print(x);
  Serial.print("; Y = ");
  Serial.println(y);
  }

void loop() {
  Blynk.run();
  blePeripheral.poll();

   uint16_t val1;
    int dat;
    //int val2;
 if (x == 75 && y == 75) {
    digitalWrite(4,HIGH);
    analogWrite(5,0);
    digitalWrite(7,HIGH);
    analogWrite(6,0);
   // delay(20);

  }
  if ((x > 60 && x < 110) && y > 75) {
    digitalWrite(4,HIGH);
    analogWrite(5,100);
    digitalWrite(7,HIGH);
    analogWrite(6,100);
    //delay(20);

  }
  if (x < 60 && y > 75) {
    digitalWrite(4,LOW);
    analogWrite(5,100);
    digitalWrite(7,HIGH);
    analogWrite(6,100);
    //delay(20);

  }
  if (x > 110 && y > 75) {
    digitalWrite(4,HIGH);
    analogWrite(5,100);
    digitalWrite(7,LOW);
    analogWrite(6,100);
// delay(20);
  }
  if ((x > 60 && x < 110) && y < 75) {
    digitalWrite(4,LOW);
    analogWrite(5,100);
    digitalWrite(7,LOW);
    analogWrite(6,100);
   // delay(20);

  }
  if (x < 60 && y < 75) {
    digitalWrite(4,LOW);
    analogWrite(5,100);
    digitalWrite(7,HIGH);
    analogWrite(6,100);
   //delay(20);

  }
  if (x > 110 && y < 75) {
    digitalWrite(4,HIGH);
    analogWrite(5,100);
    digitalWrite(7,LOW);
    analogWrite(6,100);
    // delay(20);
  }  

}


#4

You didn’t format your code as requested… So I have edited your post to show you how it is done… Please look back at it and see how to do that yourself in the future. Thanks.

Your code has way too much happening in the main void loop()… When using Blynk, it is important to try to break functions out into SimpleTimer routines.

Search this site for the words or phrases like “robot” and “how to use simpletimer”, etc. to see many different examples and methods. Also check out the links at the top right of this page for the Documentation, the Help Center, Sketch Builder and other helpful learning tips and examples.


#5

I am guessing you are using the joystick widget? If so, set your X & Y variables to the neutral 128 settings in setup. And if you haven’t already, create an “if loop” that always defaults to those settings ±5 (and stops the motors) until it sees something else coming from the BLYNK_WRITE(V1) function.


#6

yes,I am using the joysticks widget to control my car. I code it after learning from your sketch.But the BLYNK_WRITE(V1) function can’t run in the “if loop”.
so what I want to know is whether there some function to confirm the BLE connection.


#7

I haven’t made any full robot control sketches myself, yet…

They don’t need to as they are automatically called when the corresponding Widget changes state on the app end. But you can run if loops within a function and/or the void loop() to check for other status flags like time since last command (and if X >= Y seconds, then FULL STOP), etc…

Not that I have tested… there is Blynk.connected() but that is more for the Server <-> Device application.

As I understand it, Blynk Bluetooth and BLE are still in beta or experimental stages, so who knows what may be added (EDIT - or just not work quite right :wink: … and the Arduino 101 may also have the odd quirk or two?)

That said, this general question has been bandied about a few times already… adding more “is app connected” action will probably just add to the overall communications traffic… Blynk is really designed for IoT, not real-time mobile robot control. Not that it can’t do it, but fail safe features must be coded into the script and not made dependent on the presence or absence of App (or even server) connection.

After all, no one wants their house lights randomly flashing on/off or garage doors opening up just because their phone battery dies or they drive through a tunnel :wink:


#8

Thank you very much!
I think I should learn more about the code.Hope I can work it out.


#9

As I mentioned earlier, search for robot in this forum… there are many good projects with lots of code to compare with.

Keep us updated in this topic and ask if you need clarification with specific Blynk commands and such.


#10

Hi,there is a new problem.As you know, I make the project with Arduino 101.Now Intel has updated the boards from 1.0.7 to 2.0.2. I can’t connect my phone with Blynk By BLE with 2.0.2,but connect with 1.0.7.
BLynk will be updated to match the Arduino101 BLE?


#11

@vhymanskyy I think you are the one to mention this Arduino 101 & Blynk BLE issue too?


#12

@Gunner, rigth - I will take a look.
UPD: created issue here: https://github.com/blynkkk/blynk-library/issues/304


#13

BTW, 2.0.2 changes are covered here: http://forum.arduino.cc/index.php?topic=467970.0


#14

Filed an issue: https://github.com/01org/corelibs-arduino101/issues/531


Trouble Triggering LED ON/OFF with BLE Example