Trying to Set Low/Med/Hi buttons

Hello all! Long-time, first time. I am working on an Espressif’s ESP32-wroom-32ue and I am wanting to use the Blynk application to create an app that will set the minimum distance to trigger a warning. I am using a pimoroni VL53L1X time of flight sensor and coding inside the arduino desktop program.
This is one of my first projects so Im not that experienced at coding. As of right now i have the virtual buttons set at (V0) =<500, (V1)=<1000, and (V2)=<1500. These are set as BLYNK_WRITE functions outside of the void loop. The problem i am having is when the virtual button is pressed it only sends the warning once and not continually. I know i need to use some sort of loop/timer but im not that familiar with either. I will post my entire working below.
Thanks in advance!

//base code 115

#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial


#include <BlynkSimpleEsp32_BLE.h> //library needed for blynk-BLE connection
#include <BLEDevice.h> //library needed for blynk-BLE connection
#include <BLEServer.h> //library needed for blynk-BLE connection

#include <Arduino.h> //library needed for VL53L1X functions
#include <Wire.h> //library needed for VL53L1X functions
#include "SparkFun_VL53L1X.h" //library needed for VL53L1X functions

int ledPin = 16; //led connected to distance sensor


SFEVL53L1X distanceSensor;  //renaming function to distanceSensor

char auth[] = "Aob0RBj5mNWxd9MvKHuZ4wJotULlBa4K"; //authentication from blynk
BlynkTimer timer;


void setup()
{
  pinMode(ledPin, OUTPUT); //activation of led for distanceSensor
  Wire.begin();

  Serial.begin(115200); 
  Serial.println("VL53L1X Qwiic Test"); //test print

  Serial.println("Waiting for connections..."); //test print
  Blynk.setDeviceName("BlynK"); //setting name for bluetooth
  Blynk.begin(auth);

  if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
  {
    Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
    while (1)
      ;
  }
  Serial.println("Sensor online!");

  //distanceSensor.setDistanceModeShort();
  distanceSensor.setDistanceModeLong(); // changing the distance setting for sensor

 /* if (V0 = HIGH){               
    timer.setInterval(1000L,BLYNK_WRITE(V0));     when this set of code is active i receive an error saying cannot compile for board
  }*/
//  timer.setInterval(1000L,BLYNK_WRITE(V1));
//  timer.setInterval(1000L,BLYNK_WRITE(V2));

}

void loop(void)
{
  Blynk.run();
  timer.run();
  distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
  while (!distanceSensor.checkForDataReady())
  {
    //delay(1);
  }
  
  int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
  Blynk.virtualWrite(V5,distance); //retrieve distance data for virtual pin 5
  distanceSensor.clearInterrupt();
  distanceSensor.stopRanging();

  //Serial.print("VL53L1X Distance(mm): "); //print pretext for distance
  //Serial.print(distance); //print distance
  //delay(10); 


////////////////////////////////////////////////////    
  //delay(10);
  Serial.println();
 }
///////////////////////////////////////////////////////////////

 ///////////////DISTANCE 1/////////////////
BLYNK_WRITE(V0)
 {
  int ledPin = 16;
  int distance = distanceSensor.getDistance();
    //if (param.asInt())
    
      if(distance <= 500) //when distance is less than 1500mm turn led in pin 16 on, send message to Serial Monitor, Send push notification to phone
      {
        digitalWrite(ledPin, HIGH);
        Serial.print(" WARNING!");  // this works
        //Blynk.notify("Danger Close"); //this does not work
      }
    else
    {
      digitalWrite(ledPin, LOW);
    }
     
}
 
/////////////////DISTANCE 2///////////////////////
 BLYNK_WRITE(V1)
 {
  int ledPin = 16;
  int distance = distanceSensor.getDistance();
    //if (param.asInt())
    
      if(distance <= 1000) //when distance is less than 1500mm turn led in pin 16 on, send message to Serial Monitor, Send push notification to phone
      {
        digitalWrite(ledPin, HIGH);
        Serial.print(" WARNING!");  // this works
        //Blynk.notify("Danger Close"); //this does not work
      }
    else
    {
      digitalWrite(ledPin, LOW);
    }
     
}
    
  
  /////////////////DISTANCE 3///////////////////
 BLYNK_WRITE(V2)
 {
  int ledPin = 16;
  int distance = distanceSensor.getDistance();
    //if (param.asInt())
    
      if(distance <= 1500) //when distance is less than 1500mm turn led in pin 16 on, send message to Serial Monitor, Send push notification to phone
      {
        digitalWrite(ledPin, HIGH);
        Serial.print(" WARNING!");  // this works
        //Blynk.notify("Danger Close"); //this does not work
      }
    else
    {
      digitalWrite(ledPin, LOW);
    }
     
}
    

