3 sensor connect to 3 Motor

My Problem is With Blynk
First Mode auto it work normal
Then i change Mode Manual it work well
When i change back to Auto it not work
if i make for only one sensor it work well but when use 3 same sensor with 3 motor it not work well

  • Arduino NodeMCU
  • Soil Moisture Sensor(i use Digital)
  • Arduino 4 Channel Relay Module
  • DC Pump Motor

Sensor1 (16) control Motor1 (14)
Sensor2 (05) control Motor2 (12)
Sensor3 (04) control Motor3 (13)

cpp

#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

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

int sensorData;
int button2;
int button3;
int button4;
SimpleTimer timer;
int selectmode;
int i;
int timer2;
int val;  
int val2;
int val3;

WidgetLED led1(V1);
WidgetLED led2(V3);
WidgetLED led3(V5);

WidgetLED led4(V7);
WidgetLED led5(V8);


void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(14,OUTPUT); 
  pinMode(12,OUTPUT);  
  pinMode(13,OUTPUT);  
  
  pinMode(16,INPUT);  
  pinMode(05,INPUT);  
  pinMode(04,INPUT);    


}


void Auto()
{
{
  if( i==0 && selectmode ==0)
{ 
  val = digitalRead(16);
  if(val == LOW ) 
  {
  digitalWrite(14,HIGH);  
  delay(20);
  }
  else 
  {
  digitalWrite(14,LOW);  
  delay(20);
  }
  
 //..........................................................................................................................................
  val2 = digitalRead(16);
  if(val2 == LOW ) 
  {
  digitalWrite(12,HIGH);  
  delay(20);
  }
  else 
  {
  digitalWrite(12,LOW);  
  delay(20);
  }
  }
  
 //..........................................................................................................................................
  val3 = digitalRead(16);
  if(val3 == LOW ) 
  {
  digitalWrite(13,HIGH);  
  delay(20);
  }
  else 
  {
  digitalWrite(13,LOW); 
  delay(20);
  }
}
   Blynk.virtualWrite(V0,val);  // Sensor 1
    Blynk.virtualWrite(V2,val2); // Sensor 2
     Blynk.virtualWrite(V4,val3); // Sensor 33
timer2=timer.setTimeout(1000L,OFF);
}

void OFF()
{
  timer.deleteTimer(timer2);

}


void Manual()
{
  
    
    if(button2==1&&selectmode==1)
    {
    digitalWrite(14,LOW);
    led1.on();
 
  }
 else
 {
  digitalWrite(14,HIGH);
  led1.off();

  }
  
 //..........................................................................................................................................
  
    if(button3==1&&selectmode==1)
    {
    digitalWrite(12,LOW);
    led2.on();

  }
 else
 {
  digitalWrite(12,HIGH);
  led2.off();

  }


 //..........................................................................................................................................
  
    if(button4==1&&selectmode==1)
    {
    digitalWrite(13,LOW);
    led3.on();

  }
 else
 {
  digitalWrite(13,HIGH);
  led3.off();
  }

}
  




void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

BLYNK_WRITE(V9) // Case 1 ONOFF
{
 button2 = param.asInt(); 
}
BLYNK_WRITE(V10) // Case 2 ONOFF
{
 button3 = param.asInt(); 
}
BLYNK_WRITE(V11) // Case 3 ONOFF
{
 button4 = param.asInt(); 
}

BLYNK_WRITE(V6) //Select Mode
{
 selectmode = param.asInt();
 if(selectmode ==1){

    led5.on();
    led4.off();
     timer.setInterval(1000L, Manual);   

 }
 else{

   led4.on();
   led5.off();
   timer.setInterval(15000, Auto);

   
 }
}

BLYNK_CONNECTED(){
  Blynk.syncVirtual(V6); //Select Mode
  Blynk.syncVirtual(V9); // Case 1 ONOFF
  Blynk.syncVirtual(V10); //Case 2 ONOFF
  Blynk.syncVirtual(V11); //Case 3 ONOFF

}

Well the code is a little tricky to follow but some things I noticed that MAY be causing your issue

