Issues with Arduino mega and ultra Sonic sensors

#define BLYNK_PRINT Serial

 

#include <ESP8266_Lib.h>

#include <BlynkSimpleShieldEsp8266.h>

#include <Servo.h>

 

char auth[] = "*******************************";

char ssid[] = "**********";

char pass[] = "****************";

 

const int outputpin1 = 52;

const int outputpin2 = 53;

int servoPin1 = 8;

int servoPin2 = 9;

int servoPin3 = 12;

int servoPin4 = 11; 

int stop1=4;

int stop2=7;

int back1=2;

int back2=5;

int pwm1=3;

int pwm2=6;

int pos=150;

int pos1=30;

int pos2=150;

int val1;

int val2;

boolean trash;

 

#define EspSerial Serial1

#define ESP8266_BAUD 115200

#define bintrig1 22

#define binecho1 23

#define bintrig2 24

#define binecho2 25

#define lefttrig 26

#define leftecho 27

#define righttrig 28

#define rightecho 29

#define rttrig 30

#define rtecho 31

#define rbtrig 32

#define rbecho 33

#define lttrig 34

#define ltecho 35

#define lbtrig 36

#define lbecho 37

 

ESP8266 wifi(&EspSerial);

 

BlynkTimer timer;

Servo myservo1;

Servo myservo2;

Servo mainservo;

Servo binservo;

 

void myTimerEvent()

{

  Blynk.virtualWrite(V5, millis() / 1000);

}

 

void setup()

{

 

  Serial.begin(9600);

  EspSerial.begin(ESP8266_BAUD);

  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

  pinMode(outputpin1, INPUT);

  pinMode(outputpin2, INPUT);

  myservo1.attach(11);

  myservo2.attach(12);

  mainservo.attach(8);

  binservo.attach(9);

  binservo.write(0);

  myservo1.write(pos1);

  myservo2.write(pos2);

  mainservo.write(pos);

  pinMode(stop1, OUTPUT);

  pinMode(stop2, OUTPUT);

  pinMode(back1, OUTPUT);

  pinMode(back2, OUTPUT);

  pinMode(pwm1, OUTPUT);

  pinMode(pwm2, OUTPUT);

  

  pinMode(bintrig1,OUTPUT);

  pinMode(binecho1,INPUT);

  pinMode(bintrig2,OUTPUT);

  pinMode(binecho2,INPUT);

   pinMode(lefttrig,OUTPUT);

  pinMode(leftecho,INPUT);

   pinMode(righttrig,OUTPUT);

  pinMode(rightecho,INPUT);

   pinMode(rttrig,OUTPUT);

  pinMode(rtecho,INPUT);

   pinMode(rbtrig,OUTPUT);

  pinMode(rbecho,INPUT);

   pinMode(lttrig,OUTPUT);

  pinMode(ltecho,INPUT);

   pinMode(lbtrig,OUTPUT);

  pinMode(lbecho,INPUT);

}

BLYNK_WRITE(1)

{

  if (param.asInt()==1) {      

        pin1_activated();  

    }

   

  }

  BLYNK_WRITE(3)

{

  if (param.asInt()==1) {      

        forward();

        delay(100);

        Stop();  

    }

  

  }

BLYNK_WRITE(4)

{

  if (param.asInt()==1) {      

        backward();

        delay(100);

        Stop();

       

    }

   

  }

  BLYNK_WRITE(5)

{

  if (param.asInt()==1) {      

        left();

        delay(100);

        Stop();

    }

   

  }

   BLYNK_WRITE(6)

{

  if (param.asInt()==1) {      

        right();

        delay(100);

        Stop();

    }

 

  }

 BLYNK_WRITE(7)

{

  if (param.asInt()==1) {       

        Stop();

    }

   

  }

  BLYNK_WRITE(12)

{

  if (param.asInt()==1) {      

        forward();  

    }

   

  }

  BLYNK_WRITE(13)

{

  if (param.asInt()==1) {      

        backward();  

    }

   

  }

BLYNK_WRITE(2)

{

  int x = param[0].asInt();

  int y = param[1].asInt();

  if(y>220)

  {forward();}

  else if(y<35)

  {backward();}

  else if(x>220)

  {right();}

  else if(x<35)

  {left();}

  else

  {Stop();}

}

 

int autostate;

BLYNK_WRITE(11)

{

  autostate=param.asInt();

while(autostate==1){

  autonomous();}

 

 

  }

 

