How to call loops right

Hi guys, I am still writing the sketch for a smart illumination with a normal LED strip. No RGB…only usually LED. But I would program more modes. For example Mode1 which can only switch on and off the LED strip, Mode 2 which can dim the LED strip and so on. Can someone tell me a way how to call the different loops. My methode below does not works. I do not know why, (actual they become called usually) but if I comment al expect of the required part from Mode1, Mode 1 works. In the case below, the LED usually blinks.
I would make it so that if I click on the button Mode1 in Blynk App, Mode1 is running. With the second click I reset the the mode and it should switch off. When Mode1 is running and I click on Button Mode 2, Mode 1 becomes reseted and Mode2 runs.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";                       // Blynk Tooken
char ssid[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";                       //Wlan SSID
char pass[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";                       //Wlan Passwort

byte varCentralOut = 0;                    //Variable Zentral Aus        (V20)   Button set to PUSH
byte varMode1 =                            //Variable Modus LED Ein-Aus  (V21)   Button set to PUSH
byte varMode2 = 0;                         //Variable Modus Szene 1      (V22)   Button set to PUSH
byte varLCD_clear = 0;                     //Variable to clear LCD
int varLedOutput = 0;                      //flag to simulate output signal
int LedPin = D3;                           //LED conected to D3
int DimValue = 0;

WidgetLCD lcd (V30);                      //LCD Widget attached to Pin V30
WidgetLED led(V100);                      //LED Widget attached to Pin V100
BlynkTimer timer;                         //Timer called timer
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode (D3, OUTPUT);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
  Blynk.run();
  SerialMonitoring();
  Mode1();
  Mode2();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SerialMonitoring() {                           //Function to control Serial Monitor
  Serial.print    ("varCentralOut is ");
  Serial.println  (varCentralOut);
  Serial.print    ("varMode1 is    ");
  Serial.println  (varMode1);
  Serial.print    ("varMode2 is    ");
  Serial.println  (varMode2);
  Serial.print    ("DimValue is    ");
  Serial.println  (DimValue);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V20)                                      //Function Central Out
{
  int CentralOut = param.asInt();
  if (CentralOut == 1) {
    varMode1 = 0;
    varMode2 = 0;
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V21)                                  //Function to switch LED ON and OFF
{
  int Mode1 = param.asInt();
  if (Mode1 == 1) {
    varMode1++;
    varMode2 = 0;
  }
  if (varMode1 == 2) {
    varMode1 = 0;
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V23)                                     //Function to dim LED (Button)
{
  int Mode2 = param.asInt();
  if (Mode2 == 1) {
    varMode2++;
    varMode1 = 0;
  }
  if (varMode2 == 2) {
    varMode2 = 0;
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V25)                                 //Function to dim LED (Slider Widget)
{
  int DimValue = param.asInt();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Mode1() {

  if ((varMode1 == 1) && (varCentralOut == 0)) {
    varLedOutput = 1;
    digitalWrite (LedPin, HIGH);
    led.setValue(255);
    lcd.print (1, 0, "MODE 1      ON ");
    lcd.print (1, 1, "LED ON/OFF");
  }
  if ((varMode1 == 1) && (varCentralOut == 1)) {
    varLedOutput = 0;
    digitalWrite (LedPin, LOW);
    led.setValue(0);
    lcd.print (1, 0, "MODE 1      OFF");
    lcd.print (1, 1, "LED ON/OFF");
  }
  if ((varMode1 == 0) && (varCentralOut == 0)) {
    varLedOutput = 0;
    digitalWrite (LedPin, LOW);
    led.setValue(0);
    lcd.print (1, 0, "MODE 1      OFF");
    lcd.print (1, 1, "LED ON/OFF");

  }
  if ((varMode1 == 0) && (varCentralOut == 1)) {
    varLedOutput = 0;
    digitalWrite (LedPin, LOW);
    led.setValue(0);
    lcd.print (1, 0, "MODE 1      OFF");
    lcd.print (1, 1, "LED ON/OFF");
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Mode2() {

  if ((varMode2 == 1) && (varCentralOut == 0)) {
    varLedOutput = 1;
    analogWrite (LedPin, DimValue);
    led.setValue(DimValue);
    lcd.print (1, 0, "MODE 2      ON ");
    lcd.print (1, 1, "LED DIMMER");
  }
  if ((varMode2 == 1) && (varCentralOut == 1)) {
    varLedOutput = 0;
    analogWrite (LedPin, 0);
    led.setValue(0);
    lcd.print (1, 0, "MODE 2      OFF");
    lcd.print (1, 1, "LED DIMMER");
  }
  if ((varMode2 == 0) && (varCentralOut == 0)) {
    varLedOutput = 0;
    analogWrite (LedPin, 0);
    led.setValue(0);
    lcd.print (1, 0, "MODE 2      OFF");
    lcd.print (1, 1, "LED DIMMER");

  }
  if ((varMode2 == 0) && (varCentralOut == 1)) {
    varLedOutput = 0;
    analogWrite (LedPin, 0);
    led.setValue(0);
    lcd.print (1, 0, "MODE 2      OFF");
    lcd.print (1, 1, "LED DIMMER");
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

An other way I tried is to call the loops direct from an other loop. So, the loops became not called all usually. But there I end in a infinite loop. If the program is jumped in a loop, it can not leave it.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";                       // Blynk Tooken
char ssid[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";                       //Wlan SSID
char pass[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";                       //Wlan Passwort

byte varCentralOut = 0;                    //Variable Zentral Aus        (V20)   Button set to PUSH
byte varMode1 =                            //Variable Modus LED Ein-Aus  (V21)   Button set to PUSH
byte varMode2 = 0;                         //Variable Modus Szene 1      (V22)   Button set to PUSH
byte varLCD_clear = 0;                     //Variable to clear LCD
int varLedOutput = 0;                      //flag to simulate output signal
int LedPin = D3;                           //LED conected to D3
int DimValue = 0;

WidgetLCD lcd (V30);                      //LCD Widget attached to Pin V30
WidgetLED led(V100);                      //LED Widget attached to Pin V100
BlynkTimer timer;                         //Timer called timer
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode (D3, OUTPUT);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
  Blynk.run();
  SerialMonitoring();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SerialMonitoring() {                           //Function to control Serial Monitor
  Serial.print    ("varCentralOut is ");
  Serial.println  (varCentralOut);
  Serial.print    ("varMode1 is    ");
  Serial.println  (varMode1);
  Serial.print    ("varMode2 is    ");
  Serial.println  (varMode2);
  Serial.print    ("DimValue is    ");
  Serial.println  (DimValue);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V20)                                      //Function Central Out
{
  int CentralOut = param.asInt();
  if (CentralOut == 1) {
    varMode1 = 0;
    varMode2 = 0;
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V21)                                  //Function to switch LED ON and OFF
{
  int Mode1 = param.asInt();
  if (Mode1 == 1) {
    varMode1++;
   FuncMode1();                                    //call the loop FuncMode1
    varMode2 = 0;
  }
  if (varMode1 == 2) {
    varMode1 = 0;
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V23)                                     //Function to dim LED (Button)
{
  int Mode2 = param.asInt();
  if (Mode2 == 1) {
    varMode2++;
    FuncMode2();                                       //call the loop FuncMode2
    varMode1 = 0;
  }
  if (varMode2 == 2) {
    varMode2 = 0;
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BLYNK_WRITE(V25)                                 //Function to dim LED (Slider Widget)
{
  int DimValue = param.asInt();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FuncMode1() {

  if ((varMode1 == 1) && (varCentralOut == 0)) {
    varLedOutput = 1;
    digitalWrite (LedPin, HIGH);
    led.setValue(255);
    lcd.print (1, 0, "MODE 1      ON ");
    lcd.print (1, 1, "LED ON/OFF");
  }
  if ((varMode1 == 1) && (varCentralOut == 1)) {
    varLedOutput = 0;
    digitalWrite (LedPin, LOW);
    led.setValue(0);
    lcd.print (1, 0, "MODE 1      OFF");
    lcd.print (1, 1, "LED ON/OFF");
  }
  if ((varMode1 == 0) && (varCentralOut == 0)) {
    varLedOutput = 0;
    digitalWrite (LedPin, LOW);
    led.setValue(0);
    lcd.print (1, 0, "MODE 1      OFF");
    lcd.print (1, 1, "LED ON/OFF");

  }
  if ((varMode1 == 0) && (varCentralOut == 1)) {
    varLedOutput = 0;
    digitalWrite (LedPin, LOW);
    led.setValue(0);
    lcd.print (1, 0, "MODE 1      OFF");
    lcd.print (1, 1, "LED ON/OFF");
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void FuncMode2() {

  if ((varMode2 == 1) && (varCentralOut == 0)) {
    varLedOutput = 1;
    analogWrite (LedPin, DimValue);
    led.setValue(DimValue);
    lcd.print (1, 0, "MODE 2      ON ");
    lcd.print (1, 1, "LED DIMMER");
  }
  if ((varMode2 == 1) && (varCentralOut == 1)) {
    varLedOutput = 0;
    analogWrite (LedPin, 0);
    led.setValue(0);
    lcd.print (1, 0, "MODE 2      OFF");
    lcd.print (1, 1, "LED DIMMER");
  }
  if ((varMode2 == 0) && (varCentralOut == 0)) {
    varLedOutput = 0;
    analogWrite (LedPin, 0);
    led.setValue(0);
    lcd.print (1, 0, "MODE 2      OFF");
    lcd.print (1, 1, "LED DIMMER");

  }
  if ((varMode2 == 0) && (varCentralOut == 1)) {
    varLedOutput = 0;
    analogWrite (LedPin, 0);
    led.setValue(0);
    lcd.print (1, 0, "MODE 2      OFF");
    lcd.print (1, 1, "LED DIMMER");
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

If you think the code above is all :poop: let me know and tell me how a programmer would write it.
Thanks in meantime
Your tecMaker

Hello.

This is not a code factory or repair shop :wink: But you might want to consider using Menu for your choices and switch case for processing them.

http://docs.blynk.cc/#widgets-interface-menu

https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

Sorry to disturb you again, but I had again no success… this time with “switch cases” :thinking:
I also know, where the error is, but I am not able to remove it :no_mouth:
If the conditions for Mode2 are true I jump into case 6… and until the conditions are true I can not jump out from the loop, so I do not get any value from Slider and the led do not let dim. How I can jump into this “type of loop” but concurrent I want get values from outside the loop?

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "aace274ad74842ea93387312b66fb654";
char ssid[] = "Fritzbox3272";
char pass[] = "67033709486218110510";
int LedPin = D3;
int varSwitchModi = 0;
byte varMainButton = 0;
byte varMode1 = 0;
byte varMode2 = 0;
int DimValue = 0;

WidgetLCD lcd (V30);                                             //LCD Widget attached to Pin V30

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode (D3, OUTPUT);
  lcd.print (1, 0, "SELECT         ");
  lcd.print (1, 1, "MODE           ");
}

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

BLYNK_WRITE(V20)                                                        //Button ON/OFF
{
  int MainButton = param.asInt();
  if (MainButton == 1) {
    varMainButton++;
  }
  if (varMainButton == 2) {
    varMainButton = 0;
  }
}
BLYNK_WRITE(V21)                                                //Function to switch LED ON and OFF
{
  int Mode1 = param.asInt();
  if (Mode1 == 1) {
    varMode1++;
    varMode2 = 0;
  }
  if (varMode1 == 2) {
    varMode1 = 0;
  }
}

BLYNK_WRITE(V23)                                                        //Function to DIM LED
{
  int Mode2 = param.asInt();
  if (Mode2 == 1) {
    varMode2++;
    varMode1 = 0;
  }
  if (varMode2 == 2) {
    varMode2 = 0;
  }
}

BLYNK_WRITE(V25)                                                        //Get DimValue from slider
{
  int DimValue = param.asInt();
}

void FuncModiRun() {
  Serial.print ("MainButton = ");
  Serial.println (varMainButton);
  Serial.print ("varMode1   = ");
  Serial.println (varMode1);
  Serial.print ("varMode2   = ");
  Serial.println (varMode2);
  Serial.print ("varSwitchModi = ");
  Serial.println (varSwitchModi);

  if ((varMainButton == 1) && (varMode1 == 0) || (varMainButton == 1) && (varMode2 == 0)) {
    varSwitchModi = 0;                                 //No Mode selected
  }
  if ((varMainButton == 0) && (varMode1 == 1)) {        //Mode1 selected
    varSwitchModi = 1;
  }
  if ((varMainButton == 0) && (varMode2 == 1)) {        //Mode2 selected
    varSwitchModi = 2;
  }
  
  if ((varMainButton == 1) && (varMode1 == 1)) {        //Mode1 running
    varSwitchModi = 5;
  }
  if ((varMainButton == 1) && (varMode2 == 1)) {        //Mode2 running
    analogWrite (LedPin, DimValue);
    varSwitchModi = 6;
  }

  switch (varSwitchModi) {
    case 0:
      analogWrite (LedPin, 0);
      lcd.print (1, 0, "NO MODE        ");
      lcd.print (1, 1, "SELECTED       ");
      break;
    case 1:
      analogWrite (LedPin, 0);
      lcd.print (1, 0, "MODE 1 SELECTED");
      lcd.print (1, 1, "PRESS TO START ");
      break;
    case 2:
      analogWrite (LedPin, 0);
      lcd.print (1, 0, "MODE 2 SELECTED");
      lcd.print (1, 1, "PRESS TO START ");
      break;
    case 5:
      analogWrite (LedPin, 255);
      lcd.print (1, 0, "MODE 1         ");
      lcd.print (1, 1, "RUNNING        ");
      break;
    case 6:
      analogWrite (LedPin, DimValue);
      lcd.print (1, 0, "MODE 2         ");
      lcd.print (1, 1, "RUNNING        ");
      break;
 
  }
}

I would be verry thankful if someone could tell me a solution, so that I come on and do not have to disturb you again :face_with_raised_eyebrow:

First… calling a function directly from the void loop() is just as bad as running all the stuff in the void loop()… it still tries to run thousands of times a second :stuck_out_tongue_winking_eye:

You really need to use a timer to call that function every few seconds or whenever you need it… even at 250ms (4 times a second) is much better then as it is.

As for the rest… I don’t know… I am working on my own troublesome projects… I just don’t have to resources to troubleshoot others non-blynk related coding issues as well. Sorry.

Okay thank… so I know tomorrow what I will do :wink:
Recode the whole sketch, but no problem it is now only the, I am not sure, but it could be the 10 one I do it :smile:
That is why I love programming :smile:

I don’t see the point in using the switch case function when the variable that’s used for the switch is assigned via a complex set of If statements. You might as well simplify the code by putting the analogWrite and lcd.print statements in the If statements.

At the moment, apart from the fact that you’re calling FuncModiRun() from void loop, I think your code is probably failing because none of the If conditions are being met and you don’t have a ‘case else’ option to deal with these situations.

Pete.

Hi PeteKnight, your solution

"You might as well simplify the code by putting the analogWrite and lcd.print statements in the If statements."

doesn´t work, because it dosn´t synchronize the value from a loop with the value from the other loop. I realized now like this below

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "Fritzboxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxxxx";

int LedPinD3 = D3;
int LedPinD2 = D2;
int LedPinD1 = D1;
byte varMainButton = 0;
byte varSwitchMode1 = 0;
byte varOutput = 0;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode (LedPinD1, OUTPUT);
  pinMode (LedPinD2, OUTPUT);
  pinMode (LedPinD3, OUTPUT);
}

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

BLYNK_WRITE(V20)                                                        //Button ON/OFF
{
  int MainButton = param.asInt();

  if (MainButton == 1) {
    varMainButton = 1;
    digitalWrite (LedPinD3, HIGH);
  }
  if (MainButton == 0) {
    varMainButton = 0;
    digitalWrite (LedPinD3, LOW);
  }
}

BLYNK_WRITE(V21)                                                        //Button SwitchMode1
{
  int SwitchMode1 = param.asInt();
  if (SwitchMode1 == 1) {
    varSwitchMode1 = 1;
    digitalWrite (LedPinD2, HIGH);
  }
  if (SwitchMode1 == 0) {
    varSwitchMode1 = 0;
    digitalWrite (LedPinD2, LOW);
  }
  if ((varSwitchMode1 == 1) && (varMainButton == 1)) {
    varOutput = 1;
    digitalWrite (LedPinD1, HIGH);
  }
  if ((varSwitchMode1 == 1) && (varMainButton == 0)) {
    varOutput = 0;
    digitalWrite (LedPinD1, LOW);
  }
  Serial.print ("varMainButton  = ");
  Serial.println (varMainButton);
  Serial.print ("varSwitchMode1 = ");
  Serial.println (varSwitchMode1);
  Serial.print ("varOutput = ");
  Serial.println (varOutput);
}

But thanks for you help, I go on modifing the sketch with timers :man_facepalming: :rofl:

Well, it seems to me that you’ve got rid of the switch/case statement and are doing all your processing in the If statements, which was what I was advocating for simpler and tidier code!

Pete.

Yeah, but if it would also work, I would be happy. :rofl::man_facepalming:

Hi Gunner, can´t solve it myself. Now I start the loop Mode2, how you tould me, with a timer every 250ms.
At least I found out where the bug in my brain (and in my sketch) is :man_facepalming:
I usually thought if I get a value from slider from Blynk App and declare it at the begin from the sketch as a global variable I can use it from the whole sketch and not only in the loop where I get it. Is there no possibility to use the incomming values global, it would be important. I found out because the value in Serial Monitor was and still is usually 0

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "Fritzboxxxxx";
char pass[] = "xxxxxxxxxxxxxx";
int LedPinD3 = D3;                       //to output the incomming value from slider (doesn´t work)
int LedPinD2 = D2;                          //to check SwitchMode2
int LedPinD1 = D1;                          //to check MainButton
int DimmValue = 0;
byte varMainButton = 0;
byte varSwitchMode1 = 0;
byte varSwitchMode2 = 0;

BlynkTimer timer;


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode (LedPinD1, OUTPUT);
  pinMode (LedPinD2, OUTPUT);
  pinMode (LedPinD3, OUTPUT);
  timer.setInterval(250L, Mode2);
}

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

BLYNK_WRITE(V20)                                                        //Button ON/OFF
{
  int MainButton = param.asInt();
  if (MainButton == 1) {
    varMainButton = 1;
  }
  if (MainButton == 0) {
    varMainButton = 0;
  }
}
BLYNK_WRITE(V22)                                                        //Mode 2 LED DIMMER
{
  int SwitchMode2 = param.asInt();
  if (SwitchMode2 == 1) {
    varSwitchMode2 = 1;
  }
  if (SwitchMode2 == 0) {
    varSwitchMode2 = 0;
  }
}
BLYNK_WRITE(V25)                                                //Get value from Slider
{
  int DimmValue = param.asInt();                             //there I also get the incomming values
  Serial.print("DimmValue = ");
  Serial.println(DimmValue);
}
void Mode2() {
  if (varMainButton == 1) {
    digitalWrite (LedPinD1, HIGH);                    //Check Main Button OK (works)
  }
  if (varMainButton == 0) {
    digitalWrite (LedPinD1, LOW);
  }
  if (varSwitchMode2 == 1) {                            //Check SwitchMode2 OK (works)
    digitalWrite (LedPinD2, HIGH);
  }
  if (varSwitchMode2 == 0) {
    digitalWrite (LedPinD2, LOW);
  }
  if ((varMainButton == 1) && (varSwitchMode2 == 1)){
  analogWrite (LedPinD3, DimmValue);                    //doesn´t work because I get not DimmValue
  Serial.print("DimmValue = ");                                                  
  Serial.println(DimmValue);
}

Would be very happy if I would get an answer. Thx in the meantime :thinking::smiley:

By putting ‘int’ in front of the DimValue variable you’re effectively re-declaring it as local to this piece of code. Remove the ‘int’ here and the variable will be global again.

Pete.

Yeah…short and sweet!
I love such answers.
Thanks bro! :stuck_out_tongue_closed_eyes: