Need help with dimmer project arduino uno , wemos d1 mini

Bsically what i’m trying to do is dim a led with 2 phisical buttons but also be able to use a slider

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

char auth[] = "xxx"; //UNO
byte mac[] = {0x11, 0xFE, 0xFE, 0xEF, 0xEE, 0xF1};
byte ip[] = { 192, 168, 1, 150};


SimpleTimer timer;
const int button1 = 7;
const int button2 = 8;
const int led = 9;
int brightness = 100;




void setup()
{ Ethernet.begin(mac, ip);

  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth);

    pinMode(9,OUTPUT);        // Set pin 13 to be in output mode
  pinMode(7,INPUT_PULLUP);   // Set pin 7 to be in input mode
  pinMode(8,INPUT_PULLUP);  

 
  if(digitalRead(7) == LOW) brightness--;   // If button on pin 7 is pressed, reduce brightness
  if(brightness < 0) brightness = 0;        // Don't let brightness drop below zero
  if(digitalRead(8) == LOW) brightness++;   // If button on pin 8 is pressed, increase brightness
  if(brightness > 255) brightness = 255;    // Don't let brightness get above 255
  analogWrite(9, brightness);  
}
  void loop()
{
  Blynk.run(); // Run Blynk
  timer.run(); // Run SimpleTimer
}

try this sketch. add slider to v0 on blynk app. set range 0 to 255 and here we go.

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

char auth[] = "xxx"; //UNO
byte mac[] = {0x11, 0xFE, 0xFE, 0xEF, 0xEE, 0xF1};
byte ip[] = { 192, 168, 1, 150};


SimpleTimer timer;
const int button1 = 2;
const int button2 = 3;
const int led = 9;
int brightness = 100;


BLYNK_WRITE(V0)
{   
  brightness=param.asInt();
  analogWrite(9, brightness); 
}

void brightnessUp()
{
  brightness+=20;
  analogWrite(9, brightness);
}

void brightnessDown()
{
  brightness-=20;
  analogWrite(9, brightness);
}
void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth);

  //attach interrupt on pin 2
  attachInterrupt(0, brightnessUp, CHANGE);
  // attach intrrupt on pin 3
  attachInterrupt(1, brightnessDown, CHANGE);
 
  pinMode(9,OUTPUT);        // Set pin 13 to be in output mode
  pinMode(7,INPUT_PULLUP);   // Set pin 7 to be in input mode
  pinMode(8,INPUT_PULLUP);  

 
  if(digitalRead(7) == LOW) brightness--;   // If button on pin 7 is pressed, reduce brightness
  if(brightness < 0) brightness = 0;        // Don't let brightness drop below zero
  if(digitalRead(8) == LOW) brightness++;   // If button on pin 8 is pressed, increase brightness
  if(brightness > 255) brightness = 255;    // Don't let brightness get above 255
  analogWrite(9, brightness);  
}
  void loop()
{
  Blynk.run(); // Run Blynk
  timer.run(); // Run SimpleTimer
}

you need to move this to a function on its own…

so slider is working but buttons doesn’t do anything

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

char auth[] = "xxx"; //UNO
byte mac[] = {xxx};
byte ip[] = { 192, 168, 1, 159};


SimpleTimer timer;
const int button1 = 7;
const int button2 = 8;
const int led = 9;
int brightness = 100;


BLYNK_WRITE(V0)
{   
  brightness=param.asInt();
  analogWrite(9, brightness); 
}

void brightnessUp()
{
  brightness+=20;
  analogWrite(9, brightness);
}

void brightnessDown()
{
  brightness-=20;
  analogWrite(9, brightness);
}
void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth);

  //attach interrupt on pin 7
  attachInterrupt(0, brightnessUp, CHANGE);
  // attach intrrupt on pin 8
  attachInterrupt(1, brightnessDown, CHANGE);
 
  pinMode(9,OUTPUT);        // Set pin 9 to be in output mode
  pinMode(7,INPUT_PULLUP);   // Set pin 7 to be in input mode
  pinMode(8,INPUT_PULLUP);  

 
  if(digitalRead(7) == LOW) brightness--;   // If button on pin 7 is pressed, reduce brightness
  if(brightness < 0) brightness = 0;        // Don't let brightness drop below zero
  if(digitalRead(8) == LOW) brightness++;   // If button on pin 8 is pressed, increase brightness
  if(brightness > 255) brightness = 255;    // Don't let brightness get above 255
  analogWrite(9, brightness);  
}
  void loop()
{
  Blynk.run(); // Run Blynk
  timer.run(); // Run SimpleTimer
}