Can you explain what the purpose of these buttons is?

You are taking distance readings every time your void loop executes (which is probably a bit excessive, you might be better using a timer to call the routine which takes these readings less frequently - maybe once per second?)

You are also triggering a distance reading when one of the three button widgets is pressed.
I would have expected some logic attached to this looped distance measuring routine which then maybe lights up one of three LED widgets based on the distance reading - or maybe a single LED which changes colour depending on the distance reading.

One thing is for certain, and that is that you can’t loop the BLYNK_WRITE(vPin) functions. These functions are triggered when the value of a virtual pin changes, which is normally caused by user input via the app.

Pete.

1 Like

Yes, so i want to set virtual button’s V0 V1 and V2 to register different distances measured by the ToF sensor. they are set to send a warning message when an object gets withing 500 mm or less, 1000mm or less and 1500 mm or less. I would like to be able to change the minimum detection distance by selecting one of the buttons on the app. I’ve learned the hard way i simply cant put a loop statement inside the BLYNK function but i do not know how to achieve the same results using Blynk’s preferred method. I would like to have the sensor take a reading and display the warning message if applicable at least a few times a second. How would i go about that?

I think you need to figure out the user interface properly first.
Using buttons to select distance ranges is fine, although I think you’d be better using a segmented switch with three positions for that.
However, using the same buttons as indicators of distance makes no sense. As I said before, that would be better done using one or more LEDs.

What is the minimum acceptable frequency for taking these measurements?

Pete.

1 Like

thank you for the idea of using the segmented switch, it is better for my scenario so i went ahead and updated my sketch. i have the timer.setInterval within each case but it is only looping for case 1 which is =<500 mm. when case 2 or 3 is selected it still only loops the registered values under 500mm instead of their programmed 1000 and 1500.

//base code 115

#define BLYNK_USE_DIRECT_CONNECT
#define BLYNK_PRINT Serial


#include <BlynkSimpleEsp32_BLE.h> //library needed for blynk-BLE connection
#include <BLEDevice.h> //library needed for blynk-BLE connection
#include <BLEServer.h> //library needed for blynk-BLE connection

#include <Arduino.h> //library needed for VL53L1X functions
#include <Wire.h> //library needed for VL53L1X functions
#include "SparkFun_VL53L1X.h" //library needed for VL53L1X functions

int ledPin = 16; //led connected to distance sensor


SFEVL53L1X distanceSensor;  //renaming function to distanceSensor

char auth[] = "Aob0RBj5mNWxd9MvKHuZ4wJotULlBa4K"; //authentication from blynk
BlynkTimer timer;


void setup()
{
  pinMode(ledPin, OUTPUT); //activation of led for distanceSensor
  Wire.begin();

  Serial.begin(115200); 
  Serial.println("VL53L1X Qwiic Test"); //test print

  Serial.println("Waiting for connections..."); //test print
  Blynk.setDeviceName("BlynK"); //setting name for bluetooth
  Blynk.begin(auth);

  if (distanceSensor.begin() != 0) //Begin returns 0 on a good init
  {
    Serial.println("Sensor failed to begin. Please check wiring. Freezing...");
    while (1)
      ;
  }
  Serial.println("Sensor online!");

  //distanceSensor.setDistanceModeShort();
  distanceSensor.setDistanceModeLong(); // changing the distance setting for sensor     
}

void loop(void)
{
  Blynk.run();
  timer.run();
  distanceSensor.startRanging(); //Write configuration bytes to initiate measurement
  while (!distanceSensor.checkForDataReady())
  {
    //delay(1);
  }
  
  int distance = distanceSensor.getDistance(); //Get the result of the measurement from the sensor
  Blynk.virtualWrite(V5,distance); //retrieve distance data for virtual pin 5
  distanceSensor.clearInterrupt();
  distanceSensor.stopRanging();
  Serial.println();
 }
