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

Blynk Connects then Disconnects

Hello,
I am doing a project involving multiple coin cell Motors in a specific vibration pattern which strength is controlled via Blynk. For some reason, when I connect to Blynk, it says “Connecting…” for a while, then says can’t connect, but then when I go out but then back into the bluetooth connect, it is connected. However, when I try to run it, it says the device is offline. Does anyone have any suggestions on how to fix this? Here is my code:

//to communicate with Blynk app
#include <BlynkSimpleSerialBLE.h>


/************************** Global Variables *********************************/
// Question: How many motors can the battery power to still get strong vibrations?
// Want to make it so it only powers about 3-4 motors at the same time but rapid changes which motors vibrate


//Motor pin assignments
#define tl 5  //Top left motor is on pin 5
#define tr 6  //Top right motor is on pin 6
#define tp 7  // Top motor on pin 7
#define bm 8 // Top motor is on pin 8
#define bl 10 //Bottom left motor is on pin 10
#define br 11 //Bottom right motor is on pin 11


//Motor Pair Variables
int ma1 = tl;  //Motor1 for pair a
int ma2 = bl; //Motor2 for pair a
int ma3 = tp; // Motor 3 for pair a
int mb1 = tr;  //Motor1 for pair b
int mb2 = br; //Motor2 for pair b
int mb3 = bm; // Motor 3 for pair a

//Input Variables
int v = 0; //Vibration strength
int t = 300; //Time between alternations

// Want the timing of the switching of motor states to be every 300 milliseconds. Don't know if that means t should equal this or t1 shold equal this
int m = 1; //Motor alternation mode
int p = 0; //On/Off for watch vibration


//Vibration Variables
int motorState = 0;   //Decides which motor pair to turn on/off
unsigned long t1; //Used to determine time between alternations

//Authentification code to connect to blynk
char auth[] = "sNeJOFbTJaQR3TS2OnnOw_prhAYrvTbw";

void setup()
{
  // BLE Setup
  Serial.begin(115200); //Initializes serial communications
  Blynk.begin(Serial, auth); //Initializes Blynk start-up protocol

  //Pin Setup
  pinMode(tl,OUTPUT); //Set top left motor as an output
  pinMode(tr,OUTPUT); //Set top right motor as an output
  pinMode(tp, OUTPUT);  // Set top motor as an output
  pinMode(bl,OUTPUT); //Set bottom left motor as an output
  pinMode(br,OUTPUT); //Set bottom right motor as an output
  pinMode(bm, OUTPUT); // Set as outpit
 
}

void loop()
{
  //Get Inputs
  Blynk.run();

  //Assign Motors
  assignMotors();

  //Vibration Logic
  if(p && Blynk.connected()){
    vibrate();
  }else{
    stopVibrate();
    p = 0;
  }
}


//Method that chooses which motors alternate. Depending on what value is stored in m,
//which is determined by the value sent from the Blynk app, there will be different 
//motor pairs assigned.

// This is where motor pairs (6) comes in 
void assignMotors() {

  //Left and Right
  if(m == 1){
    //Left Motors
    ma1 = tl;
    ma2 = bl;
    mb3 = bm;

    //Right Motors
    mb1 = tr;
    mb2 = br;
    ma3 = tp;
  }
    
}


// Figure out the timing of the vibrations and just make it constant
// Want to have two options: timing constant but also one that doesnt alternate vibrations and they are just always constant

//Method that turns on the motors. After t milliseconds of time have passed, it switches
//which motor pair is on and which is off.
void vibrate(){
  if(millis()-t1 > t){
    switchMotors(); //Switch motor vibration
    t1 = millis(); //Reset time counter after the 7 patterns have been done
  }
}

//This method switches which motor pair is vibrating. This is done using seven states.
void switchMotors(){
  if(motorState == 0){
    //Turn motor pair A on and B off
    analogWrite(ma1,v);
    analogWrite(ma2,v);
    analogWrite(ma3,v);
    analogWrite(mb1,0);
    analogWrite(mb2,0);
    analogWrite(mb3,0);

    motorState = motorState + 1;
  }
  else if(motorState == 1)
  {
    //Turn motor pair A off and B on
    analogWrite(ma1,0);
    analogWrite(ma2,0);
    analogWrite(ma3,0);
    analogWrite(mb1,v);
    analogWrite(mb2,v);
    analogWrite(mb3,v);

    
    motorState = motorState + 1;
  }
  else if(motorState == 2) {
    analogWrite(ma1,v);
    analogWrite(ma2,0);
    analogWrite(ma3,v);
    analogWrite(mb1,0);
    analogWrite(mb2,v);
    analogWrite(mb3,0);
    
    motorState = motorState + 1;
  }

    else if(motorState == 3) {
    analogWrite(ma1,0);
    analogWrite(ma2,v);
    analogWrite(ma3,0);
    analogWrite(mb1,v);
    analogWrite(mb2,0);
    analogWrite(mb3,v);
    
    motorState = motorState + 1;
  }
    else if(motorState == 4) {
    analogWrite(ma1,v);
    analogWrite(ma2,v);
    analogWrite(ma3,0);
    analogWrite(mb1,0);
    analogWrite(mb2,v);
    analogWrite(mb3,0);

    motorState = motorState + 1;
  }
    else if(motorState == 5) {
    analogWrite(ma1,v);
    analogWrite(ma2,0);
    analogWrite(ma3,v);
    analogWrite(mb1,v);
    analogWrite(mb2,0);
    analogWrite(mb3,0);

    motorState = motorState + 1;
  }
    else if(motorState == 6) {
    analogWrite(ma1,v);
    analogWrite(ma2,0);
    analogWrite(ma3,0);
    analogWrite(mb1,0);
    analogWrite(mb2,v);
    analogWrite(mb3,v);

    motorState = 0;
  }
}

//This method turns off all the motors.
void stopVibrate(){
  //Turn off all motors
  analogWrite(ma1,0);
  analogWrite(ma2,0);
  analogWrite(ma3,0);
  analogWrite(mb1,0);
  analogWrite(mb2,0);
  analogWrite(mb3,0);
}

/************* BLYNK LIBRARY METHODS *************************/
//These methods are called whenever a button is pressed or
//a slider is moved on the app. When Blynk sends a value to
//the Arduino, that value is stored in a variable depending
//on which slider/button is pressed.

//Assign V0 to v
BLYNK_WRITE(V0)
{
  v = param.asInt();
}

//Assign V1 to t
BLYNK_WRITE(V1)
{
  t = param.asInt();
}

//Assign V2 to m
BLYNK_WRITE(V2){
  m = param.asInt();
}

//Assign V3 to p
BLYNK_WRITE(V3){
  p = param.asInt();
}

Hey there, check this out
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

:scream::scream::scream::scream:

2 Likes

Ok, thank you for this. I changed my code, so now it looks like this:

//to communicate with Blynk app
#include <BlynkSimpleSerialBLE.h>


/************************** Global Variables *********************************/
// Question: How many motors can the battery power to still get strong vibrations?
// Want to make it so it only powers about 3-4 motors at the same time but rapid changes which motors vibrate


//Motor pin assignments
#define tl 5  //Top left motor is on pin 5
#define tr 6  //Top right motor is on pin 6
#define tp 7  // Top motor on pin 7
#define bm 8 // Top motor is on pin 8
#define bl 10 //Bottom left motor is on pin 10
#define br 11 //Bottom right motor is on pin 11
BlynkTimer timer; // Announcing the timer


//Motor Pair Variables
int ma1 = tl;  //Motor1 for pair a
int ma2 = bl; //Motor2 for pair a
int ma3 = tp; // Motor 3 for pair a
int mb1 = tr;  //Motor1 for pair b
int mb2 = br; //Motor2 for pair b
int mb3 = bm; // Motor 3 for pair a

//Input Variables
int v = 0; //Vibration strength
int t = 0; //Time between alternations
// Want the timing of the switching of motor states to be every 300 milliseconds. Don't know if that means t should equal this or t1 shold equal this
int m = 1; //Motor alternation mode
int p = 0; //On/Off for watch vibration


//Vibration Variables
int motorState = 0;   //Decides which motor pair to turn on/off
unsigned long t1; //Used to determine time between alternations

//Authentification code to connect to blynk
char auth[] = "sNeJOFbTJaQR3TS2OnnOw_prhAYrvTbw";

void setup()
{
  // BLE Setup
  Serial.begin(115200); //Initializes serial communications
  Blynk.begin(Serial, auth); //Initializes Blynk start-up protocol

  //Pin Setup
  pinMode(tl,OUTPUT); //Set top left motor as an output
  pinMode(tr,OUTPUT); //Set top right motor as an output
  pinMode(tp, OUTPUT);  // Set top motor as an output
  pinMode(bl,OUTPUT); //Set bottom left motor as an output
  pinMode(br,OUTPUT); //Set bottom right motor as an output
  pinMode(bm, OUTPUT); // Set as outpit

  // Declare function should run every 1000 milliseconds
 timer.setInterval(1000L, assignMotors); // should I use switchMotors?
}

