LED Dimmer + Blynk =?

Hello Experts!
Help Projects tie!
As this dimmer insert Blynk? To be able to drive the LED with a physical button, and the application (virtual button)?

#define BUTTON1 8
#define led 3
int scd1=0;
int ligh1  = 255;
int stateD1 = HIGH;
int P1 = 0;
int P2 = 250;

void setup() {
 pinMode(BUTTON1,INPUT);
 pinMode(led,OUTPUT);
}
 void loop(){
 if ( digitalRead(BUTTON1)==HIGH) {//счетчик кнопки 
   scd1++;
   } 
 else {
   scd1=0;
 }
  delay(5);
if ( scd1 >= 100 && digitalRead(BUTTON1)==HIGH) { // длинное нажатие
   if (P1 == 1 && P2 >2 &&  stateD1 == LOW ){// увеличиваем яркость
     P2--;
     analogWrite(led ,P2);
     delay(5);
    }
   
   if (P1 == 1 && P2 < 251 &&  stateD1 == HIGH ){//уменьшаем яркость
    P2++;
    analogWrite(led ,P2);
     delay(5);
    }
 }
  if ( scd1 >= 100 && digitalRead(BUTTON1)==LOW) {  
   stateD1 = !stateD1;
 } 
 
 
 if ( scd1 > 0 && scd1 < 100 && digitalRead(BUTTON1)==LOW ) { //короткое нажатие включаем или выключаем лампу
   if (P1 == 0){
   ligh1 = P2;
   P1 = 1;
   analogWrite(led ,ligh1);
   }
  else {
   ligh1 = 0;
   P1 = 0;
   analogWrite(led ,ligh1);
   }
}
 if (P1 != 0) {ligh1 = 251-P2;}
 else {ligh1 = 255;}
 }
`#include <SimpleTimer.h>
#define BUTTON1 8
#define led 3
int scd1=0;
int ligh1  = 255;
int stateD1 = LOW;
int P1 = 0;
int P2 = 255;

SimpleTimer timer;
void checkPhysicalButton();
void setup() {
 pinMode(BUTTON1,INPUT);
 pinMode(led,OUTPUT);
timer.setInterval(10L, checkPhysicalButton);
}

void checkPhysicalButton()
{ 
  if (digitalRead(BUTTON1)==HIGH) {//счетчик кнопки 
   scd1++;
   } 
 else {
   scd1=0;
 }
  delay(10);

if ( scd1 >= 100 && digitalRead(BUTTON1)==HIGH) { // длинное нажатие
   if (P1 == 1 && P2 >1 &&  stateD1 == LOW ){// увеличиваем яркость
     P2--;
     analogWrite(led ,P2);
     delay(5);
    }
   
   if (P1 == 1 && P2 < 254 &&  stateD1 == HIGH ){//уменьшаем яркость
    P2++;
    analogWrite(led ,P2);
     delay(5);
   
    }
 }
  if ( scd1 >= 100 && digitalRead(BUTTON1)==LOW) {  
   stateD1 = !stateD1;
 } 
 
 if ( scd1 > 0 && scd1 < 100 && digitalRead(BUTTON1)==LOW ) { //короткое нажатие включаем или выключаем лампу
   if (P1 == 0){
   ligh1 = P2;
   P1 = 1;
   analogWrite(led ,ligh1);
 
   }
  else {
   ligh1 = 0;
   P1 = 0;
   analogWrite(led ,ligh1);
   
   }
}
 if (P1 != 0) 
 {ligh1 = 254-P2;}
 else {ligh1 = 255;}
 }
 void loop(){
  timer.run();
 }

Hi,

Not sure what are you trying to achieve. Also, have you tried the simplest Blynk examples? What’s your plan on connecting to the Internet?

Hi,
Yes I tried the simplest Blynk examples
I use NodeMCU!

When soliciting the help of the Blynk forum, it’s best to,

  1. …be using Blynk. Seriously, at least make an attempt to use the blynk library and commands.

  2. …describe your project. No one here knows in advance what you’re attempting to do, and your case isn’t helped by the use of variables that are only meaningful to you (e.g. scds1, P1, P2).

That said, I’m guessing that you’ve got this bit of code that works for your purpose, and that you’d just like to add the ability to virtually “push” BUTTON1 with the Blynk app. Start by adding the code from the Blynk example to your code. I haven’t used NodeMCU, but I’d guess that the ESP8266 example is the right one.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

void setup()
{
  Blynk.begin(auth, "ssid", "pass");
}

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

Okay, once you’ve added those lines to your existing code, build your Blynk dashboard on the app. It looks like your dashboard will probably consist of a single button. Assign a virtual pin to the button. Let’s go with V0. Now to access that button in your code, you’ll need a couple things. First, let’s create a new button variable, and call it BUTTON2. In order to match BUTTON2’s value to V0, you’ll need to add a BLYNK_WRITE function. Try this:

BLYNK_WRITE(V0){
     if (param.asInt()){
          BUTTON2 = HIGH:
     }
     else BUTTON2 = LOW;
}

Finally, anywhere in your code where you’re reading BUTTON1, add an “or” to also look for BUTTON2. That first if statement should now read “if ( digitalRead(BUTTON1)==HIGH || BUTTON2 == HIGH)”

That should do it.

#define BLYNK_DEBUG
#define BLYNK_PRINT Serial 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define BUTTON1 4
#define led 13
int btnState = 0;
int ledState  = 255;
int state = LOW;
int P1 = 0;
int P2 = 255;
int butPress;
char auth[] = "1234567890";
SimpleTimer timer;
void checkPhysicalButton();
void setup() {
 pinMode(BUTTON1,INPUT);
 pinMode(led,OUTPUT);
timer.setInterval(10L, checkPhysicalButton);
Serial.begin(9600);
  Blynk.begin(auth, "1234567890", "1234567890", "192.168.1.100");
}
BLYNK_CONNECTED()
 {
  Blynk.syncAll();
  }
BLYNK_WRITE(V10)
{
  P2 = param.asInt();
  analogWrite(led ,P2);
}

void checkPhysicalButton()
{
  if (digitalRead(BUTTON1)==HIGH) {//счетчик кнопки 
   btnState++;
   } 
 else {
   btnState=0;
 }
  delay(10);

if (btnState >= 100 && digitalRead(BUTTON1)==HIGH) { // длинное нажатие
   if (P1 == 1 && P2 >10 &&  state == LOW){// увеличиваем яркость
     P2-=15;
     analogWrite(led ,P2);
     Blynk.virtualWrite(V10,P2);
     delay(5);
    }
   
   if (P1 == 1 && P2 < 254 &&  state == HIGH){//уменьшаем яркость
    P2+=15;
    analogWrite(led ,P2);
    Blynk.virtualWrite(V10,P2);
     delay(5);
   
    }
 }
  if (btnState >= 100 && digitalRead(BUTTON1) == LOW) {  
   state = !state;
  
 } 
 
 if (btnState > 0 && btnState < 100 && digitalRead(BUTTON1) == LOW) { //короткое нажатие включаем или выключаем лампу
   if (P1 == 0){
   ledState = P2;
   P1 = 1;
   analogWrite(led ,ledState);
    Blynk.virtualWrite(V10,ledState);
     delay(5);
   }
  else {
   ledState = 0;
   P1 = 0;
   analogWrite(led ,ledState);
   Blynk.virtualWrite(V10,ledState);
   }
}
 if (P1 != 0) 
 {ledState = 254-P2;}
 else {ledState = 255;}
 }
 void loop()
 {
   Blynk.run();
  timer.run();
 }