void autonomous(){

  int ldistance,rdistance,rtdistance,rbdistance,ltdistance,lbdistance,lduration,rduration,rtduration,rbduration,ltduration,lbduration;

   digitalWrite (lefttrig, HIGH);

   delayMicroseconds (1000);

   digitalWrite (lefttrig, LOW);

   lduration = pulseIn (leftecho, HIGH);

   ldistance = (lduration/2) / 29.1;

 

   digitalWrite (righttrig, HIGH);

   delayMicroseconds (1000);

   digitalWrite (righttrig, LOW);

   rduration = pulseIn (rightecho, HIGH);

   rdistance = (rduration/2) / 29.1;

 

  digitalWrite (rttrig, HIGH);

   delayMicroseconds (1000);

   digitalWrite (rttrig, LOW);

   rtduration = pulseIn (rtecho, HIGH);

   rtdistance = (rtduration/2) / 29.1;

 

   digitalWrite (rbtrig, HIGH);

   delayMicroseconds (1000);

   digitalWrite (rbtrig, LOW);

   rbduration = pulseIn (rbecho, HIGH);

   rbdistance = (rbduration/2) / 29.1;

 

  digitalWrite (lttrig, HIGH);

   delayMicroseconds (1000);

   digitalWrite (lttrig, LOW);

   ltduration = pulseIn (ltecho, HIGH);

   ltdistance = (ltduration/2) / 29.1;

 

   digitalWrite (lbtrig, HIGH);

   delayMicroseconds (1000);

   digitalWrite (lbtrig, LOW);

   lbduration = pulseIn (lbecho, HIGH);

   lbdistance = (lbduration/2) / 29.1;

while(ldistance<0){

  ldistance=399;}

  while(rdistance<0){

  rdistance=399;}

  while(rtdistance<0){

  rtdistance=399;}

  while(ltdistance<0){

  ltdistance=399;}

  while(rbdistance<0){

  rbdistance=399;}

  while(lbdistance<0){

  lbdistance=399;}

 

 

  aforward();

if(rtdistance<28||ltdistance<28){

    Stop();

    if(rdistance>28&&ldistance>28){

        if(rdistance>=ldistance){

          aright();

          delay(4000);

          Stop();}

          else if(rdistance<ldistance){

            aleft();

            delay(4000);

            Stop();}}

     else if(rdistance<28||ldistance<28){

         if(rdistance<28&&ldistance>28){

          aleft();

          delay(4000);

          Stop();}

          else if(rdistance>28&&ldistance<28){

            aright();

            delay(4000);

            Stop();}

            else if(rdistance<28&&ldistance<28){

              aright();

              delay(8000);

              Stop();}}      

        }

 

 if(lbdistance<20||rbdistance<20&&rtdistance>28&&ltdistance>28){

  Stop();

  delay(100);

  if(lbdistance<20&&rbdistance<20){

    abackward();

    delay(1300);

    Stop();

    pin1_activated();

    delay(1000);}

  else if(lbdistance<20&&rbdistance>20){

    aleft();

    delay(300);

    Stop();

    abackward();

    delay(1300);

    Stop();

    pin1_activated();

    delay(1000);}

 

   else if(lbdistance>20&&rbdistance<20){

    aright();

    delay(300);

    Stop();

    abackward();

    delay(1300);

    Stop();

   pin1_activated();

    delay(1000);}  

  }

  else {

    aforward();}

 

 

 

 

 

 

}

 

void Stop()

{

  analogWrite(pwm1, 0);

  analogWrite(pwm2, 0);

}

 

void left(){

  analogWrite(pwm1, 127);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm2, 127);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  }

void aleft(){

  analogWrite(pwm1, 30);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm2, 30);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  }

void right(){

  analogWrite(pwm1, 127);

  digitalWrite(back1, HIGH);

  analogWrite(pwm2, 127);

  digitalWrite(back2, HIGH);

  }

  void aright(){

  analogWrite(pwm1, 30);

  digitalWrite(back1, HIGH);

  analogWrite(pwm2, 30);

  digitalWrite(back2, HIGH);

  }

 

void backward(){

  analogWrite(pwm1, 127);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm2, 127);

  digitalWrite(back2, HIGH);

  } 

void abackward(){

  analogWrite(pwm1, 30);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm2, 30);

  digitalWrite(back2, HIGH);

  } 

  void forward(){

  analogWrite(pwm2, 127);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm1, 127);

  digitalWrite(back1, HIGH);

  }

void aforward(){

  analogWrite(pwm2, 30);

  digitalWrite(back1, LOW);

  digitalWrite(back2, LOW);

  analogWrite(pwm1, 30);

  digitalWrite(back1, HIGH);

  }

