BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Switch Case Interupt

#1

Hello everybody,

I have a problem with my project.

I build this project for my garden watering.

The idea is to start the different valves one after the other, then a short brake and start them again.

Everything works. But now I have the problem to stop the switch case function befor the statement is ended.

Anyone a idea how to solve the problem?

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h> // here is the SimpleTimer library
#include <SPI.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

BlynkTimer timer;
WidgetRTC rtc;   


int Countdown;                // Global variable used in Slider widget and runEveryMinute()
bool ONstatus  = false;       // variable to switch device ON and OFF



// Ampel Belegung der Ausgaenge
const byte GELBPin = D0;
const byte GRUENPin = D1;
const byte BLAUPin = D2;
//
const boolean ein = HIGH;
const boolean aus = LOW;
//
int ZEITBEWAESSERUNG;
int ZEITPAUSE;
//const int ZEITROTPHASE = 3000;
//const int ZEITGELBPHASE = 3000;

unsigned long ampelMillis;
unsigned long ampelIntervall;
//
enum ZUSTAENDE {ZUSTAND1, ROTGELB, GRUEN, GELB, PAUSE,  ZUSTAND2, ZUSTAND3, ZUSTAND4, ZUSTAND5};
byte zustand = ZUSTAND1;


//enum ZUSTAENDE {ROT, ROTGELB, GRUEN, GELB, PAUSE, ZUSTAND1, Zustand2, Zustand3, Zustand4, Zustand5};


int valueAN;
int Starten;



char auth[] = "********************";   //BLYNK SERVER RASPI
char ssid[] = "*************";
char pass[] = "********************";



BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncAll();

  // Alternatively, you could override server state using:
  //Blynk.virtualWrite(V2, ledState);
}


BLYNK_WRITE(V1)   //Starten Intervall
{
  valueAN = param.asInt();
  
  
}


BLYNK_WRITE(V2) {   // add a slider to your project on V0 range 0 to 30 (minutes)
  ZEITPAUSE = param.asInt();  // set variable as Slider value
}


BLYNK_WRITE(V3) {   // add a slider to your project on V0 range 0 to 30 (minutes)
  ZEITBEWAESSERUNG = param.asInt();  // set variable as Slider value
}




void Ventile(){       // Ampelschaltung





   if ((millis() - ampelMillis >= ampelIntervall)&& (valueAN == HIGH)) {
    switch (zustand) {
      case ZUSTAND1:
        if (valueAN != true) break;
        
        digitalWrite(GELBPin, HIGH);
        digitalWrite(GRUENPin, LOW);
        digitalWrite(BLAUPin, LOW);
      
        zustand = ZUSTAND2;
        ampelMillis = millis();
        ampelIntervall = ZEITBEWAESSERUNG * 10;
        break;

      case ZUSTAND2:
        if (valueAN != HIGH) break;
      
        digitalWrite(GELBPin, LOW);
        digitalWrite(GRUENPin, LOW);
        digitalWrite(BLAUPin, LOW);
        zustand = ZUSTAND3;
        ampelMillis = millis();
        ampelIntervall = ZEITBEWAESSERUNG * 10;
        break;

      case ZUSTAND3:
        if (valueAN != HIGH) break;
        
        digitalWrite(GELBPin, LOW);
        digitalWrite(GRUENPin, HIGH);
        digitalWrite(BLAUPin, LOW);
        
        zustand = ZUSTAND4;
        ampelMillis = millis();
        ampelIntervall = ZEITBEWAESSERUNG * 10;
        break;

      case ZUSTAND4:
        if (valueAN != HIGH) break;
        
        digitalWrite(GELBPin, LOW);
        digitalWrite(GRUENPin, LOW);
        digitalWrite(BLAUPin, LOW);
        
        zustand = ZUSTAND5;
        ampelMillis = millis();
        ampelIntervall = ZEITBEWAESSERUNG * 10;
        break;
        
      case ZUSTAND5:
        if (valueAN != HIGH) break;
      
        digitalWrite(GELBPin, LOW);
        digitalWrite(GRUENPin, LOW);
        digitalWrite(BLAUPin, HIGH);
        
        zustand = PAUSE;
        ampelMillis = millis();
        ampelIntervall = ZEITBEWAESSERUNG * 10;
        break;
        
      case PAUSE:
        if (valueAN != HIGH) break;
        
        digitalWrite(GELBPin, LOW);
        digitalWrite(GRUENPin, LOW);
        digitalWrite(BLAUPin, LOW);
        
        zustand = ZUSTAND1;
        ampelMillis = millis();
        ampelIntervall = ZEITPAUSE * 100;           // in Millis
        //ampelIntervall = ZEITPAUSE * 60000; //in Minute
        break;
    }
  }

}

