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 let me know and tell me how a programmer would write it.
Thanks in meantime
Your tecMaker