Can a button change a variable in a function()

i see the button can call a pin to go high or low - but can a Blynk button (or slider) change a variable in a function?

i want to change between “warming” and “cooling” in my thermostat program that i have on my ESP8266.

currently i just have to change the variable (called setTemp) and re-upload the code and change the ‘setTemp’ to eg. 16 for cooling or 24’c for warming.

but changing the variable via two Blynk buttons would be fantastic. (i.e. cooling button changes the variable to 16 and warming button changes the variable to 24)

it would be even better if i could use a 9 variable slider.

are there any examples that i should be reviewing??

Attach Slider to Virtual Pin 1
Declare a global variable ( temp for example)
Pass it to a function. Done

Here is some pseudo code

int temp

BLYNK_WRITE(V1){
   temp = param.asInt();
}

someFunction(temp){ //pass variable to function
 //do something with temp variable 
}

thanks!

i should be able to work with that!!!

so exciting that Blynk is delivering every step of the way of this project…

i started with ‘monitoring’, then started ‘notifying’ and now moving to actually ‘controlling’ :smile:

2 Likes

I use custom sliders for my Lego trains to control speed. You can setup the Slider in the dashboard to go from point A to point B, for example, from 0 - 8. This way you can limit the number of inputs the slider can deliver. Combine that with Pavel’s code and you’re almost there :wink:

yes, i have set the temperature slider to go from 16 to 25, but still working out a few extra things, but it seems to compile ok…

i keep getting errors…

where in the sketch do i put the

  {
    setTemp = param.asInt()
  }```

You should put it out of any loop

thanks,

i added a semicolon after param.asInt() too - that seemed to help…

1 Like

That is kind of mandatory :slight_smile:

2 Likes

yep :smile: and next time it wont take me 25 minutes to work that out!

<----still learning everything!!

2 Likes

It’s a bit like learning grammar of a new language. It becomes harder with age, but once you figure it out, it will be all the more fun :slight_smile:

1 Like

OK,

so if i have:

BLYNK_WRITE(V20) //slider
  {
    setTemp = param.asInt(); 
  }
  
BLYNK_WRITE(V21) //button
  {
    setCooling = param.asInt(); 
  }

BLYNK_WRITE(V22) //button
  {
   setWarming = param.asInt(); 
  } 

then

i can do

void ventLogic()

if (setWarming == 1 && roofTemp > setTemp) 
{
digitalWrite(roofVent, HIGH)
}

else if (setWarming == 1 && roofTemp < setTemp) 
{
digitalWrite(roofVent, LOW)
}

and so on?

but how do i make it so V21 turns off when i press V22?

hmmm - time for sleep…

thanks for your help so far :relieved:

Your if statements still lack ; at the end of the line :wink:

You can also use more then one comparator in your if statement. I would highly recommend excessive use of the parenthesis to see your logic, for example:

if ( (setWarming == 1) && ( roofTemp < setTemp) && (setWarming == 0) )

You can also combine it with the || operator (which means OR, very useful!). In the above statement all three expressions have to be true, you could also make the last one (setWarming != 1), it sounds the same, but you can achieve other sorts of logic by using this, not really with boolean logic, but in case of multiple value’s it can come in handy.

If you want to save some memory you can also declare the 0/1 variables as bool (boolean), saves some bits in memory :wink:

1 Like

thank you again!

so here is my proposed logic section:


      ////cooling scenario----------------------------------------------------------
      //roof air is cooler than set temp

      if ((setCooling == 1) && (livingTemp > roofTemp) && (roofTemp < setTemp) && (houseTemp > roofTemp) && (externTemp > roofTemp))
      {
        digitalWrite(roofVent, HIGH); // roof vent OPEN
        digitalWrite(houseVent, LOW); // house vent closed
        digitalWrite(externVent, LOW); // external vent closed
        digitalWrite(upstairsFan, LOW); // ventilation fan is ON
        Serial.println(F("system = ROOF air inlet selected based on roof temp being lowest temp"));
        Serial.println(F("------------"));
        Blynk.virtualWrite(7, 0); //hse LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(8, 1023); //rof LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(9, 0); //ext LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(10, 0); //nil LED on Blynk app
        Blynk.run();
        Blynk.email("name@hackermail.com", "Subject: Upstairs Ventilation fan on - ROOF VENT: cooling", "The ventilation fan was activated...");
        Blynk.tweet((String("Upstairs: ROOF Vent selected - R:") + roofTemp + ("H:") + houseTemp + ("E:") + externTemp + ("L:") + livingTemp) + ("'C"));
      }

      //house air is cooler than set temp

      if ((setCooling == 1) && (livingTemp > houseTemp) && (houseTemp < setTemp) && (externTemp > houseTemp) && (roofTemp > houseTemp))
      {
        digitalWrite(roofVent, LOW); // roof vent closed
        digitalWrite(houseVent, HIGH); // house vent OPEN
        digitalWrite(externVent, LOW); // external vent closed
        digitalWrite(upstairsFan, LOW); // ventilation fan is ON
        Serial.println(F("system = HOUSE air inlet selected based on roof temp being lowest temp"));
        Serial.println(F("------------"));
        Blynk.virtualWrite(7, 1023); //hse LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(8, 0); //rof LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(9, 0); //ext LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(10, 0); //nil LED on Blynk app
        Blynk.run();
        Blynk.email("name@hackermail.com", "Subject: Upstairs Ventilation fan on - HOUSE VENT: cooling", "The ventilation fan was activated...");
        Blynk.tweet((String("Upstairs: HOUSE Vent selected - R:") + roofTemp + ("H:") + houseTemp + ("E:") + externTemp + ("L:") + livingTemp) + ("'C"));
      }
      //external air is cooler than set temp

      if ((setCooling == 1) && (livingTemp > externTemp) && (externTemp < setTemp) && (houseTemp > externTemp) && (roofTemp > externTemp))
      {
        digitalWrite(roofVent, LOW); // roof vent closed
        digitalWrite(houseVent, LOW); // house vent closed
        digitalWrite(externVent, HIGH); // external vent OPEN
        digitalWrite(upstairsFan, LOW); // ventilation fan is ON
        Serial.println(F("system = EXTERNAL air inlet selected based on external temp being lowest temp"));
        Serial.println(F("------------"));
        Blynk.virtualWrite(7, 0); //hse LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(8, 0); //rof LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(9, 1023); //ext LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(10, 0); //nil LED on Blynk app
        Blynk.run();
        Blynk.email("name@hackermail.com", "Subject: Upstairs Ventilation fan on - EXTERNAL VENT: cooling", "The ventilation fan was activated...");
        Blynk.tweet((String("Upstairs: EXTERNAL Vent selected - R:") + roofTemp + ("H:") + houseTemp + ("E:") + externTemp + ("L:") + livingTemp) + ("'C"));
      }


      ////warming scenario----------------------------------------------------------
      //roof air is warmer than set temp

      if ((setWarming == 1) && (livingTemp < roofTemp) && (roofTemp > setTemp) && (houseTemp < roofTemp) && (externTemp < roofTemp))
      {
        digitalWrite(roofVent, HIGH); // roof vent OPEN
        digitalWrite(houseVent, LOW); // house vent closed
        digitalWrite(externVent, LOW); // external vent closed
        digitalWrite(upstairsFan, LOW); // ventilation fan is ON
        Serial.println(F("system = ROOF air inlet selected based on roof temp being highest temp"));
        Serial.println(F("------------"));
        Blynk.virtualWrite(7, 0); //hse LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(8, 1023); //rof LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(9, 0); //ext LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(10, 0); //nil LED on Blynk app
        Blynk.run();
        Blynk.email("name@hackermail.com", "Subject: Upstairs Ventilation fan on - ROOF VENT: warming", "The ventilation fan was activated...");
        Blynk.tweet((String("Upstairs: ROOF Vent selected - R:") + roofTemp + ("H:") + houseTemp + ("E:") + externTemp + ("L:") + livingTemp) + ("'C"));
      }

      //house air is warmer than set temp

      if ((setWarming == 1) && (livingTemp < houseTemp) && (houseTemp > setTemp) && (roofTemp < houseTemp) && (externTemp < houseTemp))
      {
        digitalWrite(roofVent, LOW); // roof vent closed
        digitalWrite(houseVent, HIGH); // house vent OPEN
        digitalWrite(externVent, LOW); // external vent closed
        digitalWrite(upstairsFan, LOW); // ventilation fan is ON
        Serial.println(F("system = HOUSE air inlet selected based on house temp being highest temp"));
        Serial.println(F("------------"));
        Blynk.virtualWrite(7, 1023); //hse LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(8, 0); //rof LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(9, 0); //ext LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(10, 0); //nil LED on Blynk app
        Blynk.run();
        Blynk.email("name@hackermail.com", "Subject: Upstairs Ventilation fan on - HOUSE VENT: warming", "The ventilation fan was activated...");
        Blynk.tweet((String("Upstairs: HOUSE Vent selected - R:") + roofTemp + ("H:") + houseTemp + ("E:") + externTemp + ("L:") + livingTemp) + ("'C"));
      }
      //external is warmer than set temp

      if (setWarming == 1 && livingTemp < externTemp && externTemp > setTemp && houseTemp < externTemp && roofTemp < externTemp)
      {
        digitalWrite(roofVent, LOW); // roof vent closed
        digitalWrite(houseVent, LOW); // house vent closed
        digitalWrite(externVent, HIGH); // external vent OPEN
        digitalWrite(upstairsFan, LOW); // ventilation fan is ON
        Serial.println(F("system = EXTERN air inlet selected based on external temp being highest temp"));
        Serial.println(F("------------"));
        Blynk.virtualWrite(7, 0); //hse LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(8, 0); //rof LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(9, 1023); //ext LED on Blynk app
        Blynk.run();
        Blynk.virtualWrite(10, 0); //nil LED on Blynk app
        Blynk.run();
        Blynk.email("name@hackermail.com", "Subject: Upstairs Ventilation fan on - EXTERN VENT: warming", "The ventilation fan was activated...");
        Blynk.tweet((String("Upstairs: EXTERN Vent selected - R:") + roofTemp + ("H:") + houseTemp + ("E:") + externTemp + ("L:") + livingTemp) + ("'C"));
      }

i have not field tested it as i am awaiting arrival of more DHT22 sensors…

will this logic mean that i can set a temperature using slider, e.g. 23, and then select the warming button and the code will determine which intake has the warmest air and turn the fan on on the condition that the selected vent air is warmer than the living room air AND warmer than the set temperature?

also - will that blynk.tweet string work?

also - if i have the two buttons, is there a way of Blynk “unpressing” the warming button if i change from warming to cooling - or do i have to unpress the button in the app myself after i press the cooling button?

why are you calling Blynk.run() every time?

hmmm, because i read somewhere that it could help with sending it?

thinking back - it was when i was having trouble with Blynk working - but now i recall that once i removed all of the delays i had in my program this fixed the issue, so now i guess i should stop calling it!!!

my bad :frowning:

i just call it once in setup? right?

not in the set up, in the void loop()

We have a state syncing, but it works only for virtual pins. However you can wrap the digital write into virtual as well

1 Like

not sure how to get around wrapping the digital into the virtual yet - maybe a worked example could help?

but here’s my simplified logic - in simplifying it i recognised a few ways to make it better!

do you think it will be OK like this and never have a scenario that is not accounted for?

i have everything to 2 decimal places, but i can see where a comparison might be 22.14 vs 22.14 - in which case there would be no TRUE result? but this would not cause a major issue with my system’s intended operations (i think!)

////cooling scenario----------------------------------------------------------
//roof vent has coolest air and is lower than set temp

if ((setCooling == 1) && (roofTemp > livingTemp) && (roofTemp > setTemp) && (houseTemp > roofTemp) && (externTemp > roofTemp))
{
  digitalWrite(roofVent, HIGH); // roof vent OPEN
  digitalWrite(houseVent, LOW); // house vent closed
  digitalWrite(externVent, LOW); // external vent closed
  digitalWrite(upstairsFan, LOW); // ventilation fan is ON
}

//house vent air is cooler than the living room air AND is higher than set temp AND has coolet vent air

if ((setCooling == 1) && (houseTemp > livingTemp) && (houseTemp > setTemp) && (externTemp > houseTemp) && (roofTemp > houseTemp))
{
  digitalWrite(roofVent, LOW); // roof vent closed
  digitalWrite(houseVent, HIGH); // house vent OPEN
  digitalWrite(externVent, LOW); // external vent closed
  digitalWrite(upstairsFan, LOW); // ventilation fan is ON
}

//external vent air is cooler than the living room air AND is higher than set temp AND has coolet vent air

if ((setCooling == 1) && (externTemp > livingTemp) && (externTemp > setTemp) && (houseTemp > externTemp) && (roofTemp > externTemp))
{
  digitalWrite(roofVent, LOW); // roof vent closed
  digitalWrite(houseVent, LOW); // house vent closed
  digitalWrite(externVent, HIGH); // external vent OPEN
  digitalWrite(upstairsFan, LOW); // ventilation fan is ON
}

//no vent air is cooler than the living room air

if ((setCooling == 1) && (livingTemp < externTemp) && (livingTemp < roofTemp) && (livingTemp < houseTemp));
{
  digitalWrite(roofVent, LOW); // roof vent closed
  digitalWrite(houseVent, LOW); // house vent closed
  digitalWrite(externVent, LOW); // external vent closed
  digitalWrite(upstairsFan, HIGH); // ventilation fan is OFF
}

////----------------------warming scenario----------------------------------------------------------
//roof vent air is warmer than the living room air AND is lower than set temp AND has warmest vent air

if ((setWarming == 1) && (roofTemp > livingTemp) && (roofTemp < setTemp) && (houseTemp < roofTemp) && (externTemp < roofTemp))
{
  digitalWrite(roofVent, HIGH); // roof vent OPEN
  digitalWrite(houseVent, LOW); // house vent closed
  digitalWrite(externVent, LOW); // external vent closed
  digitalWrite(upstairsFan, LOW); // ventilation fan is ON
}

//house vent air is warmer than the living room air AND is lower than set temp AND has warmest vent air

if ((setWarming == 1) && (houseTemp > livingTemp) && (houseTemp < setTemp) && (roofTemp < houseTemp) && (externTemp < houseTemp))
{
  digitalWrite(roofVent, LOW); // roof vent closed
  digitalWrite(houseVent, HIGH); // house vent OPEN
  digitalWrite(externVent, LOW); // external vent closed
  digitalWrite(upstairsFan, LOW); // ventilation fan is ON
}

//external vent air is warmer than the living room air AND is lower than set temp AND has warmest vent air

if ((setWarming == 1) && (externTemp > livingTemp) && (externTemp < setTemp) && (houseTemp < externTemp) && (roofTemp < externTemp))
{
  digitalWrite(roofVent, LOW); // roof vent closed
  digitalWrite(houseVent, LOW); // house vent closed
  digitalWrite(externVent, HIGH); // external vent OPEN
  digitalWrite(upstairsFan, LOW); // ventilation fan is ON
}

//no vent air is warmer than the living room air

if ((setWarming == 1) && (livingTemp > externTemp) && (livingTemp > houseTemp) && (livingTemp > externTemp))
{
  digitalWrite(roofVent, LOW); // roof vent closed
  digitalWrite(houseVent, LOW); // house vent closed
  digitalWrite(externVent, LOW); // external vent closed
  digitalWrite(upstairsFan, HIGH); // ventilation fan is OFF
}

There are probably shorter versions of this, but you’ll figure that out along the journey :slight_smile: (with a little help here and there of course).

I see you have basically two options, setCooling or setWarmer, you could also do as this to make your code a little more readable:

#include blynkstuffthings

// Declare global vars
int setCool;
int setWarm;
int temp1, temp2, temp3, temp4

BLYNK_WRITE(V20) // Button attached to V20 set to push mode
{
  if(param.asInt()) // This is programmer short hand for: if (param.asInt == 1)
  {
     setCool == 1;
    doHeaterStuff(); // Call the routine to set your stuff
  }
  else
  {
     setCool == 0
  }
}

void setup()
{
  // Do setup stuff
}

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

void doHeaterStuff()
}
  if(setCooling == 1)
  {
    // Cool on
    if(temp1 < temp2 && temp3 > temp4) // Etc.
    {
      // Do cool stuff, set pins etc.
    }
  }
  else if(setWarming == 1)
  {
    // Warm on
    if (temp1 > temp2 && temp3 <temp4) // Etc.
    {
      // Do warm stuff, set pins etc.
    } 
  }
  else
  {
    // Default action, both setWarm and setCool are 0
  }
}

I’ve written it a bit out, you can fill in the blanks I think. What also helps, and I see you are well on your way doing that, is adding loads of comments. When you pass a certain amount of code and/or logic nothing seems to make sense anymore, unless you document it :slight_smile:

1 Like