RGB LED disconnection and malfunction

I have basically 2 problems in my code and I would like a suggestion from you.(I’m using a nodemcu)
my code is a physical button that triggers a relay and an RGB led. and that I can choose the color of the led for when the relay is on or off basically that’s it. however I thought of adding something like a reduction in the brightness of the led when it is a certain time selected through the timer in the blynk application. the whole code works normally but when I try to do this part of reducing the luminosity the led does not work even when the value is 0 which is how the led should work normally

1st, there is a frequent disconnection between my blynk app and I don’t know if there may be a problem with my cogido. as something I stopped doing or did too much.

2nd the code works very well without the last part that I added now that it would be a timer that sends an int 1 when the time is selected by the user and 0 when the timer ends.
What I need is that during the night the intensity of the led decreases so as not to bother with the brightness so I thought about reducing the brightness by 50% when I was at night times inserted by the user through the timer. All the code without the “nighttime” works perfectly .

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

char auth[] = "*****";
    char ssid[] = "*****";
      char pass[] = "*****";

const int relay = 2; //D4 rele
const int touch = 4; //D2 touch
#define RED 12  
#define BLUE 13 
#define GREEN 14

BlynkTimer timer;
void checkPhysicalButton();
void led();
void ledon();
void ledoff();
int relayState = LOW;
int touchState = HIGH;


//======== LED=======

int onred ;
int onblue;
int ongreen;
int offred ;
int offblue;
int offgreen;
int colorchooseon =1;
int colorchooseoff =1;
#define nighttime V3
int nightcolor;
int night = 0;
  BLYNK_WRITE(V1) {
  colorchooseon = param.asInt();
    }

BLYNK_WRITE(V2){
  colorchooseoff = param.asInt();
}

BLYNK_WRITE(nighttime){   
  nightcolor = param.asInt();
  if (nightcolor  == 1){  
  night = 0.5;  
  }
  else {
    nightcolor = 1;   
  }
  }



//===================================

BLYNK_CONNECTED() {  
    Blynk.syncVirtual(V0);
    Blynk.syncVirtual(V1);
    Blynk.syncVirtual(V2);
    Blynk.syncVirtual(nighttime);      
  }

BLYNK_WRITE(V0) {
  relayState = param.asInt();
  digitalWrite(relay, relayState);
}



void checkPhysicalButton()
{
  if (digitalRead(touch) == LOW) {
    
    if (touchState != LOW) {
      
      relayState = !relayState;
      digitalWrite(relay, relayState);
      Blynk.virtualWrite(V0, relayState); 
      ledon();
      ledoff();
      led();   
    }
    touchState = LOW;
    
  }
  if (digitalRead(touch) == HIGH) {
          touchState = HIGH;
          ledon();
          ledoff();
          led();   
    }
}

void ledon(){
  
  if(colorchooseon == 1){
     onred = (1024 *night) ;
     onblue= 0;
     ongreen = 0 ; 
        }
   
   if(colorchooseon == 2){
     onred = 0 ;
     onblue= (1024* night);
     ongreen = 0; 
   }
   if(colorchooseon == 3){
     onred = 0 ;
     onblue= 0;
     ongreen = (1024* night);    
}
}
void ledoff(){
  if(colorchooseoff == 1){
    offred = (512*night);
     offblue= 0;
     offgreen = 0;
           
  }
  if(colorchooseoff == 2){
    offred = 0 ;
     offblue= (512 * night);
     offgreen = 0;  
     }
     
     if(colorchooseoff == 3){
    offred = 0 ;
     offblue= 0;
     offgreen = (512*night);     
  }  
}


void led(){
  if (relayState == 1){
    
     analogWrite(RED, onred);    
    analogWrite(BLUE, onblue); 
    analogWrite(GREEN,ongreen); 
      
       
  }  
  
  if (relayState == 0){
        
    analogWrite(RED, offred);    
    analogWrite(BLUE, offblue); 
    analogWrite(GREEN,offgreen);       
    
}
}



void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
   pinMode(relay, OUTPUT);
  pinMode(touch, INPUT_PULLUP);
  digitalWrite(relay, relayState);
  pinMode (RED, OUTPUT); // Pino 3 declarado como saída
          pinMode (BLUE, OUTPUT); // Pino 5 declarado como saída
            pinMode (GREEN, OUTPUT); // Pino 6 declarado como saída  

    timer.setInterval(100L, checkPhysicalButton);
}

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


You have a lot happening g every 100ms, and some of the code isn’t very well written.
Take your checkPhysicalButton function as an example…

You force the MCU to do two digitalRead operations.

if (digitalRead(touch) == LOW) {

if (digitalRead(touch) == HIGH) {

The second if statement could simply be an else because the result of reading the touch pin can only be LOW or HIGH, but in other circumstances the pin could be read once and the result assigned to a local variable which is then evaluated by if or switch/case statements.

Your disconnections are probably being caused by consuming too much processor time with these nested functions that are called every 100ms. This is easy enough to check by temporarily increasing the timer. I guess that adding Blynk.run commands before ledon, ledoff and led are called or processed would help.

As far as your nighttime issue is concerned, I’d add some serial print statements to track your variable values and figure-out where the logic is going wrong.

Pete.

I increased the time to 250ms to do some tests.
I didn’t understand what you meant by adding Blynk.run before the ledon, ledoff. led, would it be like this?

void checkPhysicalButton()
{
  if (digitalRead(touch) == LOW) {
    
    if (touchState != LOW) {
      
      relayState = !relayState;
      digitalWrite(relay, relayState);
      Blynk.virtualWrite(V0, relayState); 
      Blynk.run();
      ledon();
      ledoff();
      led();   
    }
    touchState = LOW;
    
  }
  else{ 
          touchState = HIGH;
          Blynk.run();
          ledon();
          ledoff();
          led();   
  }
}

I also added a virtual one to check if the value as sujeriu and the result would be arriving and that in the red V5 pin I receive the zero value and in the blue V5 I receive the values ​​described there 1023 for on and 512 for off. in other words, the problem is in the multiplication of the night variable.and I don’t know what is wrong because to me theoretically it looked like it would work. :frowning:

Blynk.virtualWrite(V0, relayState); 
Blynk.run();
ledon();
Blynk.run();
ledoff();
Blynk.run();  
led();

An integer variable can only be a whole number. You’d need it to be a float variable to hold a value of 0.5

Pete.

1 Like