///////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////
BLYNK_WRITE(V0) {
  int ledPin = 16;
  int distance = distanceSensor.getDistance(); 
  switch (param.asInt())
  {
    case 1: { 
    //if (param.asInt())    
      if(distance <= 500) //when distance is less than 1500mm turn led in pin 16 on, send message to Serial Monitor, Send push notification to phone
      {
        digitalWrite(ledPin, HIGH);
        Serial.print(" WARNING!");  // this works
        timer.setInterval(300L,DistanceShort); 

        //Blynk.notify("Danger Close"); //this does not work
      }
    else
    {
      digitalWrite(ledPin, LOW);
    }
        break;
      }
    case 2: {
    //if (param.asInt())    
      if(distance <= 1000) //when distance is less than 1000mm turn led in pin 16 on, send message to Serial Monitor, Send push notification to phone
      {
        digitalWrite(ledPin, HIGH);
        Serial.print(" WARNING!");  // this works
        timer.setInterval(300L,DistanceMedium);
        //Blynk.notify("Danger Close"); //this does not work
      }
    else
    {
      digitalWrite(ledPin, LOW);
    }
        break;
      }
    case 3: { 
    //if (param.asInt())    
      if(distance <= 1500) //when distance is less than 1500mm turn led in pin 16 on, send message to Serial Monitor, Send push notification to phone
      {
        digitalWrite(ledPin, HIGH);
        Serial.print(" WARNING!");  // this works
        timer.setInterval(300L,DistanceLong);
        //Blynk.notify("Danger Close"); //this does not work
      }
    else
    {
      digitalWrite(ledPin, LOW);
    }
        break;
      }

    }
}

void DistanceShort() {
  int ledPin = 16;
  int distance = distanceSensor.getDistance();
    //if (param.asInt())
    
      if(distance <= 500) //when distance is less than 1500mm turn led in pin 16 on, send message to Serial Monitor, Send push notification to phone
      {
        digitalWrite(ledPin, HIGH);
        Serial.print(" WARNING!");  // this works
        //Blynk.notify("Danger Close"); //this does not work
      }
    else
    {
      digitalWrite(ledPin, LOW);
    }
}

void DistanceMedium() {
  int ledPin = 16;
  int distance = distanceSensor.getDistance();
    //if (param.asInt())
    
      if(distance <= 1000) //when distance is less than 1500mm turn led in pin 16 on, send message to Serial Monitor, Send push notification to phone
      {
        digitalWrite(ledPin, HIGH);
        Serial.print(" WARNING!");  // this works
        //Blynk.notify("Danger Close"); //this does not work
      }
    else
    {
      digitalWrite(ledPin, LOW);
    }
}
void DistanceLong() {
  int ledPin = 16;
  int distance = distanceSensor.getDistance();
    //if (param.asInt())
    
      if(distance <= 1500) //when distance is less than 1500mm turn led in pin 16 on, send message to Serial Monitor, Send push notification to phone
      {
        digitalWrite(ledPin, HIGH);
        Serial.print(" WARNING!");  // this works
        //Blynk.notify("Danger Close"); //this does not work
      }
    else
    {
      digitalWrite(ledPin, LOW);
    }
}

I think you should read this:
https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Also, blynk.notify has been replaced with blynk.logevent
https://docs.blynk.io/en/getting-started/events-tutorial

He’s using Legacy with BLE, so it’s still Blynk.notify I’m this case.

Pete.

Ohh, sorry.
As far as I know the notification doesn’t work with Bluetooth

@DmEk9 i can’t help unless you answer my questions…

Your code needs to be structured so that all that BLYNK_WRITE(V0) does is to store the current segmented switch value as a global variable which can be accessed elsewhere in your sketch.

You then need to gave a function which takes a reading and, depending on the value of that reading and the position of the segmented switch, takes the appropriate actions.

Notifications may not be appropriate, as they’re is a limit to how frequently these can be sent.

Pete.

1 Like

I am sorry, if i am understanding your questing right the maximum frequency for the sensor is 50Hz but for my application i think the minimum acceptable frequency should be around 3-4Hz

Okay, we’ll in that case you can’t use notifications in the way that you’re planning, as they are limited to one notifications every 5 seconds.
As you are using BLE, you’ll also need to check if notifications work at all, as some features don’t work in the same way.

You’ll need to use a BlynkTimer to call your measurement and evaluation routine. I’d start by calling the timer once every second (1000ms) to test the functionality, then reduce this to every 250ms to give you the 4 times per second sampling rate you need.

Pete.

1 Like