void loop()
{
  //Get Inputs
  Blynk.run();
  timer.run(); // run timer every second

  //Vibration Logic
  if(p && Blynk.connected()){
    vibrate();
  }else{
    stopVibrate();
    p = 0;
  }
}


//Method that chooses which motors alternate. Depending on what value is stored in m,
//which is determined by the value sent from the Blynk app, there will be different 
//motor pairs assigned.

// This is where motor pairs (6) comes in 
void assignMotors() {

  //Left and Right
  if(m == 1){
    //Left Motors
    ma1 = tl;
    ma2 = bl;
    mb3 = bm;

    //Right Motors
    mb1 = tr;
    mb2 = br;
    ma3 = tp;
  }
    
}


// Figure out the timing of the vibrations and just make it constant
// Want to have two options: timing constant but also one that doesnt alternate vibrations and they are just always constant

//Method that turns on the motors. After t milliseconds of time have passed, it switches
//which motor pair is on and which is off.
void vibrate(){
  if(millis()-t1 > t){
    switchMotors(); //Switch motor vibration
    t1 = millis(); //Reset time counter after the 7 patterns have been done
  }
}

//This method switches which motor pair is vibrating. This is done using seven states.
void switchMotors(){
  if(motorState == 0){
    //Turn motor pair A on and B off
    analogWrite(ma1,v);
    analogWrite(ma2,v);
    analogWrite(ma3,v);
    analogWrite(mb1,0);
    analogWrite(mb2,0);
    analogWrite(mb3,0);

    motorState = motorState + 1;
  }
  else if(motorState == 1)
  {
    //Turn motor pair A off and B on
    analogWrite(ma1,0);
    analogWrite(ma2,0);
    analogWrite(ma3,0);
    analogWrite(mb1,v);
    analogWrite(mb2,v);
    analogWrite(mb3,v);

    
    motorState = motorState + 1;
  }
  else if(motorState == 2) {
    analogWrite(ma1,v);
    analogWrite(ma2,0);
    analogWrite(ma3,v);
    analogWrite(mb1,0);
    analogWrite(mb2,v);
    analogWrite(mb3,0);
    
    motorState = motorState + 1;
  }

    else if(motorState == 3) {
    analogWrite(ma1,0);
    analogWrite(ma2,v);
    analogWrite(ma3,0);
    analogWrite(mb1,v);
    analogWrite(mb2,0);
    analogWrite(mb3,v);
    
    motorState = motorState + 1;
  }
    else if(motorState == 4) {
    analogWrite(ma1,v);
    analogWrite(ma2,v);
    analogWrite(ma3,0);
    analogWrite(mb1,0);
    analogWrite(mb2,v);
    analogWrite(mb3,0);

    motorState = motorState + 1;
  }
    else if(motorState == 5) {
    analogWrite(ma1,v);
    analogWrite(ma2,0);
    analogWrite(ma3,v);
    analogWrite(mb1,v);
    analogWrite(mb2,0);
    analogWrite(mb3,0);

    motorState = motorState + 1;
  }
    else if(motorState == 6) {
    analogWrite(ma1,v);
    analogWrite(ma2,0);
    analogWrite(ma3,0);
    analogWrite(mb1,0);
    analogWrite(mb2,v);
    analogWrite(mb3,v);

    motorState = 0;
  }
}

//This method turns off all the motors.
void stopVibrate(){
  //Turn off all motors
  analogWrite(ma1,0);
  analogWrite(ma2,0);
  analogWrite(ma3,0);
  analogWrite(mb1,0);
  analogWrite(mb2,0);
  analogWrite(mb3,0);
}

/************* BLYNK LIBRARY METHODS *************************/
//These methods are called whenever a button is pressed or
//a slider is moved on the app. When Blynk sends a value to
//the Arduino, that value is stored in a variable depending
//on which slider/button is pressed.

//Assign V0 to v
BLYNK_WRITE(V0)
{
  v = param.asInt();
}

//Assign V1 to t
BLYNK_WRITE(V1)
{
  t = param.asInt();
}

//Assign V2 to m
BLYNK_WRITE(V2){
  m = param.asInt();
}

//Assign V3 to p
BLYNK_WRITE(V3){
  p = param.asInt();
}

It still is having the same problem of connecting, it connects, then when I press play on the Blynk it says it is offline. I am really inexperienced with Blynk and Arduino, so if anyone knows how to alter the code that would be a big help.

Your void loop should look like this

void loop()
{
  Blynk.run();
  timer.run();
}