Here you use V6 to initiate the auto and manual timers, but they never get turned off. So essentially they could both be running together. So when you are in auto mode it gets to the if statements in the manual function and most/all of them will not be true therefore the else statement will be executed.

Also, it seems that your Auto function has an extra set of brackets, and

is not contained within the first if statement as the other two are. Not sure if this was done on purpose.

And I am not sure of the point of creating a timer just to delete it 1 second later.

1 Like

thank you so much i newbie …
yes2 it always on then off like blink the output (motor)
how to change the V6

the Auto function my mistake it suppose
val = 16
val2 =05
val3= 04

i also not sure about timer delete 1 sec because i follow this automanualpump

This will depend on what you are trying to accomplish. As you do not want both timers enabled at the same time, you will need to disable one while enabling the other.

Which is what I would expect with both the auto mode and manual mode functioning at the same time.

Just blindly following someone elses code is probably not the best idea. You need to understand why they are doing what they are doing, and determine if this is what you are trying to achieve. You need to understand what is going on in the code first.

I suggest you take some time to learn about programming and its basic functions, like if/else functions. YouTube and Google are a good place to start. You will also want to read about SimpleTimer and how to properly utilize its functions. SimpleTimer can be a very powerful tool, and is almost a MUST for BLYNK. You will also need to study the BLYNK DOCS, and make sure you understand the BLYNK functions/commands, how they relate to certain widgets and how/when to properly use them.

After all of that take another look at your code and try to fix the things mentioned in my first post. If you are still struggling, then post your “fixed” code and we (the BLYNK Community) will see if we can provide any guidance/support and get you on the right track.

1 Like

Thank you so much i had learn many thing it work well
but went i go to Auto Back it slow a bit to trigger …
I put disable timer at V6 (the timer i dedclare 2 that for Auto and manual)
Correct me if i wrong …
Thank you

cpp

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
// include your ESP library here
// and other blynk widget
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>                                         
#include <SPI.h>

// Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " b0b1b74bc5d8464cb5e3f3c4443a8ad9";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "nikzhafran";
char pass[] = "678250nik";

int button2; //Manual Blynk Case 1 ONOFF
int button3; //Manual Blynk Case 2 ONOFF
int button4; //Manual Blynk Case 3 ONOFF
SimpleTimer timer;  // allocate a name (mytimer) to the timer
int selectmode; //Select Mode for Auto or Manual
int timer2; //Manual Blynk
int timer3; //Auto Blynk
int val;  // Sensor 1
int val2; // Sensor 2
int val3; // Sensor 3

//Configuration of the widges used at the Blynk panel in the mobile
WidgetLED led1(V1); //virtual led Blynk for Motor 1
WidgetLED led2(V3); //virtual led Blynk for Motor 2
WidgetLED led3(V5); //virtual led Blynk for Motor 3
WidgetLED led4(V7); //virtual led Blynk for Auto Mode
WidgetLED led5(V8); //virtual led Blynk for Manual Mode


void setup()
{
  // Debug console
  Serial.begin(115200);     // Set ESP8266 baud rate
  Blynk.begin(auth, ssid, pass); // Here your Arduino connects to the Blynk Cloud.
  //Motor 
  pinMode(14,OUTPUT);  
  pinMode(12,OUTPUT);  
  pinMode(13,OUTPUT);  
  //Sensor
  pinMode(16,INPUT);  
  pinMode(05,INPUT);  
  pinMode(04,INPUT);    

}


void Auto()
{
  if(selectmode ==0)
{ 
  val = digitalRead(16);
  if(val == HIGH ) //if sensor 1 high
  {
  digitalWrite(14,LOW);  
    led1.on(); // light 1 blynk on
  }
  else 
  {
  digitalWrite(14,HIGH);  
    led1.off(); // light 1 blynk off
  }
  
 //..........................................................................................................................................
  val2 = digitalRead(05);
  if(val2 == HIGH ) //if sensor 2 high
  {
  digitalWrite(12,LOW);//Motor 1 On  
    led2.on(); // light 2 blynk on
  }
  else 
  {
  digitalWrite(12,HIGH); //Motor 1 Off  
    led2.off(); // light 2 blynk off
  }
  }
  
 //..........................................................................................................................................
  val3 = digitalRead(04);
  if(val3 == HIGH ) //if sensor 3 high
  {
  digitalWrite(13,LOW);  //Motor 2 On  
    led3.on();// light 3 blynk on
  }
  else 
  {
  digitalWrite(13,HIGH); //Motor 2 Off  
    led3.off(); // light 3 blynk off
    
  Blynk.virtualWrite(V0,val);  // This code will update the virtual port 0 (Sensor 1)
  Blynk.virtualWrite(V2,val2); // This code will update the virtual port 2 (Sensor 2)
  Blynk.virtualWrite(V4,val3); // This code will update the virtual port 4 (Sensor 3)
  }
}



