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();
}