Simple Fade Question

I am a newbie and had support getting this up and running. My goal is to have a white strip of less that will dim as a turn a dial and finally go off. I have a ton of neopixels laying around so I used them. The issue is that this isn’t really a true fade. At about 5o% the strip turns blue in color and continues to fade in that tint as the dial is turned more.

Is there a way to do this with the set.brightness command? I think that might preserve the white look.

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN              8  
#define NUMPIXELS       43
#define KNOB            A1

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 10; // delay for half a second

void setup() {
  pixels.begin();
  pinMode(PIN, OUTPUT);
}

void loop() {
   int i;              // loop variable
   int value = analogRead(KNOB)>>2;
   int clr = pixels.Color(255-value, 255-value, 255-value);
   //clr = pixels.Color(value, value, value); // same value for red green and blue gives levels of white
   for(int i=0;i<NUMPIXELS;i++){
   pixels.setPixelColor(i, clr);
   pixels.show();
   delay(delayval); // Delay for a period of time (in milliseconds).
   }
}

You are writing to the strip even if there is no change on the dial and you are delaying between writes which should cause that the color change is like a running light. You code provides no obvious answers to why the strip turns blue, this may be an electrical issue.

int delayval = 10; // delay for half a second

Not the cause of your problem, but comments are intended to help understand and/or document what the program is doing, not confuse the reader

The whole program would be easier to read if Auto formatted in the IDE

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN              8
#define NUMPIXELS       43
#define KNOB            A1

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 10; // delay for half a second

void setup()
{
  pixels.begin();
  pinMode(PIN, OUTPUT);
}

void loop()
{
  int i;              // loop variable
  int value = analogRead(KNOB) >> 2;
  int clr = pixels.Color(255 - value, 255 - value, 255 - value);
  //clr = pixels.Color(value, value, value); // same value for red green and blue gives levels of white
  for (int i = 0; i < NUMPIXELS; i++)
  {
    pixels.setPixelColor(i, clr);
    pixels.show();
    delay(delayval); // Delay for a period of time (in milliseconds).
  }
}

I also note this :

 int i;              // loop variable
  for (int i = 0; i < NUMPIXELS; i++)

How many different variables named i do you need ?

Neither of these will stop the program running but do indicate a lack of attention to detail

@Danois90

Thank you for the reply. How do I write to the strip in when there is a change to the dial? I thought I need the refresh to update the strip.

Read the value. If it is the same as the last time you read it do nothing but save the value for the next time you check

If it has changed then take action as required

@UKHeliBob Thanks for the helpful reply. OK, I will try that.

As to your first post, I tried to explain I was a newbie and am really barely grasping the code.

The comments in my sketch where there when I copied and pasted them into this.

From your comment I assume that this code:
int delayval = 10; // delay for half a second is not set for a half a second?
Would half a second be .5?

As to the "how many "i" variables do you need?" comment. I have no idea. I just pieced together code from other sketches and frankly was amazed and proud of myself that I got close to what I wanted. However from your comments I felt the exact opposite. It's obvious you took some time to write that post, but it lead me no closer to a answer or understanding and only made me feel stupid.

Maybe the term newbie doesn't mean what I think.

Hi,

delay is measured in a number of milli-seconds, so half a second is a delay of 500…

Peter

As to the “how many “i” variables do you need?” comment.

Two variables named i are declared in the program, one at the start of the loop() function (thus available anywhere in the function) and one as the for loop variable (thus available anywhere in the code block for the for loop).

The names are the same but they are not the same variable as each has different scope (read up on variable scope). You could change one of them and the other would not change. That in itself need not be a problem but you can see how it could be confusing at best and maybe disastrous at worst.

nizer:
am really barely grasping the code.
...
I have no idea. I just pieced together code from other sketches and frankly was amazed and proud of myself that I got close to what I wanted. However from your comments I felt the exact opposite. It's obvious you took some time to write that post, but it lead me no closer to a answer or understanding and only made me feel stupid.

Maybe the term newbie doesn't mean what I think.

Have you read any documentation? All the Arduino functions like delay() are documented on this site.

Thanks for the help all. I am slowly learning. This helps a lot. I will get back to the tutorials.

With a bunch of reading and a friend who is a Arduino genius we solved the “blue tint” issue. We changed the

int clr = pixels.Color(255-value, 255-value, 255-value);

to

uint32_t clr = pixels.Color(255-value, 255-value, 255-value);

Now it fades joyously all in white.

Also added, the “do nothing” if nothing has changed to the loop.

Hopefully this will help the next person that has this question.

Here is the final code:

//Thanks to Jonathan Dean for the help
//This sketch allows a potentiometer (dial) to change the brighness of a strip of LEDs (RGB Neopixels).
//Social Media Circle Light Project by Mark Neisser 2020
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#endif
#define PIN              8  
#define NUMPIXELS       43
#define KNOB            A1

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 100; // delay
int lastvalue = 0;   

void setup() {
  pixels.begin();
}

void loop() {
   int i;
   int value = analogRead(KNOB)>>2;
 if (value != lastvalue) {
   uint32_t clr = pixels.Color(255-value, 255-value, 255-value);
   for(i=0;i<NUMPIXELS;i++)
   {
   pixels.setPixelColor(i, clr);
   }
   pixels.show();
   delay(delayval); // Delay for a period of time (in milliseconds).
   lastvalue=value;
   } else {
   }
}