How to solved about button in blynk and moisture sensor

I have 2 code
I have 4 moisture sensor 4 button in Blynk and 4 solenoid output
first : code have 4 moisture sensor and turn solenoid on when the moisture value reaches the set value
second : code have 4 button in Bynk when I push button on it the solenoid turn on too

but my issue is how can I sum my 2 code to one file because
-when moisture value reaches the set value it have HIGH status like I push the button 1 time

and it have HIGH status again and again like push the button again and again and definitely my soilenoid has toggle off and on
fisrt code here

#include <Adafruit_ADS1015.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>


#define MaxValue 27000
#define MinValue 10000

#define MinMoisture 5
#define MaxMoisture 70
Adafruit_ADS1115 ads(0x48);
const int pinOut[4] = {D5, D6, D7, D8};
char auth[] = "Token";
char ssid[] = "ssid";
char pass[] = "pass";
int statebutton1 = LOW;
int statebutton2 = LOW;
int statebutton3 = LOW;
int statebutton4 = LOW;
int ledState = LOW;
int btnState = HIGH;
BlynkTimer timer;
void setup()
{
  Serial.begin(115200);
  ads.begin();
  pinMode(pinOut[0], OUTPUT);
  pinMode(pinOut[1], OUTPUT);
  pinMode(pinOut[2], OUTPUT);
  pinMode(pinOut[3], OUTPUT);
  Wire.begin(D1, D2);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(500L, swith);

}


int16_t value(int16_t *value, byte length)
{
  for (int x = 0; x < length; x++)
  {
    value[x] = map(ads.readADC_SingleEnded(x), MinValue, MaxValue, 100, 0);
  }
}

void swith()
{
  int16_t Moisture[4];
  byte Moisture_len = sizeof(Moisture) / sizeof(int16_t);

  value(Moisture, Moisture_len);
  Blynk.virtualWrite(V0, Moisture[0]);
  if (Moisture[0] <= MaxMoisture && Moisture[0] > MinMoisture)
  {
    digitalWrite(pinOut[0], HIGH);
    statebutton1 != statebutton1;
  }
  else
  {
    digitalWrite(pinOut[0], LOW);
    digitalWrite(statebutton1, LOW);
  }


  Blynk.virtualWrite(V1, Moisture[1]);
  if (Moisture[1] <= MaxMoisture && Moisture[1] > MinMoisture)
  {
    digitalWrite(pinOut[1], HIGH);
    statebutton2 != statebutton2;
  }
  else
  {
    digitalWrite(pinOut[1], LOW);
    digitalWrite(statebutton1, LOW);
  }

  Blynk.virtualWrite(V2, Moisture[2]);
  if (Moisture[2] <= MaxMoisture && Moisture[2] > MinMoisture)
  {
    digitalWrite(pinOut[2], HIGH);
    statebutton3 != statebutton3;
  }
  else
  {
    digitalWrite(pinOut[2], LOW);
    digitalWrite(statebutton1, LOW);
  }

  Blynk.virtualWrite(V3, Moisture[3]);
  if (Moisture[3] <= MaxMoisture && Moisture[3] > MinMoisture)
  {
    digitalWrite(pinOut[3], HIGH);
    statebutton4 != statebutton4;
  }
  else
  {
    digitalWrite(pinOut[3], LOW);
    digitalWrite(statebutton3, LOW);
  }
  Serial.println(statebutton1);
  Serial.println(statebutton2);
  Serial.println(statebutton3);
  Serial.println(statebutton4);



}
void loop()
{

  Blynk.run();
  timer.run();



}

second code here

#include <Adafruit_ADS1015.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define BLYNK_PRINT Serial
#define MaxValue 27000
#define MinValue 11300
#define MinMoisture 4
#define MaxMoisture 60
Adafruit_ADS1115 ads(0x48);

int ledState = LOW;
int btnState = HIGH;
const int pinOut[4] = {D5, D6, D7, D8};
const int Bly[4] = {V0, V1, V2, V3};
boolean relaybutton1=0 ;
boolean relaybutton2=0 ; 
boolean relaybutton3=0 ; 
boolean relaybutton4=0 ;

char auth[] = "My Token";
char ssid[] = "MyID";
char pass[] = "0943625724";

BlynkTimer timer;


  BLYNK_CONNECTED() {
  Blynk.syncVirtual(V4);
  Blynk.syncVirtual(V5);
  Blynk.syncVirtual(V6);
  Blynk.syncVirtual(V7);
}
  

  BLYNK_WRITE(V4)
    {
      ledState = param.asInt(); // Get the state of the VButton
      digitalWrite(pinOut[0], ledState);
      
    }
    BLYNK_WRITE(V5)
    {
     ledState = param.asInt(); // Get the state of the VButton
     digitalWrite(pinOut[1], ledState);
    }
    BLYNK_WRITE(V6)
    
    {
     ledState = param.asInt(); // Get the state of the VButton
     digitalWrite(pinOut[2],ledState);
    }
    BLYNK_WRITE(V7)
    {
     ledState = param.asInt(); // Get the state of the VButton
     digitalWrite(pinOut[3], ledState);
    }
