how to add brightness control to buttoncycler example (ADAFRUIT NEOPIXELS)

hello,

i´ve been messing around with the Adafruit Neopixel Ring (x12), and i wanted to add a brightness control with a potentiometer. I´ve been using the “buttoncycler” example, and i like it, i just would love to control the brightness of each setup. I´ve tried to add the variables and statements. i guess that the analogread of the potentiometer must run in a loop, and I don´t know how to get both loops ( the one for the button and the potentiometer) tu run ok. Could somebody help me please?? I´m quite new at this, so I will appreciate any help sooo much. Thanks again! PS, here is the code for the “buttoncycler” example :

// This is a demonstration on how to use an input device to trigger changes on your neo pixels.
// You should wire a momentary push button to connect from ground to a digital IO pin.  When you
// press the button it will change to a new pixel animation.  Note that you need to press the
// button once to start the first animation!

#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN   2    // Digital IO pin connected to the button.  This will be
                         // driven with a pull-up resistor so the switch should
                         // pull the pin to ground momentarily.  On a high -> low
                         // transition the button press logic will execute.

#define PIXEL_PIN    6    // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT 16

// Parameter 1 = number of pixels in strip,  neopixel stick has 8
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_RGB     Pixels are wired for RGB bitstream
//   NEO_GRB     Pixels are wired for GRB bitstream, correct for neopixel stick
//   NEO_KHZ400  400 KHz bitstream (e.g. FLORA pixels)
//   NEO_KHZ800  800 KHz bitstream (e.g. High Density LED strip), correct for neopixel stick
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;

void setup() {
 pinMode(BUTTON_PIN, INPUT_PULLUP);
 strip.begin();
 strip.show(); // Initialize all pixels to 'off'
}

void loop() {
 // Get current button state.
 bool newState = digitalRead(BUTTON_PIN);

 // Check if state changed from high to low (button press).
 if (newState == LOW && oldState == HIGH) {
   // Short delay to debounce button.
   delay(20);
   // Check if button is still low after debounce.
   newState = digitalRead(BUTTON_PIN);
   if (newState == LOW) {
     showType++;
     if (showType > 9)
       showType=0;
     startShow(showType);
   }
 }

 // Set the last button state to the old state.
 oldState = newState;
}

void startShow(int i) {
 switch(i){
   case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
           break;
   case 1: colorWipe(strip.Color(255, 0, 0), 50);  // Red
           break;
   case 2: colorWipe(strip.Color(0, 255, 0), 50);  // Green
           break;
   case 3: colorWipe(strip.Color(0, 0, 255), 50);  // Blue
           break;
   case 4: theaterChase(strip.Color(127, 127, 127), 50); // White
           break;
   case 5: theaterChase(strip.Color(127,   0,   0), 50); // Red
           break;
   case 6: theaterChase(strip.Color(  0,   0, 127), 50); // Blue
           break;
   case 7: rainbow(20);
           break;
   case 8: rainbowCycle(20);
           break;
   case 9: theaterChaseRainbow(50);
           break;
 }
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
 for(uint16_t i=0; i<strip.numPixels(); i++) {
   strip.setPixelColor(i, c);
   strip.show();
   delay(wait);
 }
}

void rainbow(uint8_t wait) {
 uint16_t i, j;

 for(j=0; j<256; j++) {
   for(i=0; i<strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel((i+j) & 255));
   }
   strip.show();
   delay(wait);
 }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
 uint16_t i, j;

 for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
   for(i=0; i< strip.numPixels(); i++) {
     strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
   }
   strip.show();
   delay(wait);
 }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
 for (int j=0; j<10; j++) {  //do 10 cycles of chasing
   for (int q=0; q < 3; q++) {
     for (int i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, c);    //turn every third pixel on
     }
     strip.show();

     delay(wait);

     for (int i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, 0);        //turn every third pixel off
     }
   }
 }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
 for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
   for (int q=0; q < 3; q++) {
     for (int i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
     }
     strip.show();

     delay(wait);

     for (int i=0; i < strip.numPixels(); i=i+3) {
       strip.setPixelColor(i+q, 0);        //turn every third pixel off
     }
   }
 }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
 WheelPos = 255 - WheelPos;
 if(WheelPos < 85) {
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
 }
 if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
 }
 WheelPos -= 170;
 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

buttoncycler.ino (4.92 KB)

Right, first things first.