void Manual()
{
    if(button2==1&&selectmode==1)
    {
    digitalWrite(14,LOW);//Motor 1 On  
    led1.on(); // light 1 blynk on
    }
 else
 {
  digitalWrite(14,HIGH);//Motor 1 Off  
  led1.off(); // light 1 blynk off

  }
  
 //..........................................................................................................................................
  
    if(button3==1&&selectmode==1)
    {
    digitalWrite(12,LOW); //Motor 2 On  
    led2.on(); // light 2 blynk on
    }
 else
 {
  digitalWrite(12,HIGH);//Motor 2 Off  
  led2.off(); // light 2 blynk off
  }

 //..........................................................................................................................................
  
    if(button4==1&&selectmode==1)
    {
    digitalWrite(13,LOW);//Motor 3 On  
    led3.on(); // light 3 blynk on
    }
 else
 {
  digitalWrite(13,HIGH);//Motor 3 Off  
  led3.off(); // light 3 blynk off
  }
}
  
void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // call the simple timer routine
}

// When App button is pushed - switch the state for v9
BLYNK_WRITE(V9) // Case 1 ONOFF
{
 button2 = param.asInt(); // Get the state of the VButton for Case 1 ONOFF
}

// When App button is pushed - switch the state for v10
BLYNK_WRITE(V10) // Case 2 ONOFF
{
 button3 = param.asInt(); // Get the state of the VButton for Case 2 ONOFF
}

// When App button is pushed - switch the state for v11
BLYNK_WRITE(V11) // Case 3 ONOFF
{
 button4 = param.asInt(); // Get the state of the VButton for case 3 ONOFF
}

// Sets btnPin HIGH or LOW depending on state of Button Widget
BLYNK_WRITE(V6) //Select Mode
{
 selectmode = param.asInt(); // Get the state of the VButton for selectmode
 if(selectmode ==1){
timer.disable(timer3); // Disables the timer3
    led5.on(); // light 5 blynk on
    led4.off(); // light 4 blynk off
timer2=timer.setInterval(1000L, Manual);   // Setup a function to be called every 1000 ms
 }
 else
{
timer.disable(timer2); //Disables the timer2
   led4.on(); // light 4 blynk on
   led5.off(); // light 5 blynk off
  timer3=timer.setInterval(15000, Auto); // Setup a function to be called every 1500 ms
 }
}

BLYNK_CONNECTED(){
  Blynk.syncVirtual(V6); //Select Mode
  Blynk.syncVirtual(V9); // Case 1 ONOFF
  Blynk.syncVirtual(V10); //Case 2 ONOFF
  Blynk.syncVirtual(V11); //Case 3 ONOFF

}

Taking a quick look at the code I only see one thing that may be an issue. In your AUTO() function it appears that you have a bracket out of place.

You Have:

