Hello,
I’m struggling to get a slider to work with my project and can’t quite find the answer. Although I did find this amazing thread, to which I copied and pasted the code into my sketch, so thank you for that. My code skills are far from great but I’m trying to learn but I’m truly stumped with this one.
My project is to use a Wemos D1 Mini with some Neopixels (8leds), which will be inserted into a star projector for my son, to replace the current system.
So far, I’ve gotten the zeRGBa to work correctly and an analogue button to shift through some colours, to which I plan to do a virtual pin to work alongside the analogue button. My next step was to get a slider to adjust the brightness of the neopixels but it doesn’t work. I’ve tried to use various pins but nothing. If I use pin 2, for the built in led, then the built in leds brightness is adjusted.
My pin controlling the neopixels is called ‘PIN’ using pin 2. I then used #define LEDslide 2 for the slider. Also removed ‘LEDslide’ and just used ‘PIN’ but with the same result.
Any ideas to where I should go? I’m not sure what to adjust, as the slider works but just not where I need it to. The buttons for the brightness have not actually been implemented yet, as I’d like the slider to work first.
I’m using the Blynk app on an iPhone 11.
Thank you
#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
#define PIN 2 // was 2 but it's internal led
#define NUMPIXELS 8
#define BLYNK_PRINT Serial
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
SimpleTimer timer;
#define UPButton 14 // Set increment button pin...............not used yet.....neeeds pullup
#define DNButton 12 // Set decrement button pin...............not used yet.....neeeds pullup
#define LEDslide 2 // Slide for brightness....if change to button should be 0...........was 2 also
int brightness = 0; // Start on with LED OFF.
SimpleTimer timerBUTTONS; // Setup timerBUTTONS to check buttons.
void setup()
{
Serial.begin(115200);
Blynk.begin("Blynk", "Internetdeets", "pword");
timerBUTTONS.setInterval(1000L, whiteLED); // Timer to run whiteLED function every second.
pixels.begin();
}
BLYNK_WRITE(V2)
{
int R = param[0].asInt();
int G = param[1].asInt();
int B = param[2].asInt();
Serial.println(R);
Serial.println(G);
Serial.println(B);
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(R,G,B));
pixels.show();
}
}
BLYNK_WRITE(V0)
{
brightness = param.asInt(); // Get slider value.
analogWrite(LEDslide, brightness); // Send slider value to LED or mosfet
Serial.print("Slider: ");
Serial.println(brightness);
}
/////////////////////////////////////////////////
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 < 1023) { // Only do something if button pressed and brightness counter in range.
brightness += 100; // Increment counter by 100
Blynk.virtualWrite(V0, constrain(brightness, 0, 1023)); // Send feedback to Slider Widget - but keep within 0-1023
analogWrite(LEDslide, constrain(brightness, 0, 1023)); // Send data to LED - but keep within 0-1023
Serial.print("Button: ");
Serial.println(brightness);
}
// 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 -= 100; // Decrement counter by 100
Blynk.virtualWrite(V0, constrain(brightness, 0, 1023)); // Send feedback to Slider Widget - but keep within 0-1023
analogWrite(LEDslide, constrain(brightness, 0, 1023)); // Send data to LED - but keep within 0-1023
Serial.print("Button: ");
Serial.println(brightness);
}
else { // nothing happening here... move along :)
}
Serial.print("Button: ");
Serial.println(brightness);
}
void loop()
{
Blynk.run(); // Runs usual Blynk routine.
timerBUTTONS.run(); // Checks timer and runs whiteLED function.
}