void setup()
{

  Serial.begin(115200); // See the connection status in Serial Monitor
  Blynk.begin(************************);    //RASPI CONF IP Adresse local server
  WiFi.hostname("Intervall_Test_1");


  // Definiert die Pins als Ausgang
  pinMode(GELBPin, OUTPUT);
  pinMode(GRUENPin, OUTPUT);
  pinMode(BLAUPin, OUTPUT);

    rtc.begin();



  timer.setInterval(100L, Ventile);

  
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer to ensure the 60s timer keeps running 
}
0 Likes

#2

Thanks a lot!

1 Like

#3

I try to stop the switch case Statement with:

if (valueAN != HIGH) break;

But it doesn’t work.

0 Likes

#4

I’m not sure if this is the cause of your problem - maybe not, but you’re declaring valueAN as an integer:

int valueAN;

then testing whether it’s HIGH:

   if ((millis() - ampelMillis >= ampelIntervall)&& (valueAN == HIGH)) {

or not true:

if (valueAN != true) break;

Whilst 0/1, LOW/HIGH and false/true should all evaluate to the same thing, it’s certainly messy programming.

When you can’t work-out what is hammening to your logic flow then it’s time to start adding serial print statements to track where you are within your program flow and what is happening to your variables.
Like this…

BLYNK_WRITE(V1)   //Starten Intervall
{
  valueAN = param.asInt();
  Serial.print("valueAN set to ");
  Serial.print(valueAN);
  Serial.println(" in BLYNK_WRITE(V1)");
}

and like this…

    case ZUSTAND1:
      Serial.println("At point case ZUSTAND1 ");
      Serial.print("valueAN = ");
      Serial.println(valueAN);        
      if (valueAN != true)
      {
        Serial.println("About to break");          
        break;
      }
      Serial.println("Still in case ZUSTAND1 ");        
      digitalWrite(GELBPin, HIGH);
      digitalWrite(GRUENPin, LOW);
      digitalWrite(BLAUPin, LOW);

      zustand = ZUSTAND2;
      ampelMillis = millis();
      ampelIntervall = ZEITBEWAESSERUNG * 10;
      break;

Pete.

2 Likes

#5

Hello Pete,

thanks for your answer.

I test it with the Serialmonitor.

If I stop my cycle (valueAN = LOW), the cycle stops in the diffenent (ZUSTAND) and don’t leave the function.

I have no idea what how to solve the Problem.

This evening I will uploade my new Version with documentation. I think it will be easier to understand.

0 Likes

#6
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h> // here is the SimpleTimer library
#include <SPI.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

BlynkTimer timer;
WidgetRTC rtc;   


int Countdown;                // Global variable used in Slider widget and runEveryMinute()
bool ONstatus  = false;       // variable to switch device ON and OFF


// Ampel Belegung der Ausgaenge
const byte relay1 = D0;
const byte relay2 = D1;
const byte relay3 = D2;
//
const boolean ein = HIGH;
const boolean aus = LOW;
//
int ZEITBEWAESSERUNG;
int ZEITPAUSE;
int UNTERBRECHUNG;

    unsigned long ampelMillis;
unsigned long ampelIntervall;
//
enum ZUSTAENDE {ZUSTAND1, ROTGELB, GRUEN, GELB, PAUSE,  ZUSTAND2, ZUSTAND3, ZUSTAND4, ZUSTAND5};
byte zustand = ZUSTAND1;



int valueAN;
int Starten;

int AN;
int AUS;    

char auth[] = "********************";   //BLYNK SERVER RASPI
char ssid[] = "*****";
char pass[] = "*******";

BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncAll();

}

BLYNK_WRITE(V1)   //Starten Intervall
{
  valueAN = param.asInt();

  Serial.print("valueAN set to ");
  Serial.print(valueAN);
  Serial.println(" in BLYNK_WRITE(V1)");
}



BLYNK_WRITE(V2) {   // add a slider to your project on V0 range 0 to 30 (minutes)
  ZEITPAUSE = param.asInt();  // set variable as Slider value
}