void Auto()
{
  if(selectmode ==0)
{ 
  val = digitalRead(16);
  if(val == HIGH ) //if sensor 1 high
  {
  digitalWrite(14,LOW);  
    led1.on(); // light 1 blynk on
  }
  else 
  {
  digitalWrite(14,HIGH);  
    led1.off(); // light 1 blynk off
  }
  
 //..........................................................................................................................................
  val2 = digitalRead(05);
  if(val2 == HIGH ) //if sensor 2 high
  {
  digitalWrite(12,LOW);//Motor 1 On  
    led2.on(); // light 2 blynk on
  }
  else 
  {
  digitalWrite(12,HIGH); //Motor 1 Off  
    led2.off(); // light 2 blynk off
  }
  }
  
 //..........................................................................................................................................
  val3 = digitalRead(04);
  if(val3 == HIGH ) //if sensor 3 high
  {
  digitalWrite(13,LOW);  //Motor 2 On  
    led3.on();// light 3 blynk on
  }
  else 
  {
  digitalWrite(13,HIGH); //Motor 2 Off  
    led3.off(); // light 3 blynk off
    
  Blynk.virtualWrite(V0,val);  // This code will update the virtual port 0 (Sensor 1)
  Blynk.virtualWrite(V2,val2); // This code will update the virtual port 2 (Sensor 2)
  Blynk.virtualWrite(V4,val3); // This code will update the virtual port 4 (Sensor 3)
  }
}

And it should probably be:

void Auto()
{
  if(selectmode ==0)
{ 
  val = digitalRead(16);
  if(val == HIGH ) //if sensor 1 high
  {
  digitalWrite(14,LOW);  
    led1.on(); // light 1 blynk on
  }
  else 
  {
  digitalWrite(14,HIGH);  
    led1.off(); // light 1 blynk off
  }
  
 //..........................................................................................................................................
  val2 = digitalRead(05);
  if(val2 == HIGH ) //if sensor 2 high
  {
  digitalWrite(12,LOW);//Motor 1 On  
    led2.on(); // light 2 blynk on
  }
  else 
  {
  digitalWrite(12,HIGH); //Motor 1 Off  
    led2.off(); // light 2 blynk off
  }

  
 //..........................................................................................................................................
  val3 = digitalRead(04);
  if(val3 == HIGH ) //if sensor 3 high
  {
  digitalWrite(13,LOW);  //Motor 2 On  
    led3.on();// light 3 blynk on
  }
  else 
  {
  digitalWrite(13,HIGH); //Motor 2 Off  
    led3.off(); // light 3 blynk off
   }
    
  Blynk.virtualWrite(V0,val);  // This code will update the virtual port 0 (Sensor 1)
  Blynk.virtualWrite(V2,val2); // This code will update the virtual port 2 (Sensor 2)
  Blynk.virtualWrite(V4,val3); // This code will update the virtual port 4 (Sensor 3)
  }
 }

As for the lag I suspect it is because you have the timer set for 15 seconds, not 1.5 seconds as you comment states. You have an extra 0.

timer3=timer.setInterval(15000, Auto); // Setup a function to be called every 1500 ms
1 Like

i see there had extra bracket at led 2 off…
ok i will change it thank you so much :blush:

Ok i had done complete code it work very well and perfect

cpp
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
// include your ESP library here
// and other blynk widget
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>                                         
#include <SPI.h>

// Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " b0b1b74bc5d8464cb5e3f3c4443a8ad9";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "nikzhafran";
char pass[] = "678250nik";

int button2; //Manual Blynk Case 1 ONOFF
int button3; //Manual Blynk Case 2 ONOFF
int button4; //Manual Blynk Case 3 ONOFF
SimpleTimer timer;  // allocate a name (mytimer) to the timer
int selectmode; //Select Mode for Auto or Manual
int timer2; //Manual Blynk
int timer3; //Auto Blynk
int val;  // Sensor 1
int val2; // Sensor 2
int val3; // Sensor 3

//Configuration of the widges used at the Blynk panel in the mobile
WidgetLED led1(V1); //virtual led Blynk for Motor 1
WidgetLED led2(V3); //virtual led Blynk for Motor 2
WidgetLED led3(V5); //virtual led Blynk for Motor 3
WidgetLED led4(V7); //virtual led Blynk for Auto Mode
WidgetLED led5(V8); //virtual led Blynk for Manual Mode


void setup()
{
  // Debug console
  Serial.begin(115200);     // Set ESP8266 baud rate
  Blynk.begin(auth, ssid, pass); // Here your Arduino connects to the Blynk Cloud.
  //Motor 
  pinMode(14,OUTPUT);  
  pinMode(12,OUTPUT);  
  pinMode(13,OUTPUT);  
  //Sensor
  pinMode(16,INPUT);  
  pinMode(05,INPUT);  
  pinMode(04,INPUT);    

}


