BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Only one of my virtual buttons work and the others have no response from my robot

#1

Hey guys I’ve made a line follower robot that is supposed to go to the specific rooms it’s assigned to through the app. I have 3 buttons for rooms 1,2, and 3 respectively on the app, but my robot only responds when I press Room1 and not when I press room2 or room3 buttons. I’ve assigned the virtual pins accordingly on the app.
I’m using the ESP8266 with Wi-Fi, two IR sensors for line following and an ultrasonic sensor for collision avoidance.
The buttons worked for me before I added the Ultrasonic sensor and have no idea why it’s not working now.

The code is as follows:

#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial

#define motorPin1 D0
#define motorPin2 D1
#define motorPin3 D2
#define motorPin4 D3
#define SS1 D5
#define SS2 D6
#define trigPin D7
#define echoPin D8

int cnt1,cnt2,cnt3=0;

int room1,room2,room3;
char auth[] = "b26579b935784bf79cc19a30b60614d0";

char ssid[] = "a";
char pass[] = "12345678";

WidgetLCD lcd(V1);

void setup()
{
  Serial.begin(9600);
  lcd.clear();
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(SS1,INPUT_PULLUP);
  pinMode(SS2,INPUT_PULLUP);
      analogWrite(motorPin1, 0);
     analogWrite(motorPin2, 0);
     analogWrite(motorPin3, 0);
     analogWrite(motorPin4, 0); 
     pinMode(trigPin, OUTPUT);
     pinMode(echoPin, INPUT);

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

BLYNK_WRITE(V6)
{
 room1 = param.asInt(); 
    Blynk.run(); 
}
BLYNK_WRITE(V7)
{
 room2 = param.asInt(); 
    Blynk.run(); 
}
BLYNK_WRITE(V8)
{
 room3 = param.asInt(); 
    Blynk.run(); 
}

void loop()
{
    int s1=digitalRead(SS1);
    int s2=digitalRead(SS2);
    Serial.print("ir1 : ");
    Serial.println(s1);
    Serial.print("ir2 : ");
    Serial.println(s2);
    Blynk.run();
    long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

Serial.println(distance);
Blynk.virtualWrite(V5, distance);
if(distance<10)
{
     analogWrite(motorPin1, 0);
     analogWrite(motorPin2, 0);
     analogWrite(motorPin3, 0);
     analogWrite(motorPin4, 0);
  }
else if(room1==1 && distance>10)
{
  lcd.print(0,0, "On the way");
  if(s1==1 && s2==1)
   {
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 350);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 350);
      Blynk.run();
    }
    else if(s1==1 && s2==0)
    {
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 0);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 350);
     Blynk.run();
    }
    else if(s1==0 && s2==1)
    {
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 350  );
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 0); 
      Blynk.run();
    }
    else if(s1==0 && s2==0)
    {
      
      if(cnt1==0){
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 0);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 0);
      lcd.clear();
      lcd.print(0,0, "Reached");
      Blynk.run();
     delay(500);
           Blynk.run();
     delay(500);
           Blynk.run();
     delay(500);   
     Blynk.run(); 
     cnt1=1;
      }
      else
      {
     analogWrite(motorPin1, 0);
     analogWrite(motorPin2, 0);
     analogWrite(motorPin3, 0);
     analogWrite(motorPin4, 0);
     Blynk.run(); 
      } 
   }

