0

Brightness setting - NeoPixels on Trinket
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Brightness setting - NeoPixels on Trinket

by mr_bridger on Tue Feb 12, 2019 5:57 am

Hi, wonder if someone can help me.

i have a load of neopixels connected to a Trinket Pro 5v. my sketch all works fine, but i want to be able to switch the brightness at powerup. so if i hold a push to make button when it starts, the sketch runs at full brightness (255), but if its not pressed, it runs at 50% (127) brightness for everything in the sketch, until it is power cycled.

I cant seem to work out how to do it / get it to work, i have used the arduino button tutorial wiring as a start (5v to button on pin 2, resistor to GND from https://www.arduino.cc/en/tutorial/button
I'm assuming this can be done once before the main loop to set global brightness until power cycle?

Heres the code i have up to the Void loop. Any pointers / help appreciated


Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>
#ifdef __AVR_ATtiny85__
  #include <avr/power.h>
#endif

#define PIN 6
#define NUM_LEDS 191
const int brightsense = 2; //Brightness push to make on pin 2


// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino 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)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_RGB + NEO_KHZ400);


void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
   if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
 #endif
  // End of trinket special code


switchpos = digitalRead(brightsense);

  strip.begin();
 
  if (switchpos == HIGH) {
  strip.setBrightness(255);
 } else {
  strip.setBrightness(127);
 }
 
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

mr_bridger
 
Posts: 10
Joined: Thu Mar 03, 2016 5:57 pm

Re: Brightness setting - NeoPixels on Trinket

by adafruit_support_carter on Tue Feb 12, 2019 4:22 pm

You need to call pinMode() to set your pin to input.

adafruit_support_carter
 
Posts: 9840
Joined: Tue Nov 29, 2016 2:45 pm

Re: Brightness setting - NeoPixels on Trinket

by mr_bridger on Wed Feb 13, 2019 12:35 pm

adafruit_support_carter wrote:You need to call pinMode() to set your pin to input.


Thanks for the reply, yes, i see i totally missed that bit!. thanks for the help all working as intended now. many thanks!.
I also noted i had Pin 2 on the code but had the switch plugged in to pin 4 as there is no pin 2 on the trinket pro! Facepalm!
Thanks!
Ross.

mr_bridger
 
Posts: 10
Joined: Thu Mar 03, 2016 5:57 pm

Please be positive and constructive with your questions and comments.