void Auto()
{
  if(selectmode ==0)
{ 
  val = digitalRead(16);
  if(val == HIGH ) //if sensor 1 high
  {
  digitalWrite(14,LOW);  
    led1.on(); // light 1 blynk on
  }
  else 
  {
  digitalWrite(14,HIGH);  
    led1.off(); // light 1 blynk off
  }
  
 //..........................................................................................................................................
  val2 = digitalRead(05);
  if(val2 == HIGH ) //if sensor 2 high
  {
  digitalWrite(12,LOW);//Motor 1 On  
    led2.on(); // light 2 blynk on
  }
  else 
  {
  digitalWrite(12,HIGH); //Motor 1 Off  
    led2.off(); // light 2 blynk off
  }
  
 //..........................................................................................................................................
  val3 = digitalRead(04);
  if(val3 == HIGH ) //if sensor 3 high
  {
  digitalWrite(13,LOW);  //Motor 2 On  
    led3.on();// light 3 blynk on
  }
  else 
  {
  digitalWrite(13,HIGH); //Motor 2 Off  
    led3.off(); // light 3 blynk off
  }
  Blynk.virtualWrite(V0,val);  // This code will update the virtual port 0 (Sensor 1)
  Blynk.virtualWrite(V2,val2); // This code will update the virtual port 2 (Sensor 2)
  Blynk.virtualWrite(V4,val3); // This code will update the virtual port 4 (Sensor 3)
  }
}



void Manual()
{
    if(button2==1&&selectmode==1)
    {
    digitalWrite(14,LOW);//Motor 1 On  
    led1.on(); // light 1 blynk on
    }
 else
 {
  digitalWrite(14,HIGH);//Motor 1 Off  
  led1.off(); // light 1 blynk off

  }
  
 //..........................................................................................................................................
  
    if(button3==1&&selectmode==1)
    {
    digitalWrite(12,LOW); //Motor 2 On  
    led2.on(); // light 2 blynk on
    }
 else
 {
  digitalWrite(12,HIGH);//Motor 2 Off  
  led2.off(); // light 2 blynk off
  }

 //..........................................................................................................................................
  
    if(button4==1&&selectmode==1)
    {
    digitalWrite(13,LOW);//Motor 3 On  
    led3.on(); // light 3 blynk on
    }
 else
 {
  digitalWrite(13,HIGH);//Motor 3 Off  
  led3.off(); // light 3 blynk off
  }
}
  
void loop()
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // call the simple timer routine
}

// When App button is pushed - switch the state for v9
BLYNK_WRITE(V9) // Case 1 ONOFF
{
 button2 = param.asInt(); // Get the state of the VButton for Case 1 ONOFF
}

// When App button is pushed - switch the state for v10
BLYNK_WRITE(V10) // Case 2 ONOFF
{
 button3 = param.asInt(); // Get the state of the VButton for Case 2 ONOFF
}

// When App button is pushed - switch the state for v11
BLYNK_WRITE(V11) // Case 3 ONOFF
{
 button4 = param.asInt(); // Get the state of the VButton for case 3 ONOFF
}

// Sets btnPin HIGH or LOW depending on state of Button Widget
BLYNK_WRITE(V6) //Select Mode
{
 selectmode = param.asInt(); // Get the state of the VButton for selectmode
 if(selectmode ==1){
timer.disable(timer3); // Disables the timer3
    led5.on(); // light 5 blynk on
    led4.off(); // light 4 blynk off
timer2=timer.setInterval(1000L, Manual);   // Setup a function to be called every 1000 ms
 }
 else
{
timer.disable(timer2); //Disables the timer2
   led4.on(); // light 4 blynk on
   led5.off(); // light 5 blynk off
  timer3=timer.setInterval(1500, Auto); // Setup a function to be called every 1500 ms
 }
}

BLYNK_CONNECTED(){
  Blynk.syncVirtual(V6); //Select Mode
  Blynk.syncVirtual(V9); // Case 1 ONOFF
  Blynk.syncVirtual(V10); //Case 2 ONOFF
  Blynk.syncVirtual(V11); //Case 3 ONOFF

}
1 Like