How to control multiple digital pins at a time (2 or more)?)

How to control 2 or more digital pins at a time using a switch in the dashboard?

I have multiple valves connected to each digital pin of esp8266 via MOSFET PWM, I would like close the 3 valves at a time with the help of a single switch.

thing["manual1"] << digitalPin(valve1, valve2, valve3); // control valve1, 2 & 3

I know the above logic would now work, but just posting for the reference.

Any help would be highly appreciated.

Hi,

You are using the compressed code and it allows to control just one resource, the expanded code looks like:

  thing["InputPins"] << [](pson & in) {
      if (in["Pin1"].is_empty()) in["Pin1"] = valve1;
      else valve1 = in["Pin1"];

      if (in["Pin2"].is_empty()) in["Pin2"] = valve2;
      else valve2 = in["Pin2"];
};

The platform recognizes the variable type and shows the field according, for boolen values shows the state switch, for numbers or text shows a textfield, into the device’s api.

Hope this helps.

1 Like

HI @ega,

Thanks for your reply. But I’m still little confused with your hint. Below is my code for your reference. I would like to switch ON both the valves 3 & 5 with the press of a spargingmanual button in the console. And switch ON the valves 4 & 5 with the press of purgignmanual button.

All the valves are connected to digital pins of ESP8266 via PWM.


  thing["spargingmanual"] << digitalPin(***********); // control valve 3 & valve 5
  thing["purgingmanual"] << digitalPin(*************); // control valve 4 & valve 5 

Could you please explain your code with my logic?

Oh sorry, I missunderstood, I thought you want to add more than one resource in the same control.

Well as you want, what I would do is to define a boolean variable that will rule both pins, one way to do it is by the expanded code:

thing["ControlPins"] << [](pson & in) {
    if (in["Pin1"].is_empty()) in["Pin1"] = booleanVar;
    else booleanVar= in["Pin1"];

   digitalWrite(outputPin1, booleanVar);
   digitalWrite(outputPin2, booleanVar);
};

Note that it will run just every single time you execute a call to that reosurce.

Hope this helps.

1 Like

Yes, that actually worked. Thank you very much.

1 Like

Hi,

Just came across another hurdle. I would really appreciate if someone could help me out.

  1. I wanted to control multiple valves connected to digital pins on a ESP8266 by using a single button in thinger.
  2. And also wanted to show the status of the individual valves in thinger using on/off state switch.

I was able to achieve the first step but couldnt get the 2nd step working. Here’s my code for the thinger control.

thing["spargingmanual"] << [](pson &in){
     if(in.is_empty()){
        in=false;  //when it is empty no changes are made
     }else{
        bool val=in;   
        if(val==true){
               digitalWrite(15,!digitalRead(15));      //new value will be the opposite as before  
               digitalWrite(13,!digitalRead(13));      //new value will be the opposite as before  
               digitalWrite(12,!digitalRead(12));      //new value will be the opposite as before  
               digitalWrite(14,!digitalRead(14));      //new value will be the opposite as before  
               
         }
  //       delay(500);  //this delay is the time it will take for the button to return to its common position (off)
         in=false;
        } 
    };
     thing["purgingmanual"] << [](pson &in){
     if(in.is_empty()){
        in=false;  //when it is empty no changes are made
     }else{
        bool val=in;   
        if(val==true){
               digitalWrite(5,!digitalRead(5));      //new value will be the opposite as before  
               digitalWrite(14,!digitalRead(14));      //new value will be the opposite as before  
               
         }
  //       delay(500);  //this delay is the time it will take for the button to return to its common position (off)
         in=false;
        } 
    };

Also here’s the screenshot of my dashboard

The valves 10 to 15 are connected to sparging manual switch and valve 8 is connected to purging manual switch. If I hit the button sparging manual switch valves 10 to 15 should change the state to ON and so for the purging status.

Thank you.

Hi,

To get the actual status, I would try something like this:

thing["Status"] >> [](pson & out){
out["Pin1"] = digitalRead(pin1);
out["Pin2"] = digitalRead(pin2);
out["Pin3"] = digitalRead(pin3);
};

Hope this helps.