Go and read the instructions, then go back and modify your post (use the "More --> Modify" option to the bottom right of the post) to mark up the code as such so we can review it conveniently and accurately.

Note: Also mark up any data in the same way. This includes error output that you get from the IDE.

Until you reply and demonstrate that you understand these points that have been made so far, there is absolutely no point working through the other problems with the code, so I will leave it at that for the present.

thanks paul, i´m new at this, and I appreciatte any advice you can give. my post sucked! so i hope it´s better now! thx!

OK, so this is a pretty basic example, really the same as the one I was using at the New Year festivities but with the button control. (OK, one guy did pick it as an Iron Man crib, but said I was not wearing it properly in the middle! :astonished: )

I have modified my module to include the button, albeit on pin 3 as this is the necessary spacing from Reset on the Nano to mount the button directly to these pins. I am also using pin 8 as the PIXEL_PIN as this corresponds to my original version of the code.

Let's see what I can do. Working on it. ...

any progression on this Paul_B ??

Errr, sorry, no, it is presently drooping beside the desk here, still plugged into the USB with the pilot light on! :grinning:

I'll see if I can devote some time to it since someone else is showing interest ...

You are interested in brightness control, right?

thanks paul! i have found some other piece of code that allows me to change the brightnes with a potentiometer and tu change the colour whit another potentiokmetet. It flickers a bit, but it would be much more awsome to change the colour with a button, and add different colour patterns and effects. here´s the code, i hope it helps!

#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_GFX.h>
//#include <Adafruit_SSD1306.h>

#define PIN 6
//#define OLED_DC 11
//#define OLED_CS 12
//#define OLED_CLK 10
//#define OLED_MOSI 9
//#define OLED_RESET 13
//Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

//#define XPOS 0
//#define YPOS 1
//#define DELTAY 2

//#define LOGO16_GLCD_HEIGHT 16 
//#define LOGO16_GLCD_WIDTH  16 

//#if (SSD1306_LCDHEIGHT != 64)
//#error("Height incorrect, please fix Adafruit_SSD1306.h!");
//#endif



// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);


uint8_t brightness = 32;    // Global brightness, 0-255

int16_t hue = 680;          // initial hue 0-1535 (96*16)
uint8_t  width = 16;        // initial pulse width
float   tail = 1.7;         // initial length of tail
float   chroma = 0;         // initial chroma value for tail
int8_t  rate = 2;           // initial spin rate
uint8_t lastPeak = 0;       // storage for ring position

uint32_t iColor[16];        // Background colors

uint8_t colorCorrect(uint8_t value);
uint8_t colorCorrect(uint8_t value, uint8_t br);

int widthPin  = A0;         // Controls width of pulse
int tailPin   = A1;         // Controls length of fade
int huePin    = A2;         // Controls hue of gauge
int chromaPin = A3;         // Controls color of fade
//int ratePin   = A4;         // Controls rate/direction of spin
//int levelPin  = A5;         // Controls brightness level

  
void setup() {
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  /*
  hue = getHue();
  width = getWidth();
  tail = getTail();
  rate = getRate();
  */
  //testdrawchar();
  //delay(100000);
  brightness = getLevel();
  colorSwirlIn(hue, width, 4, rate);
  width = getWidth();
  tail  = getTail();
  hue = getHue();
  chroma = getChroma();
  brightness = getLevel();
  rate = getRate();
  showData();
  
  
}

//void testdrawchar(void) {
//  display.clearDisplay();
//  display.setTextSize(1);
//  display.setTextColor(WHITE);
//  display.setCursor(0,0);
//  
//  for (uint8_t i=0; i < 255; i++) {
//    if (i == '\n') continue;
//    display.write(i);
//    if ((i > 0) && (i % 16 == 0))
//      display.println();
//    if ((i > 0) && (i % 128 == 0)) {
//      delay(10000);
//      display.clearDisplay();
//      display.setCursor(0,0);
//    }
//        
//  }    
//  display.display();
//}

//void showData(void) {
//  display.clearDisplay();
//  display.setTextSize(1);
//  display.setTextColor(WHITE);
//  display.setCursor(0,0); 
//  display.print("Width:");
//  display.print(width);
//  display.setCursor(64,0);
//  display.print("Decay:");
//  display.print(tail);
//  display.setCursor(0,8); 
//  display.print("Rate:");
//  display.print(rate);
//  display.setCursor(64,8);
//  display.print("Level:");
//  display.print(brightness);  
//  
//  display.setCursor(0,16);  
//  display.print("Hue:");
//  display.print(hue);
//  display.setCursor(0,24);
//  display.print("Chroma:");
//  display.println(chroma);    
//  display.display();
//}  


