Difference between revisions of "NeoPixel"

From Hive76 Wiki
Jump to: navigation, search
Line 1: Line 1:
<syntaxhighlight lang="cpp">
#include "FastLED.h"    // Loads FastLED library
#include "FastLED.h"    // Loads FastLED library
Line 43: Line 43:

Revision as of 17:33, 18 March 2017

  1. include "FastLED.h" // Loads FastLED library
  1. define NUM_LEDS 20 // How many NeoPixels on your strip?
  2. define DATA_PIN 3 // Data pin from microcontroller connected to LED strip
  3. define BUTTON_PIN 4 // Data pin to read button state
  4. define BUTTON_GND 6 // Data pin providing ground source to button, since we have limited mounting options for the Metro-Mini
  5. define KNOB_PIN A0 // analog input pin that is connected to the center pin of potentiometer, where pot position will be measured
  6. define KNOB_GND A1 // analog input pin that is connected to one side of potentiometer, and will be needed to supply ground

int buttonPressed = 1; // initial state of push button, since input is pulled high and we need to declare variable scope.

CRGB leds[NUM_LEDS]; // Tells the Library how many pixels we have

void setup() {

 // put your setup code here, to run once:
 FastLED.addLeds<NEOPIXEL,DATA_PIN>(leds, NUM_LEDS); // this specifies the configuration for the FastLED library
 pinMode(KNOB_GND, OUTPUT);                          // configures Analog pin to be an output...
 digitalWrite(KNOB_GND, LOW);                        // ...and writes a zero/low, to supply ground to the potentiometer
 pinMode(BUTTON_PIN, INPUT_PULLUP);                  // configures the digital pin for the button to be an input, and enable the internal pullup resistor
 pinMode(BUTTON_GND, OUTPUT);                        // configures the digital pin for the button ground to be an output...
 digitalWrite(BUTTON_GND, LOW);                      // ...and writes a zero/low, to supply ground to the button


void loop() {

 // put your main code here, to run repeatedly:
 int val = analogRead(KNOB_PIN);                 // reads the analog voltage of the potentiometer
 int colorOfLed = map(val, 0, 1023, 0, 255);     // this map function takes the 10-bit value read from potentiometer and converts to 8-bit value that can be used for the hue
 leds[0].setHSV( colorOfLed, 255, 200);          // sets the first NeoPixel to the hue of the potentiometer, with full color saturation and 200 brightness
 FastLED.show();                                 // sends the above data to the NeoPixels
 buttonPressed = digitalRead(BUTTON_PIN);        // read and store button state
 if (buttonPressed == LOW) {                     // logic check if button is currently pressed
   for (int i = 1; i < NUM_LEDS; i++) {          // this for loop starts at second pixel (1), then increments pixels until until it reachs the last LED on the strip
     leds[i] = CHSV( colorOfLed, 255, 200);      // sets the incremented pixel to the hue set by pot, full color saturation, 200 brightness
     FastLED.show();                             // writes data to the NeoPixels
     leds[i-1] = CRGB::Black;                    // blanks out the previous pixel
     FastLED.show();                             // writes data to the NeoPixels
     FastLED.delay(15);                          // delays 15ms between movement of each pixel
   FastLED.clear();                              // turns off the pixels, and writes immediately, when the pixel animation loop above completes