Speeding up my script

Hey guys, I’m attempting to speed my script up and any help is much appreciated. I’m using 6 Sr-04 sensors to provide measurments of surrounding objects. Ideally I need the data to provide the warning message as quickly as possible as it mimics driver warnings. I had all the sensor functions within one ‘void sensor’ but I’ve split them up into 6 individual voids as I thought it may speed it up, no luck though. The measurments don’t seem to be coming in at any set speed, sometimes they come in 1 second gaps and other times it takes awhile for a few to come through and they all come in at once.

#define BLYNK_PRINT Serial

#include <BlynkSimpleSerialBLE.h>

// Auth Token for the Blynk App.
char auth[] = "-Ewz1xFp-iDB7fj0Woeucbgn65bRU5lH";

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

// Attach virtual LEDs to Virtual Pins V2-V7.
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"
#define BLYNK_YELLOW    "#ED9D00"

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() 
{
  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); // Opens Serial Port 2 on the Mega Board for Bluetooth Transfer.
 
  Serial.begin(9600); // Opens Serial Port 1 for Debug in Serial Monitor.
    
  Blynk.begin(Serial2, auth); // Begins Bluetooth Transfer.

  Serial.println("Waiting for connections..."); // Serial Monitor message.

  timer.setInterval(1000, sensor1); // Timer Function for Sensor Data.
  timer.setInterval(1000, sensor2); // Timer Function for Sensor Data.
  timer.setInterval(1000, sensor3); // Timer Function for Sensor Data.
  timer.setInterval(1000, sensor4); // Timer Function for Sensor Data.
  timer.setInterval(1000, sensor5); // Timer Function for Sensor Data.
  timer.setInterval(1000, sensor6); // Timer Function for Sensor Data.
}

void sensor1()
{
// Object Ahead Distance.
  terminal.clear();
  long duration1, distance1;
  digitalWrite(trigPin1, LOW);  // Added this line
  delayMicroseconds(1); // Added this line
  digitalWrite(trigPin1, HIGH);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;
  
   if (distance1 > 40){
    Blynk.virtualWrite(V1, "Nothing Ahead, Okay");}
   else if (distance1 > 5){
    Blynk.virtualWrite(V1, "Vehicle Ahead, Slow");}
   else {
    Blynk.virtualWrite(V1, "Stop");}
    Serial.print(distance1);
    Serial.println("cm");
    
    led1.on();    
     if (distance1 > 40) {
    led1.setColor(BLYNK_GREEN);}
    else if (distance1 > 5){
    led1.setColor(BLYNK_YELLOW);}
    else {
    led1.setColor(BLYNK_RED);}
}

void sensor2()
{
// Right Lane Keeping.
  long duration2, distance2;
  digitalWrite(trigPin2, LOW);  // Added this line
  delayMicroseconds(1); // Added this line
  digitalWrite(trigPin2, HIGH);
  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");

    led2.on();
      if (distance2 = 2) {
    led2.setColor(BLYNK_GREEN);}
    else if (distance2 <= 2) {
    led2.setColor(BLYNK_GREEN);}
    else if (distance2 >= 2) {
    led2.setColor(BLYNK_RED);}
}

   void sensor3()
{
// Rear Right Blind Spot Detection.
  long duration3, distance3;
  digitalWrite(trigPin3, LOW);  // Added this line
  delayMicroseconds(1); // Added this line
  digitalWrite(trigPin3, HIGH);
  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");

    led3.on();
      if (distance3 >= 20) {
    led3.setColor(BLYNK_GREEN);}
    else if (distance3 <= 20) {
    led3.setColor(BLYNK_RED);}
}

void sensor4()
{
// Parking Sensor.
  long duration4, distance4;
  digitalWrite(trigPin4, LOW);  // Added this line
  delayMicroseconds(1); // Added this line
  digitalWrite(trigPin4, HIGH);
  duration4 = pulseIn(echoPin4, HIGH);
  distance4 = (duration4/2) / 29.1;
  
   if (distance4 > 40){
    Blynk.virtualWrite(V1, "Nothing Behind, Okay");}
   else if (distance4 > 5){
    Blynk.virtualWrite(V1, "Object Behing, Slow");}
   else {
    Blynk.virtualWrite(V1, "Stop");}
    Serial.print(distance4);
    Serial.println("cm");
    
    led4.on();    
     if (distance4 > 40) {
    led4.setColor(BLYNK_GREEN);}
    else if (distance4 > 5){
    led4.setColor(BLYNK_YELLOW);}
    else {
    led4.setColor(BLYNK_RED);}
}

void sensor5()
{
// Rear Left Blind Spot Detection.
  long duration5, distance5;
  digitalWrite(trigPin5, LOW);  // Added this line
  delayMicroseconds(1); // Added this line
  digitalWrite(trigPin5, HIGH);
  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");

    led5.on();
    if (distance5 >= 20) {
    led5.setColor(BLYNK_GREEN);}
    else if (distance5 <= 20) {
    led5.setColor(BLYNK_RED);}
}

void sensor6()
{
// Left Lane Keeping.
  long duration6, distance6;
  digitalWrite(trigPin6, LOW);  // Added this line
  delayMicroseconds(1); // Added this line
  digitalWrite(trigPin6, HIGH);
  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");

    led6.on();
      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(); // Runs Blynk.
  timer.run(); // Initiates BlynkTimer.
}

All your timers are set to run at the exact same time.

Stagger them so that the 2nd doesn’t start until the 1st finishes processing, and so on. Look for the Staggering Timers: section in this post…

As you are calling all of the sensors each time you might just as well have only one timer and combine the calls into one.

If it were my code I would change all the magic numbers into defines. Look for common code to make into small routine to be called from the sensor reads and put a delay between the timer.setInterval calls that allowed for the routine to complete before calling the next sensor. As @Gunner hinted.

It will not speed it up that much but it will make it much more predictable.

if you want to speed it up you will need to eliminate as many virtualWrites as you can and all the serial output.

Give it a try with just serial prints. Try commenting out all Blynk writes to see how much of the bottle neck is blynk.

Could be shorter by a bit. This is not a big deal as it works but it is a waste.

This is logically the same and gets rid of two comparisons. Reducing this type of redundancy can make things much faster in a tight loop.

if (distance2 <= 2) led2.setColor(BLYNK_GREEN);
else led2.setColor(BLYNK_RED);

You have similar logic redundancy all over your code.

Another thing you can do to make it run faster.
Define your variables globally so that they do not get reallocated each time a routine runs.

This is a BIG deal
Get rid of the floating point math.

This is Slooooooooow
distance2 = (duration2/2) / 29.1;

It would be much faster if you just divide by 58 or even better if you can accept 64 as that would just be a logical shift left of 6 places.

Here is my version if you need the precision. As you can see I got rid of the extra var duration2

distance2 = pulseIn(echoPin2, HIGH)/58;

Is this working? I think it should be == but it’s irrelevant if you use Gyromikes code…