can you give me some example?

buttons must be on pin2 and pin3 on arduino. this example using interrupts. use my full example. read comments.

made it work, all i need now is sync it with slider (is it even possible?)
any help greatly appreciated
heres my new sketch !

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

char auth[] = "xxx"; //UNO
byte mac[] = {0x18, xxx};
byte ip[] = { 192, 168, 1, 159};

SimpleTimer timer;

int brightness = 250;

BLYNK_WRITE(V0)
{
  brightness = param.asInt();
  analogWrite(9, brightness);
}

void brightness1()
{
  if (digitalRead(7) == LOW) brightness--;  // If button on pin 7 is pressed, reduce brightness
  if (brightness < 0) brightness = 0;       // Don't let brightness drop below zero
  if (digitalRead(8) == LOW) brightness++;  // If button on pin 8 is pressed, increase brightness
  if (brightness > 255) brightness = 255;   // Don't let brightness get above 255
  analogWrite(9, brightness);
}
void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth);

  pinMode(9, OUTPUT);       // Set pin 9 to be in output mode
  pinMode(7, INPUT_PULLUP);  // Set pin 7 to be in input mode
  pinMode(8, INPUT_PULLUP);
  timer.setInterval(20L, brightness1);

}
void loop()
{
  Blynk.run(); // Run Blynk
  timer.run(); // Run SimpleTimer
}

maybe?

void brightness1()
{
  if (digitalRead(7) == LOW) brightness--;  // If button on pin 7 is pressed, reduce brightness
  if (brightness < 0) brightness = 0;       // Don't let brightness drop below zero
  if (digitalRead(8) == LOW) brightness++;  // If button on pin 8 is pressed, increase brightness
  if (brightness > 255) brightness = 255;   // Don't let brightness get above 255
  analogWrite(9, brightness);
  Blynk.virtualWrite(V0, brightness);
}

but that might cause Blynk flood as you are doing it every 20 milliseconds… but play around and it should work?

it’s working but 50millis witch it takes forever to go from 0 to 255
any suggestions?

yeah a suggestion could be - you need to have more logical dimmer increments , you should lookup the map() function:

https://www.arduino.cc/en/Reference/Map

then in the slider settings, match your map() settings…

OR try adjusting the slider settings without the map() function, but i dont know enough bout arduino to know if that will work…

i’ve made some changes but when i hold dim button it goes below 0 to -10 and led is lit untill i release the button

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

char auth[] = "xxx"; //UNO
byte mac[] = {0x18,xx};
byte ip[] = { 192, 168, 1, 159};


SimpleTimer timer;


int brightness = 250;

BLYNK_WRITE(V0)
{
  brightness = param.asInt();
  analogWrite(9, brightness);
}

void brightness1()
{
  if (digitalRead(7) == LOW) brightness+=10.2;  // If button on pin 7 is pressed, reduce brightness
  if (brightness < 0) brightness = 0;       // Don't let brightness drop below zero
  if (digitalRead(8) == LOW) brightness-=10.2;  // If button on pin 8 is pressed, increase brightness
  if (brightness > 255) brightness = 255;   // Don't let brightness get above 255
  analogWrite(9, brightness);
  Blynk.virtualWrite(V0, brightness);
}
void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth);

  pinMode(9, OUTPUT);       // Set pin 9 to be in output mode
  pinMode(7, INPUT_PULLUP);  // Set pin 7 to be in input mode
  pinMode(8, INPUT_PULLUP);
  timer.setInterval(80L, brightness1);

}
void loop()
{
  Blynk.run(); // Run Blynk
  timer.run(); // Run SimpleTimer
}

Did you examine map(); function?

i did but i’ve noticed another problem ,when i use slider and try it make it brighter (slide it to the right side) slider keeps pulling it back to the left side i think theres a bug or something

