NeoPixel: Difference between revisions

From Hive76 Wiki
Jump to navigationJump to search
(set formatting & add title information)
(added some more info and headings)
Line 1: Line 1:
== Install the Arduino IDE ==
Grab the appropriate Arduino software from their website: https://www.arduino.cc/en/Main/Software
<br>
Follow the installation instructions for your operating system: https://www.arduino.cc/en/Guide/HomePage
== Class demo source code ==
Below, you will find the demo source code for the Hive76 NeoPixel class. Copy and paste this into the Arduino IDE to get started!
Below, you will find the demo source code for the Hive76 NeoPixel class. Copy and paste this into the Arduino IDE to get started!



Revision as of 18:04, 18 March 2017

Install the Arduino IDE

Grab the appropriate Arduino software from their website: https://www.arduino.cc/en/Main/Software
Follow the installation instructions for your operating system: https://www.arduino.cc/en/Guide/HomePage

Class demo source code

Below, you will find the demo source code for the Hive76 NeoPixel class. Copy and paste this into the Arduino IDE to get started!

#include "FastLED.h"    // Loads FastLED library

#define NUM_LEDS 20     // How many NeoPixels on your strip? 
#define DATA_PIN 3      // Data pin from microcontroller connected to LED strip
#define BUTTON_PIN 4    // Data pin to read button state 
#define BUTTON_GND 6    // Data pin providing ground source to button, since we have limited mounting options for the Metro-Mini
#define KNOB_PIN A0     // analog input pin that is connected to the center pin of potentiometer, where pot position will be measured
#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
  }
}