void pin1_activated(){

 

 delay(1000);

   

    for (pos = 150; pos >= 45; pos -= 1) {

    mainservo.write(pos);             

    delay(7);                   

  }

    delay(1000);

    for (pos1=30,pos2=150;pos1>=0,pos2<=180;pos1-=1,pos2+=1){

    myservo1.write(pos1);

    myservo2.write(pos2);

    delay(15);}

   

    delay(1000);

   

for (pos = 45; pos <= 105; pos += 1) {

    // in steps of 1 degree

    mainservo.write(pos);         

    delay(10);                    

  }

   

    delay(3000);

    val1=digitalRead(outputpin1);

  

    val2=digitalRead(outputpin2);

   

    if(val1==LOW||val2==LOW){

      binservo.write(180);

      delay(2000);

      }

      if(val1==HIGH&&val2==HIGH){

        binservo.write(0);

        delay(2000);

        }

       delay(4000);

    for (pos = 105; pos <= 150; pos += 1) {   

    mainservo.write(pos);             

    delay(10);                      

  }

  delay(1000);

   for(pos1=0,pos2=180;pos1<=30,pos2>=150;pos1+=1,pos2-=1){

    myservo2.write(pos2);

    myservo1.write(pos1);

    delay(15);}

    delay(1000);

  

   }

  

   void Blynk_Delay(int milli)

{

  int end_time = millis() + milli;

  while (millis() < end_time)

  {

if (Blynk.connected())

{

  Blynk.run();

}

yield();

  }

}

void loop()

{

  int bin1duration,bin1distance;

digitalWrite (bintrig1, HIGH);

   delayMicroseconds (1000);

   digitalWrite (bintrig1, LOW);

   bin1duration = pulseIn (binecho1, HIGH);

   bin1distance = (bin1duration/2) / 29.1;

   Blynk.virtualWrite(V8,bin1distance);

   if(bin1distance<20){

   Blynk.virtualWrite(V9,"METALLIC BIN FULL");

   delay(1000);}

   else if(bin1distance>20){

    Blynk.virtualWrite(V9," ");

   delay(1000);}

   int bin2duration,bin2distance;

digitalWrite (bintrig2, HIGH);

   delayMicroseconds (1000);

   digitalWrite (bintrig2, LOW);

   bin2duration = pulseIn (binecho2, HIGH);

   bin2distance = (bin2duration/2) / 29.1;

   Blynk.virtualWrite(V10,bin2distance);

   if(bin2distance<20){

   Blynk.virtualWrite(V9,"NON METALLIC BIN FULL");

   delay(1000);}

   else if(bin2distance>20){

    Blynk.virtualWrite(V9," ");

   delay(1000);}

 

  

 Blynk.run();

 

  val1=digitalRead(outputpin1);

   delay(10);

    val2=digitalRead(outputpin2);

    delay(10);

 timer.run();

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 



This is the code to control garbage robot connected to blynk via esp8266 wifi 01 module . Arduino mega is used a controlling platform . It has major 6 ultrasonic sensors (4pin) to sense the waste and obstacles on right left top and bottom . The problem is when I switch to auto mode only "lbtrig and lbecho " gives output other 5 US sensors don’t works I noticed that only this lb configuration works alone I changed the pin but the Arduino reads only this lb trigger and echo configuration. Plz help me I stuck halfway.
The above mentioned commands are under “int autostate”

My advice to you is the same as the advice I gave here…

Also, your sketch uses Blynk Legacy, which will stop working on 31st December 2022 (in 40 days time).

Pete.

Sir but I have already done the hardware and manual mode is working no issues only the ultrasonic sensors are stuck can u plz help me out sir

If you totally re-wrote the code to make it “Blynk friendly” , which includes removing all of the blocking delays and clutter from the void loop, and you make the necessary changes to make it work with Blynk IoT then I’d help you to resolve any issues that remain.

Pete.

Sir what do you mean by blynk friendly? Iam not getting that one help me

Start by reading this…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

As I said, all of the delays in the code (except the ones in microseconds that are used to take distance readings) need to be removed to be Blynk friendly. The code structure probably needs to be changed significantly to remove the need for these delays, so it’s really a total re-write.
In addition, the void loop meds to be cleaned-up as described in the article I linked to, so that it’s simply…

void loop()
{
  Blynk.run();        // run Blynk magic
  timer.run();        // run timer every second
}

If you’re going to that much trouble, you should probably think about switching to a more modern MCU with built-in WiFi connectivity such as the ESP32, as the Mega is almost 14 year old technology and has limited memory and processing power for a project like this.

Pete.

Sir is it possible to write code mentioned in void loop to any other void functions .but the problem is the commands included in void loop have so relation to obstacle avoiding commands.The included ones are to measure watee bin level just for that.

Use a timer to call the code that is currently in the void loop.

Pete.

Sir can u plz give me a format of that

All the information you need is in the link I provided.

Pete.