int16_t value(int16_t *value, byte length)
{
  for (int x = 0; x < length; x++)
  {
    value[x] = map(ads.readADC_SingleEnded(x), MinValue, MaxValue, 100, 0);
  }
}
void readsensor()
{
 
  
  int16_t Moisture[4];
  byte Moisture_len = sizeof(Moisture)/sizeof(int16_t);
  value(Moisture,Moisture_len);

 
  
  Blynk.virtualWrite(V0,Moisture[0]);
  if (digitalRead(relaybutton1)==HIGH){
    if (btnState != LOW) {
      ledState = !ledState;
      digitalWrite(D5, ledState);
      Blynk.virtualWrite(V4, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;

  }

  Blynk.virtualWrite(V1,Moisture[1]);
  if (relaybutton2==HIGH) {
    if (btnState != LOW) {
      ledState = !ledState;
      digitalWrite(D6, ledState);
      Blynk.virtualWrite(V5, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;

  }

  Blynk.virtualWrite(V2,Moisture[2]);
  if (relaybutton3==HIGH) {
    if (btnState != LOW) {
      ledState = !ledState;
      digitalWrite(D7, ledState);
      Blynk.virtualWrite(V6, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;
  }
 

  Blynk.virtualWrite(V3,Moisture[3]);
  if (relaybutton4==HIGH) {
    if (btnState != LOW) {
      ledState = !ledState;
      digitalWrite(D8, ledState);
      Blynk.virtualWrite(V7, ledState);
    }
    btnState = LOW;
  } else {
    btnState = HIGH;

  }

  
   
}
  

 

void setup()
{
  Serial.begin(115200);
  ads.begin();
  pinMode(pinOut[0], OUTPUT);
  pinMode(pinOut[1], OUTPUT);
  pinMode(pinOut[2], OUTPUT);
  pinMode(pinOut[3], OUTPUT);
  Wire.begin(D1, D2);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(500L, readsensor);
  digitalWrite(D5, ledState);
  digitalWrite(D6, ledState);
  digitalWrite(D7, ledState);
  digitalWrite(D8,ledState);
}

void loop()
{

  Blynk.run();
  timer.run();



}

please help me

There is no easy answer, or one way to merge any two, or more, sketches… it all comes down to how well you can analyse what you have, picture what you want and juggle the pieces until they fit… programming 101 :stuck_out_tongue_winking_eye:

So my suggestion is to break each sketch down into parts that are the same in both, and put that into a 3rd sketch (possibly with it’s own App project), then break down any seperate functions/timers, etc. from the initial sketches and copy them over one at a time to your final sketch… testing as you go!

Or like happened last time I advised this… just wait for someone bored enough to do a bad job if it for you :stuck_out_tongue_winking_eye: And as a bonus you won’t learn as much.

I try and try again but I can’t to solve my problem when I merge 2 function in one and I do not sleep 3-4 night bcz this ploblem

I understand… I don’t get any rest from solving others problems :stuck_out_tongue:

Do you know how to toggle button by sensor I need really help

Sorry for even trying to encourage self learning :unamused:

I currently have 3 active projects of my own that I am troubleshooting right now (ironically one is merging various codes together :stuck_out_tongue_winking_eye: )… as well as monitoring this forum. So I can’t help you anymore today then I have already tried… besides all I could do is exactly what I already suggested to you :wink:

HINT - read up on Blynk.syncVirtual(vPin)

Start working on that, show how much you have accomplished, and then perhaps then others will assist with any specific Blynk questions… just don’t ask for someone to do it for you.

thank you I will try again

1 Like

Not sure if this is your problem, or your only problem, but in your second code you use ledState and btnState for every one of your relaybutton checks. I think you need to add distinct variables for each one. For example:

Blynk.virtualWrite(V2,Moisture[2]);
  if (relaybutton3==HIGH) {
    if (btnState3 != LOW) {
      ledState3 = !ledState3;
      digitalWrite(D7, ledState3);
      Blynk.virtualWrite(V6, ledState3);
    }
    btnState3 = LOW;
  } else {
    btnState3 = HIGH;
  }

Dont forget to add

int ledState3 = LOW;
int btnState3 = HIGH;

as well.

I’d do this for every one of your relaybutton checks.