uint8_t getWidth() {
  return byte(analogRead(widthPin) / 4);
} 

float getTail() {
  return analogRead(tailPin) / 256.0;
} 

uint16_t getHue() {
  return 1.5 * analogRead(huePin);
} 

float getChroma() {
  return (analogRead(chromaPin) / 16.0) - 32.0;
} 

int8_t getRate() {
  return byte((1024 - analogRead(ratePin)) / 4.0) - 128;
} 

uint8_t getLevel() {
  return uint8_t(analogRead(levelPin) / 4.0);
} 


int32_t hue2color(int16_t hue) {
  int16_t a = hue;
  uint8_t r,g,b;
  switch((hue >> 8) % 6) {
    case 0: r = 255; g =   a; b =   0; break;
    case 1: r =  ~a; g = 255; b =   0; break;
    case 2: r =   0; g = 255; b =   a; break;
    case 3: r =   0; g =  ~a; b = 255; break;
    case 4: r =   a; g =   0; b = 255; break;
    case 5: r = 255; g =   0; b =  ~a; break;
  } 
  return strip.Color(r,g,b);
}
  

void colorSwirlIn(int16_t initHue, uint8_t wide, float decay, int8_t spinRate) {
    uint8_t initBrightness = brightness;
    int16_t a = initHue,distance;
    uint8_t r,g,b,level,peak;
    uint32_t c;
    float chromic = 0.0;
    uint8_t abortCount = 0;
 
    switch((initHue >> 8) % 6) {
     case 0: r = 255; g =   a; b =   0; break;
     case 1: r =  ~a; g = 255; b =   0; break;
     case 2: r =   0; g = 255; b =   a; break;
     case 3: r =   0; g =  ~a; b = 255; break;
     case 4: r =   a; g =   0; b = 255; break;
     case 5: r = 255; g =   0; b =  ~a; break;
    }
  peak = 0;
  
  for(peak=lastPeak; peak<256; peak+=spinRate) {
    if (abortCount >= 4) {
      lastPeak = peak;
      return;
    }
    spinRate = getRate();
    wide = getWidth();
    wide = max((wide / 2) - 8, 0);

    chromic = getChroma();
    decay = getTail();
    
    for(uint16_t i=0; i<strip.numPixels(); i++) {
      distance = peak - (i * 256/strip.numPixels());
      if (distance <= -128) {
        distance += 256;
      }
      if (distance >= 128) {
        distance -= 256;
      }
        distance = byte(abs(distance));
        distance = max((distance - wide), 0);
      if (distance != 0)
      {
        level = min((65536/pow(distance, decay)),255);
      } 
      else
      {
        level = 255;
      }  
      initHue = getHue();
      brightness = getLevel();
      c = colorCorrect2(hue2color(uint16_t(initHue + chromic*(distance))),level);
      iColor[i]=c;
      strip.setPixelColor(i, c);
      strip.show();
    }
    abortCount++;
  } 
  brightness = initBrightness;
}


void colorFadeIn(int16_t initHue, uint8_t wait) {
    uint8_t initBrightness = brightness;
    int16_t a = initHue;
    uint8_t r,g,b,level;
    uint32_t c; 
    switch((initHue >> 8) % 6) {
     case 0: r = 255; g =   a; b =   0; break;
     case 1: r =  ~a; g = 255; b =   0; break;
     case 2: r =   0; g = 255; b =   a; break;
     case 3: r =   0; g =  ~a; b = 255; break;
     case 4: r =   a; g =   0; b = 255; break;
     case 5: r = 255; g =   0; b =  ~a; break;
    }
  brightness = 255;
  for(level=2; level<250; level+=(1+level/32)) {
    //level = 0;
    c = strip.Color(colorCorrect(r,level), colorCorrect(g,level), colorCorrect(b,level));
    for(uint16_t i=0; i<strip.numPixels(); i++) {
      iColor[i]=c;
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
    }
  } 
  brightness = initBrightness;
}

Where are you defining colorCorrect & colorCorrect2?

I ran into issues with the buttoncycler sketch. GrumpyMike shared his version that uses less "for loops" allowing button presses to be recognized as it was pressed. I assume the same principle would work for adding a potentiometer and having it set the value in strip.setBrightness(0-255 value) after doing an analogRead and map function.