LED brightness ISSUE


#1

hi…:heart_eyes:
i’m trying to control a led brightness with the slider in thinger dashboard
i’m writing the code as shown below , what’s wrong ??:sweat:
why the slider go back to zero !!
i’m using NodeMCU … v1.0

#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>

#define USERNAME "******************"
#define DEVICE_ID "***************"
#define DEVICE_CREDENTIAL "****************"

#define LED D0

#define SSID "*******************"
#define SSID_PASSWORD "****************"

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() 
{
  thing.add_wifi(SSID, SSID_PASSWORD);

  pinMode (LED,OUTPUT);

  thing["LED"] << [](pson& in){
    if(in.is_empty()){
      in = LED;
    }
   else{
     LED= in;
    }
  };
}
void loop() {
  thing.handle();
}

THE ERROR IS :
assignment of read-only variable ‘D0’ :cry:


#2

Hi @Hussam_Farhat, I think that your code tries to change a constant value that is unacceptable ( #define LED D0). Futhermore, in order to control the brightness of an LED you have to use PWM (Pulse Width Modulation). In your code you use D0 pin that, as I am aware, does not support PWM.

So, your code should look like the example below (I’ve used D1 pin):

#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>

#define USERNAME "******************"
#define DEVICE_ID "***************"
#define DEVICE_CREDENTIAL "****************"

#define LED D1;
byte brightness = 0;

#define SSID "*******************"
#define SSID_PASSWORD "****************"

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() 
{
  thing.add_wifi(SSID, SSID_PASSWORD);

  pinMode (LED,OUTPUT);

  thing["LED"] << [](pson& in){
    if(in.is_empty()){
      in = brightness;
    }
   else {
     brightness = in;
    }
  };
}
void loop() {
  thing.handle();
  analogWrite(LED, brightness);
}

Set the slider widget from 0 to 255.


#3

Or:

#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>

#define USERNAME "******************"
#define DEVICE_ID "***************"
#define DEVICE_CREDENTIAL "****************"

#define SSID "*******************"
#define SSID_PASSWORD "****************"

#define LED D1

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() 
{
  thing.add_wifi(SSID, SSID_PASSWORD);

  pinMode (LED,OUTPUT);

  thing["LED"] << analogPin(LED);
}

void loop() {
  thing.handle();
}

#4

OMG !!!:heart_eyes:
that’s amazing !!!
it’s working as well

thaaaaaaaaaaaaaaaaaaaank you so much
for both of you guys
:slight_smile:


#5

what did work? Please write the solution in order to get it well documented for buddies with this issue in the future :wink:


#6

It worked as long as my slider is set 0 to 1023. If my slider has a value of up to 255 it doesn’t work. Another question, why my slider is at maximum value, my LED turn off and when it is at minimum it turns on?


#7

It should be because, for example, the nodemcu esp8266 has inverted logic for the builtin led (when its pin is LOW it turns ON, when its HIGH turns OFF).