Last active
November 15, 2020 20:16
-
-
Save cleaver/83344c0f3fea32cdb8495e2af6a5ac51 to your computer and use it in GitHub Desktop.
Making VU meter type display with Neopixel ring.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Adafruit_NeoPixel.h> | |
// Which pin on the Arduino is connected to the NeoPixels? | |
// On a Trinket or Gemma we suggest changing this to 1: | |
#define LED_PIN 13 | |
// How many NeoPixels are attached to the Arduino? | |
#define LED_COUNT 24 | |
#define HUE_STEP 512; | |
// Declare our NeoPixel strip object: | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800); | |
// Argument 1 = Number of pixels in NeoPixel strip | |
// Argument 2 = Arduino pin number (most are valid) | |
// Argument 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) | |
int level = 0; | |
uint32_t safe = strip.Color(0, 255, 0); | |
uint32_t warn = strip.Color(255, 255, 0); | |
uint32_t danger = strip.Color(255, 0, 0); | |
uint32_t color = safe; | |
int analogPin = A0; | |
int maxInput = 1024; | |
void setup() { | |
strip.begin(); | |
strip.clear(); // Initialize all pixels to 'off' | |
strip.setBrightness(10); | |
// randomSeed(analogRead(A0)); | |
} | |
void loop() { | |
strip.clear(); | |
level += change(); | |
if (level >= LED_COUNT) { | |
level = LED_COUNT; | |
} | |
else if (level <= 0) { | |
level = 1; | |
} | |
level = analogRead(analogPin) / (maxInput / LED_COUNT); | |
color = safe; | |
if (level > 19) { | |
color = danger; | |
} | |
else if (level > 14) { | |
color = warn; | |
} | |
strip.fill(color, 0, level); | |
strip.show(); | |
delay(50); | |
} | |
int change() { | |
return random(5) - 2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment