Call function() with Segmented Switch

Pete, unfortunately it doesn’t work. Now it’s only the auto function that works. I guess it’s because the function is called to run all the time and not by Case 1 in the Segmented Switch.
The On/Off Cases doesn’t work any longer

Pete, is it possible to do like this or will that only run once as well?

  BLYNK_WRITE(V14){
      switch (param.asInt()) {
    case 1:  //AUTO Selected
    //Start pump when the solar temp. sensor is above PARAM1
    Auto();
    break;
    
    case 2:  // ON Selected
    led1.on();
    digitalWrite(pumpRunLED,HIGH);
    digitalWrite(pumpStart,HIGH);
     break;
    
    case 3:  // OFF Selected
    led1.off();
    digitalWrite(pumpRunLED,LOW);
    digitalWrite(pumpStart,LOW);
    break;
    }
  }

void setup(){
    timer.setInterval(1000L, myTimerEvent);
    timer.setInterval(2000L, call_my_functions);

if (Auto = HIGH){
    timer.setInterval(60000L, pumpautoStart);
    }
}

void pumpautoStart(void){
    
    //Start pump when the conditions are met for PARAM1
  if (tempSensor2>=(tempSensor4+startDT)){
    led1.on();
    digitalWrite(pumpRunLED,HIGH);
    digitalWrite(pumpStart,HIGH); //Note! relay with external supply to be pulled LOW for activating the relay: https://lastminuteengineers.com/two-channel-relay-module-arduino-tutorial/
    }
   
 //Stop pump when differential temperature is below PARAM2 
  if ((tempSensor2-tempSensor4)<stopDT){
    led1.off();
    digitalWrite(pumpRunLED,LOW);
    digitalWrite(pumpStart,LOW); //Note! relay with external supply to be pulled HIGH for de-activating the relay: https://lastminuteengineers.com/two-channel-relay-module-arduino-tutorial/
    }
    Serial.print("startDT: "); Serial.print(startDT); Serial.print(", stopDT: ");Serial.println(stopDT);
  }

void call_my_functions(){
    Temp();
    Pressure();
    Voltage();
  }

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

Or at least something similar but this won’t compile obviously

Jesper

As you know, BLYNK_WRITE(V14) only executes once, when the value of V14 changes.
void setup() also only runs once, when the device boots.

I think you should probably add a global boolean variable called “auto_mode” and set this to true when Case 1 is selected, and false when Case 2 or 3 are selected.

Then, at the beginning of your pumpautoStart() function you gave an if statement which only executes the rest of the code in that function if auto_mode == true.

Pete.

Pete, thanks for prompt response.
I have done as suggested but I have a question or a chalenge to your suggestion. If I want to check if Auto_mode is true ‘lets say every minute or 60000’ can I then not use the timer function:

  if (Auto_mode == true){
    timer.setInterval(60000L, pumpautoStart);
    }

But I see it wont work in void setup() as this only runs once. any suggestions?

Jesper

I didn’t suggest this piece of code:

Pete.

Pete, I see however, I would like the pumpautoStart funtion to run every 60000ms as I need a lag on the start-up due to cold water coming through that I don’t want the pump to stop on.

I have tried this code and it doesn’t work

 BLYNK_WRITE(V14){
      switch (param.asInt()) {
    case 1:  //AUTO Selected
    //Start pump when the solar temp. sensor is above PARAM1
    Auto_mode,true;
    break;
    
    case 2:  // ON Selected
    Auto_mode,false;
    led1.on();
    digitalWrite(pumpRunLED,HIGH);
    digitalWrite(pumpStart,HIGH);
     break;
    
    case 3:  // OFF Selected
    Auto_mode,false;
    led1.off();
    digitalWrite(pumpRunLED,LOW);
    digitalWrite(pumpStart,LOW);
    break;
    }
  }

void pumpautoStart(void){
    //Start pump when the conditions are met for PARAM1
  
  if (Auto_mode == true){
    if (tempSensor2>=(tempSensor4+startDT)){
      led1.on();
      digitalWrite(pumpRunLED,HIGH);
      digitalWrite(pumpStart,HIGH); 
      }
      //Stop pump when differential temperature is below PARAM2 
    if ((tempSensor2-tempSensor4)<stopDT){
      led1.off();
      digitalWrite(pumpRunLED,LOW);
      digitalWrite(pumpStart,LOW); 
      }
    }
  }

I’m surprised that this even compiles!

If you want to set a variable to a value then you use the equals symbol…

Auto_mode=true;

Pete.

Pete, I was in doubt but it compiled okay. Now changed to:

 Auto_mode=true;

Now tested as well and Auto is still not working:

  BLYNK_WRITE(V14){
      switch (param.asInt()) {
    case 1:  //AUTO Selected
    //Start pump when the solar temp. sensor is above PARAM1
    Auto_mode=true;
    break;
    
    case 2:  // ON Selected
    Auto_mode=false;
    led1.on();
    digitalWrite(pumpRunLED,HIGH);
    digitalWrite(pumpStart,HIGH);
     break;
    
    case 3:  // OFF Selected
    Auto_mode=false;
    led1.off();
    digitalWrite(pumpRunLED,LOW);
    digitalWrite(pumpStart,LOW);
    break;
    }
  }

void pumpautoStart(void){
    //Start pump when the conditions are met for PARAM1
  
  if (Auto_mode == true){
    if (tempSensor2>=(tempSensor4+startDT)){
      led1.on();
      digitalWrite(pumpRunLED,HIGH);
      digitalWrite(pumpStart,HIGH);
      }
      //Stop pump when differential temperature is below PARAM2 
    if ((tempSensor2-tempSensor4)<stopDT){
      led1.off();
      digitalWrite(pumpRunLED,LOW);
      digitalWrite(pumpStart,LOW); 
      }
      Serial.print("startDT: "); Serial.print(startDT); Serial.print(", stopDT: ");Serial.println(stopDT);
    }
}

So what exactly does “not working” mean - give me some clues so I know where to look.
It would also be useful if you’d post your latest full code, as code snippets only tell part of the story.

Pete.

Pete, it’s me fooling around I’m very sorry for that. I had removed pumpautoStart(); from the timer function and not put it back in when I tried your suggestions. Auto_mode is working now.
I tried printing to the serial monitor and realised not even the Auto_mode changed state which made me think about pumpautoStart(); in void call_my_functions()

Now I just need to work with a timer to make the delay in stopping the pump within 60-90 secs after it starts to cope for the cold water in the hoses to the solar panel.

Thank you very much Pete

Kind regards
Jesper

1 Like