Re: NEOPIXEL brightness control
Re: NEOPIXEL brightness control
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
#define LEDS 24
const int CYCLE_MILLISECONDS = 5000; // 5 second breathing cycle.
const uint8_t KEYFRAMES[] = {
// Rising
20, 21, 22, 24, 26, 28, 31, 34, 38, 41, 45, 50, 55, 60, 66, 73, 80, 87, 95,
103, 112, 121, 131, 141, 151, 161, 172, 182, 192, 202, 211, 220, 228, 236,
242, 247, 251, 254, 255,
// Falling
254, 251, 247, 242, 236, 228, 220, 211, 202, 192, 182, 172, 161, 151, 141,
131, 121, 112, 103, 95, 87, 80, 73, 66, 60, 55, 50, 45, 41, 38, 34, 31, 28,
26, 24, 22, 21, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
};
unsigned long lastBreath = 0;
int keyframePointer = 0;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDS, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show();
}
void loop() {
int numKeyframes = sizeof(KEYFRAMES) - 1;
float period = CYCLE_MILLISECONDS / numKeyframes;
unsigned long now = millis();
if ((now - lastBreath) > period) {
lastBreath = now;
for (int i = 0; i < strip.numPixels(); i++) {
uint8_t color = (127 * KEYFRAMES[keyframePointer]) / 256;
strip.setPixelColor(i, strip.Color(KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer]));
}
strip.show();
// Increment the keyframe pointer.
if (++keyframePointer > numKeyframes) {
// Reset to 0 after the last keyframe.
keyframePointer = 0;
}
}
}
Re: NEOPIXEL brightness control
I'd like to make it "breathe" in another color but I couldn't figure it out. Right now it just breathes in white. What should I change?
strip.setPixelColor(i, strip.Color(KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer]));
Re: NEOPIXEL brightness control
adafruit_support_bill wrote:I'd like to make it "breathe" in another color but I couldn't figure it out. Right now it just breathes in white. What should I change?
This line sets the R, G and B component of the color to all the same value, so it is white.
- Code: Select all | TOGGLE FULL SIZE
strip.setPixelColor(i, strip.Color(KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer], KEYFRAMES[keyframePointer]));
If you want a different color, first you need to define what the RGB components of that color are, then scale the values proportionally for the fade.
Re: NEOPIXEL brightness control
strip.setPixelColor(i, KEYFRAMES[keyframePointer], 0, 0);
Re: NEOPIXEL brightness control
adafruit_support_bill wrote:
- Code: Select all | TOGGLE FULL SIZE
strip.setPixelColor(i, KEYFRAMES[keyframePointer], 0, 0);
Re: NEOPIXEL brightness control
strip.setPixelColor(i, (Red * KEYFRAMES[keyframePointer]) / 255, (Green* KEYFRAMES[keyframePointer]) / 255, (Blue* KEYFRAMES[keyframePointer]) / 255);
Re: NEOPIXEL brightness control
adafruit_support_bill wrote:The general formula is:
- Code: Select all | TOGGLE FULL SIZE
strip.setPixelColor(i, (Red * KEYFRAMES[keyframePointer]) / 255, (Green* KEYFRAMES[keyframePointer]) / 255, (Blue* KEYFRAMES[keyframePointer]) / 255);
Re: NEOPIXEL brightness control
Re: NEOPIXEL brightness control
adafruit_support_bill wrote:One thing that would be fairly straightforward to do is to randomize the timing a bit. Changing CYCLE_MILLISECONDS will alter the rhythm of the breathing. The random function is documented here: https://www.arduino.cc/en/Reference/Random
Re: NEOPIXEL brightness control
adafruit_support_rick wrote:You guys might want to have a look at the breathing sketch I wrote a while ago.
BreatheRightStrip.ino:
- Code: Select all | TOGGLE FULL SIZE
#include "Adafruit_NeoPixel.h"
#include "SPI.h"
//Demonstrates "breathing" effect on NeoPixel Strip
#define LED_PIN 6
#define N_LEDS 16
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
uint8_t LED_Breathe_Table[] = { 80, 87, 95, 103, 112, 121, 131, 141, 151, 161, 172, 182, 192, 202, 211, 220,
228, 236, 242, 247, 251, 254, 255, 255, 254, 251, 247, 242, 236, 228, 220, 211,
202, 192, 182, 172, 161, 151, 141, 131, 121, 112, 103, 95, 87, 80, 73, 66,
60, 55, 50, 45, 41, 38, 34, 31, 28, 26, 24, 22, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 22, 24, 26, 28,
31, 34, 38, 41, 45, 50, 55, 60, 66, 73 };
#define BREATHE_TABLE_SIZE (sizeof(LED_Breathe_Table))
#define BREATHE_CYCLE 6000 /*breathe cycle in milliseconds*/
#define BREATHE_UPDATE (BREATHE_CYCLE / BREATHE_TABLE_SIZE)
void setup() {
// Start up the LED strip
strip.begin();
// Update the strip, to start they are all 'off'
strip.show();
}
void loop() {
int cycle;
for (cycle=0; cycle < 4; cycle++) {
uniformBreathe(LED_Breathe_Table, BREATHE_TABLE_SIZE, BREATHE_UPDATE, 256, 256, 256);
}
for (cycle=0; cycle < 4; cycle++) {
sequencedBreathe(LED_Breathe_Table, BREATHE_TABLE_SIZE, BREATHE_UPDATE, 256, 256, 256);
}
}
void uniformBreathe(uint8_t* breatheTable, uint8_t breatheTableSize, uint16_t updatePeriod, uint16_t r, uint16_t g, uint16_t b)
{
int i;
uint8_t breatheIndex = 0;
uint8_t breatheRed;
uint8_t breatheGrn;
uint8_t breatheBlu;
for (breatheIndex = 0; breatheIndex < breatheTableSize; breatheIndex++) {
for (i=0; i < strip.numPixels(); i++) {
breatheRed = (r * breatheTable[breatheIndex]) / 256;
breatheGrn = (g * breatheTable[breatheIndex]) / 256;
breatheBlu = (b * breatheTable[breatheIndex]) / 256;
strip.setPixelColor(i, breatheRed, breatheGrn, breatheBlu);
}
strip.show(); // write all the pixels out
delay(updatePeriod);
}
}
void sequencedBreathe(uint8_t* breatheTable, uint8_t breatheTableSize, uint16_t updatePeriod, uint16_t r, uint16_t g, uint16_t b)
{
int i;
uint8_t breatheIndex = 0;
uint8_t breatheRed;
uint8_t breatheGrn;
uint8_t breatheBlu;
uint8_t sequenceIndex;
for (breatheIndex = 0; breatheIndex < breatheTableSize; breatheIndex++) {
for (i=0; i < strip.numPixels(); i++) {
sequenceIndex = (breatheIndex + (i*4)) % breatheTableSize;
breatheRed = (r * breatheTable[sequenceIndex]) / 256;
breatheGrn = (g * breatheTable[sequenceIndex]) / 256;
breatheBlu = (b * breatheTable[sequenceIndex]) / 256;
strip.setPixelColor(i, breatheRed, breatheGrn, breatheBlu);
}
strip.show(); // write all the pixels out
delay(updatePeriod);
}
}
Re: NEOPIXEL brightness control
#define N_LEDS 16
Re: NEOPIXEL brightness control
//chaseFade....Make lights fade from low to high intensity while lighting one after another
#include <Adafruit_NeoPixel.h>
const int ledNum = 13; // Number of LED's
const int ledPin = 6; // ATtiny85 LED signal output pin #
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(ledNum, ledPin);
uint32_t Blue = pixels.Color(0, 255, 0);
uint16_t i, l;
#define DELAY 30 // Time delay between Pixels
void setup() {
pixels.begin();
pixels.show();
pinMode(ledPin, OUTPUT);
}
// chaseFade
void loop() {
int chase = 0;
uint8_t brightIncrement = 255 / pixels.numPixels();
for (chase; chase < 5; chase++) {
for (i = 0; i < ledNum; i++) {
pixels.setPixelColor(i, Blue, ((i + 1) * brightIncrement));
pixels.show();
delay(DELAY);
}
for (i = 0; i < ledNum; i++) {
pixels.setPixelColor(i, 0, 0, 0);
pixels.show();
}
delay(1000);
}
}
Re: NEOPIXEL brightness control
pixels.setPixelColor(i, Blue, ((i + 1) * brightIncrement));
Re: NEOPIXEL brightness control
setPixelColor(uint16_t n, uint32_t c, uint16_t brightness),
// Set pixel brightness individually using seperate R,G,B components:
void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint16_t brightness) {
setPixelColor(n, (brightness*r/255) , (brightness*g/255), (brightness*b/255));
}
// Set pixel brightness individually using a 'packed' 32-bit RGB color:
void Adafruit_NeoPixel::setPixelColor(uint16_t n, uint32_t c, uint16_t brightness) {
uint8_t
r = (uint8_t)(c >> 16),
g = (uint8_t)(c >> 8),
b = (uint8_t)c;
setPixelColor(n, r, g, b, brightness);
}