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.
}