Single Pot RGB WS2812B (Neopixel) Color Change With Arduino (Using Atiny85 3.3v)
by StevenD6 in Circuits > Arduino
2182 Views, 0 Favorites, 0 Comments
Single Pot RGB WS2812B (Neopixel) Color Change With Arduino (Using Atiny85 3.3v)
This will explain how to set up a single Pot to change through all RGB colors using the neopixel library and HSV color values with the Arduino (Any)
I assume you already have the neopixel library installed and have wired up your RGB WS2812B LED and pot to the Arduino.
#include
#define PIN 1 //output pin to LED's #define NUMPIXELS 4 //Number of LED's Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); const int numReadings = 15; // Higher value will result in more stability // but add more lag
int readings[numReadings]; // the readings from the analog input int readIndex = 0; // the index of the current reading int total = 0; // the running total int average = 0; // the average int x; void loop(){
total = total - readings[readIndex]; readings[readIndex] = analogRead(1); //or what ever analog pin your using for the pot total = total + readings[readIndex]; readIndex = readIndex + 1; if (readIndex >= numReadings) { readIndex = 0; } average = total / numReadings; y = map(average,0,1024,0,65536); pixels.clear(); // Set all pixel colors to 'off' delay(25); uint32_t rgbcolor = pixels.ColorHSV(y); pixels.fill(rgbcolor); pixels.show(); // Send the updated pixel colors to the hardware. delay(10); }
The first set of code sets everything up, including initializing our variables and takes readings from our pot and averages it out giving smooth readings.
total = total - readings[readIndex];
readings[readIndex] = analogRead(1); //or what ever pin your using for the pot total = total + readings[readIndex]; readIndex = readIndex + 1; if (readIndex >= numReadings) { readIndex = 0; } average = total / numReadings;
Next we map out the smoothed values to HSV values.
y = map(average,0,1024,0,65536); // HSV range is from 0-65536, looping back around, // starting at red and ending at red
Then we clear the pixel values and have a small delay
pixels.clear();
delay(25);
And finally convert the mapped pot values to HSV colors and fill all pixels with that color.
uint32_t rgbcolor = pixels.ColorHSV(y); // The main line of code that makes this all // possible would be converting the pot values // into hsv color space pixels.fill(rgbcolor); pixels.show();
Supplies
LINKS:
Adafruit HSV and Color / library useage
https://learn.adafruit.com/adafruit-neopixel-uberg...
Neopixel led pinout and specs
https://cdn-shop.adafruit.com/datasheets/WS2812B.p...
Adafruit neopixel libary docs
https://adafruit.github.io/Adafruit_NeoPixel/html/...
NeoPixel library install guide