BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Flashing LED widgets

Hey guys, I’m attempting to make the LED widgets in my project flash at different rates depending on the severity of the warning. I’ve looked around the previous LED topics but nothing seems to implement the way I have it. I’ve managed to enable differing colours depending on the warning but now I want them to flash. All this takes place at the bottom of each sensor data within void sensor.

Thanks in advance for any help

#define BLYNK_PRINT Serial

#include <BlynkSimpleSerialBLE.h>

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

WidgetLED led1(V2);
WidgetLED led2(V3);
WidgetLED led3(V4);
WidgetLED led4(V5);
WidgetLED led5(V6);
WidgetLED led6(V7);

#define BLYNK_GREEN     "#23C48E"
#define BLYNK_RED       "#D3435C"

// Attach virtual serial terminal to Virtual Pin V1
WidgetTerminal terminal(V1);

BlynkTimer timer;

int trigPin1=11; //Front transmitter (Object ahead detection).
int echoPin1=10; //Front receiver.
int trigPin2=9; //Right transmitter (Lane keeping).
int echoPin2=8; //Right receiver.
int trigPin3=7; //Rear right transmitter (Right blindspot detection).
int echoPin3=6; //Rear right receiver.
int trigPin4=5; //Rear centre transmitter (Parking sensor).
int echoPin4=4; //Rear centre receiver.
int trigPin5=3; //Rear left transmitter (Left blindspot detection).
int echoPin5=2; //Rear left receiver.
int trigPin6=14; //Left transmitter (Lane keeping).
int echoPin6=15; //Left receiver.

void setup() 
{
    // put your setup code here, to run once:
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(trigPin4, OUTPUT);
  pinMode(echoPin4, INPUT);
  pinMode(trigPin5, OUTPUT);
  pinMode(echoPin5, INPUT);
  pinMode(trigPin6, OUTPUT);
  pinMode(echoPin6, INPUT);
  
  Serial2.begin(9600);
 
  Serial.begin(9600); // Debug console
  
  Blynk.begin(Serial2, auth);

  Serial.println("Waiting for connections...");

  timer.setInterval(1000L, sensor);

  led1.on();
  led2.on();
  led3.on();
  led4.on();
  led5.on();
  led6.on();
}

void sensor()
{
//Object Ahead Distance.
  terminal.clear();
  long duration1, distance1;
  digitalWrite(trigPin1, HIGH);
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;
  
   if (distance1 >= 40){
    Blynk.virtualWrite(V1, "Nothing Ahead, Okay", "\r");}
   else if (distance1 <= 39 || distance1 >= 29){
    Blynk.virtualWrite(V1, "Vehicle Ahead, Slow");}
   else if (distance1 <= 29){
    Blynk.virtualWrite(V1, "Stop");}
    Serial.print(distance1);
    Serial.println("cm");
    
      if (distance1 >= 40) {
    led1.setColor(BLYNK_GREEN);}
    else if (distance1 <= 29) {
    led1.setColor(BLYNK_RED);}

     //Right Lane Keeping.
  long duration2, distance2;
  digitalWrite(trigPin2, HIGH);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2/2) / 29.1;

   if (distance2 <= 2){
    Blynk.virtualWrite(V1, "In Lane");}
   else if (distance2 >= 2){
    Blynk.virtualWrite(V1, "Adjust Steering");}
    Serial.print(distance2);
    Serial.println("cm");
 
      if (distance2 = 2) {
    led2.setColor(BLYNK_GREEN);}
    else if (distance2 <= 2) {
    led2.setColor(BLYNK_GREEN);}
    else if (distance2 >= 2) {
    led2.setColor(BLYNK_RED);}
    
  //Rear Right Blind Spot Detection.
  long duration3, distance3;
  digitalWrite(trigPin3, HIGH);
  digitalWrite(trigPin3, LOW);
  duration3 = pulseIn(echoPin3, HIGH);
  distance3 = (duration3/2) / 29.1;

   if (distance3 >= 20){
    Blynk.virtualWrite(V1, "Right Blind Spot Clear");}
   else if (distance3 <= 20){
    Blynk.virtualWrite(V1, "Vehicle In Blindspot");}
    Serial.print(distance3);
    Serial.println("cm");

      if (distance3 >= 20) {
    led3.setColor(BLYNK_GREEN);}
    else if (distance3 <= 20) {
    led3.setColor(BLYNK_RED);}
    
  //Parking Sensor.
  long duration4, distance4;
  digitalWrite(trigPin4, HIGH);
  digitalWrite(trigPin4, LOW);
  duration4 = pulseIn(echoPin4, HIGH);
  distance4 = (duration4/2) / 29.1;
  
   if (distance4 >= 40){
    Blynk.virtualWrite(V1, "Nothing Behind, Okay");}
   else if (distance4 <= 39 || distance4 >= 29){
    Blynk.virtualWrite(V1, "Object Behind, Slow");}
//   else if (distance4 <= 29){
//    Serial.println("Stop" distance4);}
    Serial.print(distance4);
    Serial.println("cm");
    
      if (distance4 >= 40) {
    led4.setColor(BLYNK_GREEN);}
    else if (distance4 <= 29) {
    led4.setColor(BLYNK_RED);}
    
  //Rear Left Blind Spot Detection.
  long duration5, distance5;
  digitalWrite(trigPin5, HIGH);
  digitalWrite(trigPin5, LOW);
  duration5 = pulseIn(echoPin5, HIGH);
  distance5 = (duration5/2) / 29.1;

   if (distance5 >= 20){
    Blynk.virtualWrite(V1, "Right Blind Spot Clear");}
   else if (distance5 <= 20){
    Blynk.virtualWrite(V1, "Vehicle In Blindspot");}
    Serial.print(distance5);
    Serial.println("cm");
    
    if (distance5 >= 20) {
    led5.setColor(BLYNK_GREEN);}
    else if (distance5 <= 20) {
    led5.setColor(BLYNK_RED);}

    //Left Lane Keeping.
  long duration6, distance6;
  digitalWrite(trigPin6, HIGH);
  digitalWrite(trigPin6, LOW);
  duration6 = pulseIn(echoPin6, HIGH);
  distance6 = (duration6/2) / 29.1;

   if (distance6 <= 2){
    Blynk.virtualWrite(V1, "In Lane");}
   else if (distance6 >= 2){
    Blynk.virtualWrite(V1, "Adjust Steering");}
    Serial.print(distance6);
    Serial.println("cm");
    Serial.print("\e[1;1H");
 
      if (distance6 = 2) {
    led6.setColor(BLYNK_GREEN);}
    else if (distance6 <= 2) {
    led6.setColor(BLYNK_RED);}
    else if (distance6 >= 2) {
    led6.setColor(BLYNK_RED);}
}

void loop()
{
  Blynk.run();        // run Blynk magic
  timer.run(); // Initiates BlynkTimer
}
1 Like