Sunspot Home

more notes for a bad memory - - -

Testing a WS2812 8 LED bar display

WS2812

Objective
Use for simple LED uses for Arduino - no flashing rainbow modes etc
connect 8 3 colour LEDs on one data pin !!!
First test - make a bar display of a voltage value - could be used for sound level monitor etc

Method
Use Adafruit_NeoPixel.h (thanks!)
code backup

These 3 colour LEDS are blinding bright on full brightness - - - strip.Color(256, 256, 256)
The 8 LED version tested here can use the Arduino 5 V output - any more LEDs would need external power

Circuit
The data line of the bar goes to D8 via 470 ohms in this test - 470 mfd across power supply (Adafruit advises 1000mfd)
A 10 Kohm potentiometer connects across 5 V - center wiper to A0 analog input
A bar of LEDs lights up with number and colour depending on the input voltage

#include <Adafruit_NeoPixel.h>

#define PIN 8

Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
strip.begin();// Initialize all the variables

strip.show(); // Initialize all pixels to 'off'

Serial.begin(9600);
}

void loop() {

int sensorValue = analogRead(A0);
Serial.println(sensorValue);

BarDisplay(sensorValue);

}

// function to create bar of LEDs depending on voltage on A0
// color values chosen for even brightness
void BarDisplay(int x) {
// set all leds to go off unless state changed below
for (int i=0; i <= 7; i++){strip.setPixelColor(i, strip.Color(0, 0, 0));}
if (x >= 113) { strip.setPixelColor(0, strip.Color(30, 30, 30));} // white
if (x >= 227) { strip.setPixelColor(1, strip.Color(30, 30, 30));} // white
if (x > 340 && x <= 454)
{for (int i=0; i <= 2; i++){strip.setPixelColor(i, strip.Color(0, 60, 0));}} // green}
if (x > 454 && x<= 568)
{for (int i=0; i <= 3; i++){strip.setPixelColor(i, strip.Color(0, 60, 0));}} // green}
if (x > 568 && x<= 682)
{for (int i=0; i <= 4; i++){strip.setPixelColor(i, strip.Color(0, 0, 60));}} // blue
if (x > 682 && x<= 796)
{for (int i=0; i <= 5; i++){strip.setPixelColor(i, strip.Color(0, 0, 60));}} // blue
if (x > 796 && x<= 911)
{for (int i=0; i <= 6; i++){strip.setPixelColor(i, strip.Color(40, 0, 0));}} // red
if (x > 911)
{for (int i=0; i <= 7; i++){strip.setPixelColor(i, strip.Color(40, 0, 0));}} // red
strip.show();
}

// note - 8 steps as gaps between these numbers - 0 113 227 340 454 568 682 796 911 1024

Please email me if you want to swap notes

SUNSPOT HOME

more to come . . .