Neopixel and slider implementation

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.
}

I guess that when you say “analogue buttons” you actually mean physical buttons, connected to digital pins?

I think the problem is that you are trying to mix two different approaches to controlling the LEDs. The approach in the link you provided is fine if you’re not using the Neopixel library.

If you want to control brightness using Neopixels it’s done by changing the RGB values. An RGB of 255,0,0 will give red at the maximum brightness. 127,0,0 will give red at half brightness, 63,0,0 will give red at quarter brightness etc.

I’d suggest that you need your brightness slider to be ranged from 0-255 and use this to calculate the adjusted RGB values to give the correct brightness.

(RedValue * slidervalue)/255 would probably give the desired result.

Pete.

Hi Pete
Yes, you’re correct. The analogue is a physical button.

Ah that’d make sense then. Thank you for the snippet of code. Sorry to be asked to be spoon fed here but do I need to remove all the code from the link which I provided?

I’ve not studied either piece of code in great detail, but I’d guess that the answer is yes.
Also, what you’ve done in your combined code, of referencing the same pin in two different ways is rather clumsy and ought to be removed anyway…

Pete.

Hi Pete. You’re correct, which is why I mentioned that in the op, although my description was probably not the best.
I explained I tried to use both #define statements separately by removing one at a time and changing the PIN to LEDslide and vice versa but obviously that makes no difference. I want multiple things to control the same pin you see. I’ll go back to the drawing board and look at the Adafruit lib and see if I can adjust the brightness that way, thanks.