Blynk connected but BLYNK_WRITE() not triggering

I am working on a project “TrashBot” which collects trash around their surroundings
while doing the program the blynk_write() was not triggering whenever i press a button in the blynk website or android app
Arudino model - Arudino Mega 2560
Esp8226


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Servo.h>

#define BLYNK_TEMPLATE_ID "TMPLvd0V6xSm"
#define BLYNK_DEVICE_NAME "Primek"
#define BLYNK_AUTH_TOKEN "RlTRF5P3d-goboFbuRs_6D5uT__Y8nyL"

char auth[] = "RlTRF5P3d-goboFbuRs_6D5uT__Y8nyL";
char ssid[] = "TP Link extender";
char pass[] = "11030721";

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, "blynk.cloud", 80);
  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();
}

Despite this YouTube video appearing to show a working project…

any Blynk regulars looking at this code know that it wouldn’t work reliably with Blynk, because of all the blocking delays.

The problems with this code are too numerous to mention, and as you’ve obviously taken someone else’s code rather than writing your own I guess that you probably don’t have the coding skills to fix these issues.

My advice would be to either contact the original author of the code (there’s an email address in the YouTube video description but as this is 4 years old and probably never really worked properly, I doubt that you’ll get very far) or to pick a different project for your assignment.

If you do a quick search of this forum for “trashbot” or one of the fairly unique text strings from the code such as “NON METALLIC BIN FULL” you’ll see that others have been warned about the delays, and the topic usually degenerates quite quickly into the author begging for someone to write the code for them, which doesn’t happen.

Pete.

2 Likes