BLYNK_WRITE(V3) {   // add a slider to your project on V0 range 0 to 30 (minutes)
  ZEITBEWAESSERUNG = param.asInt();  // set variable as Slider value
}

BLYNK_WRITE(V6) {
  UNTERBRECHUNG = param.asInt();

  if (UNTERBRECHUNG == HIGH)
  {
    Serial.println("Resetting ESP");
    ESP.reset();
  }

}




void setup()
{

  Serial.begin(115200); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass, IPAddress(*********), 8080);    //RASPI CONF IP Adresse     local server
  WiFi.hostname("Intervall_Test_1");


  // Definiert die Pins als Ausgang
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);

  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay3, HIGH);

  rtc.begin();


}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates SimpleTimer to ensure the 60s timer keeps running 





if ((millis() - ampelMillis >= ampelIntervall)&& (valueAN == HIGH)) {
switch (zustand) {
  case ZUSTAND1:

       Serial.println("At point case ZUSTAND1 ");
       Serial.print("valueAN = ");
       Serial.println(valueAN);  
       
    Serial.println("Still in case ZUSTAND1 ");
    
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);

 /*       if ( UNTERBRECHUNG= HIGH) 
    {
  Serial.println("Resetting ESP");
  ESP.restart(); //ESP.reset();
    }
 */ 
    zustand = ZUSTAND2;
    ampelMillis = millis();
    ampelIntervall = ZEITBEWAESSERUNG * 10;
    break;

  case ZUSTAND2:
   if (valueAN != HIGH) break;

       Serial.println("At point case ZUSTAND2 ");
       Serial.print("valueAN = ");
       Serial.println(valueAN);  
  
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    zustand = ZUSTAND3;
    ampelMillis = millis();
    ampelIntervall = ZEITBEWAESSERUNG * 10;
    break;

  case ZUSTAND3:
    if (valueAN != HIGH) break;

       Serial.println("At point case ZUSTAND3 ");
       Serial.print("valueAN = ");
       Serial.println(valueAN);  
    
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, HIGH);
    
    zustand = ZUSTAND4;
    ampelMillis = millis();
    ampelIntervall = ZEITBEWAESSERUNG * 10;
    break;

  case ZUSTAND4:
    if (valueAN != HIGH) break;

       Serial.println("At point case ZUSTAND4 ");
       Serial.print("valueAN = ");
       Serial.println(valueAN);  
    
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    
    zustand = ZUSTAND5;
    ampelMillis = millis();
    ampelIntervall = ZEITBEWAESSERUNG * 10;
    break;
    
  case ZUSTAND5:
    if (valueAN != HIGH) break;

       Serial.println("At point case ZUSTAND5 ");
       Serial.print("valueAN = ");
       Serial.println(valueAN);  
  
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, LOW);
    
    zustand = PAUSE;
    ampelMillis = millis();
    ampelIntervall = ZEITBEWAESSERUNG * 10;
    break;
    
  case PAUSE:
    if (valueAN != HIGH) break;

       Serial.println("At point case PAUSE ");
       Serial.print("valueAN = ");
       Serial.println(valueAN);  
    
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    
    zustand = ZUSTAND1;
    ampelMillis = millis();
    ampelIntervall = ZEITPAUSE * 100;           // in Millis
    //ampelIntervall = ZEITPAUSE * 60000; //in Minute
    break;
}
 }

}
0 Likes

#7

I solved it!

I use an ESP.reset(). To start my Sketch from the begin.

Not nice. But it works :slight_smile:

0 Likes

#8

That’s not the way to do it.

Posting your revised code without any serial output and commentary about which button you pressed at each point you received a serial output serves no purpose whatsoever.

You’re still doing strange comparisons as well. An integer is a whole number. In this case it will be a 1 or a 0, not HIGH/LOW, true/false etc.
In many situations these will evaluate to the same thing, but if you’re having problems with how if statements are being evaluated and go back to basics and test if valueAN == 1 or valueAN==0.

Pete.

2 Likes

#9

Did you “turn it off and on” again… :heavy_check_mark:

Did you fix the problem… :x:

1 Like

#10

Never works :joy:

0 Likes

#11

No solution until yet.

My code works fine. Also with the reset.

I will Keep you in touch if I have time to work again on this Project.

My interval watering runs now for About a week without a problem.

I use the reset only to stop my switch case function.

Greets,
Florian

0 Likes