else if(room2==1 && distance>10)
{
Blynk.run(); 
  lcd.print(0,0, "On the way");
  if(s1==1 && s2==1)
   {
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 350);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 350);
      Blynk.run(); 
    }
    else if(s1==1 && s2==0)
    {
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 0);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 350);
     Blynk.run(); 
    }
    else if(s1==0 && s2==1)
    {
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 350  );
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 0); 
      Blynk.run(); 
    }
    else if(s1==0 && s2==0)
    {
      
      if(cnt2==0){
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 500);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 500);
      delay(1000);
      Blynk.run();
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 500);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 500); 
     delay(1000);    
           Blynk.run(); 
     delay(500);  
           Blynk.run(); 
     delay(500);  
     cnt2=1;
      }
      
      else if(cnt2==1)
      {
     analogWrite(motorPin1, 0);
     analogWrite(motorPin2, 0);
     analogWrite(motorPin3, 0);
     analogWrite(motorPin4, 0);
      lcd.clear();
      lcd.print(0,0, "Reached");
     delay(3000);
      Blynk.run();
     analogWrite(motorPin1, 0);
     analogWrite(motorPin2, 0);
     analogWrite(motorPin3, 0);
     analogWrite(motorPin4, 0);
     delay(2000);
      Blynk.run(); 
     cnt2=2;  
  
      }
      
      else
      {
     analogWrite(motorPin1, 0);
     analogWrite(motorPin2, 0);
     analogWrite(motorPin3, 0);
     analogWrite(motorPin4, 0);
      Blynk.run(); 
     }    Blynk.run(); 
   }     Blynk.run(); 
}
else if(room3==1 && distance>10)
{
Blynk.run(); 
lcd.clear();
      lcd.print(0,0, "On the way");
  if(s1==1 && s2==1)
   {
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 350);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 350);
      Blynk.run(); 
    }
    else if(s1==1 && s2==0)
    {
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 0);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 350);
     Blynk.run(); 
    }
    else if(s1==0 && s2==1)
    {
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 350  );
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 0); 
      Blynk.run(); 
    }
    else if(s1==0 && s2==0)
    {
      
      if(cnt3==0){
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 500);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 500);
      delay(1000);
      Blynk.run();
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 500);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 500); 
     delay(1000);    
           Blynk.run(); 
     delay(500);  
           Blynk.run(); 
     delay(500);  
     cnt3=1;
      }
      
      else if(cnt3==1)
      {
     analogWrite(motorPin1, 0);
     analogWrite(motorPin2, 500);
     analogWrite(motorPin3, 0);
     analogWrite(motorPin4, 500);
     delay(1000);
      Blynk.run();
     analogWrite(motorPin1, 0);
     analogWrite(motorPin2, 500);
     analogWrite(motorPin3, 0);
     analogWrite(motorPin4, 500);
     delay(1000);
      Blynk.run(); 
     cnt3=2;  
  
      }
      else if(cnt3==2)
      {
      analogWrite(motorPin1, 0);
      analogWrite(motorPin2, 0);
      analogWrite(motorPin3, 0);
      analogWrite(motorPin4, 0);
      delay(500);
      Blynk.run();
      lcd.clear();
      lcd.print(0,0, "Reached");
     delay(500);
      Blynk.run();
     delay(500);   
     Blynk.run();
     cnt3=3;
        }
      else
      {
     analogWrite(motorPin1, 0);
     analogWrite(motorPin2, 0);
     analogWrite(motorPin3, 0);
     analogWrite(motorPin4, 0);
      Blynk.run(); 
     }    Blynk.run(); 
   }     Blynk.run(); 
}
Blynk.run();
}
} ```
0 Likes

#2

I don’t think you’re going top get much constructive advice until you sort your void loop out…

Pete.

0 Likes

#3

Thanks a lot for that, this is my first time using Blynk and I made my void loop too long. It works perfectly now.
Also is there a way for me to control the virtual pin On/Off state automatically as the robot reaches its destination? Thank you in advance

0 Likes

#4

You can change the state of virtual pins using a virtualwrite command:

https://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynkvirtualwritevpin-value

Pete.

0 Likes

#5

These statements make me nervous if distance==10,

if (distance < 10)
else if (room1==1 && distance>10)
else if (room2==1 && distance>10)
else if (room3==1 && distance>10)

I assume the robot will keep moving and eventually distance will be less than 10. However, I’d be more comfortable with,

if (distance < 10)
else if (room1==1)
else if (room2==1)
else if (room3==1)

Now that you have it working, I think it would be worthwhile to rewrite your firmware with,

void loop(void) {
   Blynk.run();
   timer.run();
}

Eliminate all of the other calls to Blynk.run() and all of the calls to delay().

There’s a lot of redundancy. Start packaging things up into functions. For example,

void analogWriteMotorPin(int value1, int value2, int value3, int value4) {
      analogWrite(motorPin1, value1);
      analogWrite(motorPin2, value2);
      analogWrite(motorPin3, value3);
      analogWrite(motorPin4, value4);
}

Just a suggestion …

2 Likes