made minor changes but slider doesnt work the way it should so i had to delete Blynk.virtualWrite(V0, brightness); to make it work .hope somebody have a fix for that .
dave thanx for your help happy holidays!

if (digitalRead(7) == LOW) brightness -= 10; // If button on pin 7 is pressed, reduce brightness
  if (brightness < 0) brightness = 0;       // Don't let brightness drop below zero
  if (digitalRead(8) == LOW) brightness += 10; // If button on pin 8 is pressed, increase brightness
  if (brightness > 255) brightness = 255;   // Don't let brightness get above 255
  analogWrite(9, brightness);

@jack

I don’t think there are any bugs with the slider, rather it was probably just reacting to both your “physical” adjustments and feedback from the sketch as the same time… and your timing, or lack thereof may have caused an overload?? Can’t say for sure as I haven’t taken your code and ran it.

Also, you could use constraint() to keep your increments and decrements in-line with acceptable ranges (see my code for example).

Regardless… As I hadn’t even realized how the sketch could provide feedback to a slider widget :blush:, until I perused your post, It got me thinking of how I would try to do it; And here is the result below. Check it out and adapt to your needs as required.

It was a great learning exercise for myself :smile:

// Simultaneous dimmer control using Slider and Buttons

#include <BlynkSimpleEthernet.h>
IPAddress server_ip (x, x, x, x);  // IP of Local_Server
byte arduino_mac[] = {0x00, 0x00, 0x00, 0xab, 0xac, 0xad};
IPAddress arduino_ip(x, x, x, x);
IPAddress subnet_mask(255, 255, 255, 0);
IPAddress gateway_ip(x, x, x, x);
IPAddress dns_ip(x, x, x, x);

#include <SPI.h>
#include <SimpleTimer.h>
#define UPButton x  // Set increment button pin.
#define DNButton x // Set decrement button pin.
#define WLED x  // Set LED pin.
int brightness = 0;  // Start off with LED off.

char auth[] = "auth_code";  // App authorization code.
SimpleTimer timerBUTTONS;  // Setup timerBUTTONS to check buttons.

/////////////////////////////////////////////////

void setup()
{
  pinMode(UPButton, INPUT);  // Set increment button pin to INPUT - Add _PULLUP if not already set in hardware.
  pinMode(DNButton, INPUT);  // Set decrement button pin to INPUT - Add _PULLUP if not already set in hardware.

  Blynk.begin(auth, server_ip, 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);  //Start Blynk with required parameters.

  timerBUTTONS.setInterval(500L, whiteLED);  // Timer to run whiteLED function every half second.
}

////////////////////////////////////////////////

BLYNK_WRITE(V4) // Slider Widget Function - runs every-time slider widget is physically moved.
{
  brightness = param.asInt();  // Get slider value.
  analogWrite(WLED, brightness);  // Send slider value to LED
}

/////////////////////////////////////////////////

void whiteLED() // This function checks buttons status and updates White LED and slider (frequency dependent on timer).
{
// Check increment button and respond.  
if (digitalRead(UPButton) == HIGH and brightness < 255) {  // Only do something if button pressed and brightness counter in range.
    brightness += 50;  // Increment counter by 50
    Blynk.virtualWrite(V4, constrain(brightness, 0, 255));  // Send feedback to Slider Widget - but keep within 0-255
    analogWrite(WLED, constrain(brightness, 0, 255));  // Send data to LED - but keep within 0-255
  }
// Check decrement button and respond..
  else if (digitalRead(DNButton) == HIGH and brightness > 0) {  // Only do something if button pressed and brightness counter in range.
    brightness -= 50;  // Decrement counter by 50
    Blynk.virtualWrite(V4, constrain(brightness, 0, 255));  // Send feedback to Slider Widget - but keep within 0-255
    analogWrite(WLED, constrain(brightness, 0, 255));  // Send data to LED - but keep within 0-255
  }
  else { // nothing happening here... move along :)
  }
}

/////////////////////////////////////////////////
/////////////////////////////////////////////////

void loop()
{
  Blynk.run();  // Runs usual Blynk routine.
  timerBUTTONS.run();  // Checks timer and runs whiteLED function.
}

Timer usage is not a good way for button operations. For example if i didnt press 10minutes, code still sending value to server every 50ms. is it good way? if you must use timer you need to check brightness is changed ? if its not changed you dont send data to server. for example

if (brightness!=oldBrightness)
{
Blynk.virtualWrite(V4,brightness);
}
1 Like

@alonewolfx2

Yes the timer is checking the buttons every 500millis (half second) in my above example… But by using IF() loops with nested responses, data is being sent to server only if a button is actually being pressed at time of timer check (also only if the value is NOT already at either extreme… 0 or 255. So holding the increment button down if LED is at full brightness does NOT send any further increment data to server or physical LED).

I actually have the above code example nested in with other test code that is checking many slider widgets and physical sensors (using a Diyode CodeShield) as well as the zeRGBa widget, lighting a physical RGB while feeding back data to a simulated RGB LED widget; and displaying the HEX data to a LCD widget (all of those other timers are running every second); No timing issues that I have noticed so far.

PS, I am not saying my way is best, far from it :wink:
Anyone, please improve the code and show examples… this is how we No0Bz learn :+1:

1 Like

made some changes and it work the way i needed to THANX !
heres my example for somebody who might need it
now all i have to do is make it work with wemos D1 mini

// Simultaneous dimmer control using Slider and Buttons
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
byte mac[] = {0x18, ,,,};
byte ip[] = { 192, 168, 1, 159};

#include <SPI.h>
#include <SimpleTimer.h>
#define UPButton 7  // Set increment button pin.
#define DNButton 8 // Set decrement button pin.
#define WLED 9  // Set LED pin.
int brightness = 255;  // Start on with LED on.

char auth[] = "xxx";  // App authorization code.
SimpleTimer timerBUTTONS;  // Setup timerBUTTONS to check buttons.

/////////////////////////////////////////////////

void setup()
{
  pinMode(UPButton, INPUT_PULLUP);  // Set increment button pin to INPUT - Add _PULLUP if not already set in hardware.
  pinMode(DNButton, INPUT_PULLUP);  // Set decrement button pin to INPUT - Add _PULLUP if not already set in hardware.

   Ethernet.begin(mac, ip);
Blynk.begin(auth);
  timerBUTTONS.setInterval(100L, whiteLED);  // Timer to run whiteLED function every half second.
}

////////////////////////////////////////////////

BLYNK_WRITE(V4) // Slider Widget Function - runs every-time slider widget is physically moved.
{
  brightness = param.asInt();  // Get slider value.
  analogWrite(WLED, brightness);  // Send slider value to LED
}

/////////////////////////////////////////////////

void whiteLED() // This function checks buttons status and updates White LED and slider (frequency dependent on timer).
{
// Check increment button and respond.  
if (digitalRead(UPButton) == LOW and brightness < 255) {  // Only do something if button pressed and brightness counter in range.
    brightness += 20;  // Increment counter by 20
    Blynk.virtualWrite(V4, constrain(brightness, 0, 255));  // Send feedback to Slider Widget - but keep within 0-255
    analogWrite(WLED, constrain(brightness, 0, 255));  // Send data to LED - but keep within 0-255
  }
// Check decrement button and respond..
  else if (digitalRead(DNButton) == LOW and brightness > 0) {  // Only do something if button pressed and brightness counter in range.
    brightness -= 20;  // Decrement counter by 20
    Blynk.virtualWrite(V4, constrain(brightness, 0, 255));  // Send feedback to Slider Widget - but keep within 0-255
    analogWrite(WLED, constrain(brightness, 0, 255));  // Send data to LED - but keep within 0-255
  }
  else { // nothing happening here... move along :)
  }
}

/////////////////////////////////////////////////
/////////////////////////////////////////////////

void loop()
{
  Blynk.run();  // Runs usual Blynk routine.
  timerBUTTONS.run();  // Checks timer and runs whiteLED function.
}

that’s right we need more examples because not everybody went to school to learn coding or doesn’t have enought time to learn it all , we learn by trial and error therefore we need people to share their code .
happy holidays!

1 Like

so i’m trying to make this sketch work with wemos d1 mini but value 255 only gives me 0.83v and that beeing said it’s not enough for my mosfet to fully open - when i change slider value in blynk app from 0-1023 it gives me to 3.3v and thats what i need , but than again there’s a problem with this sketch … any suggestions?