Components and supplies
1
Neopixel Stick with 8 RGB WS2812 LEDs and integrated driver
1
Arduino Nano
Apps and platforms
1
Arduino_IDE 2.3.6
Project description
Code
Code
c
1#include <Adafruit_NeoPixel.h> // NeoPixel library. Download if you don't have 2 3#define PIXEL_PIN 3 // Input pin Neopixel is attached to 4#define NUMPIXELS 16 // Number of neopixels in strip, change if using another quantity; 5 6#define POT_PIN_R A0 // Potentiometer on analog pin A0 7#define POT_PIN_G A1 // Potentiometer on analog pin A1 8#define POT_PIN_B A2 // Potentiometer on analog pin A2 9#define POT_PIN_Br A3 // Potentiometer on analog pin A3 10 11Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800); 12 13// Initialize the variables 14int redColor; 15int greenColor; 16int blueColor; 17int potValue; 18 19void setup() { 20 Serial.begin(9600); // Initialize serial 21 pixels.begin(); // Initialize the NeoPixel library. 22 pixels.show(); // Update strip with new contents 23 pixels.setBrightness(50); // Set a initial brightness here 24 25 rainbow(3); // NeoPixel Test. Flowing rainbow cycle along the whole strip 26}; 27 28 29void loop() { 30 potValue = analogRead(POT_PIN_R); // Read the potentiometer value (0 to 1023) 31 redColor = map(potValue, 0, 1023, 0, 255); // Map the value to a brightness level (0 to 255) 32 potValue = analogRead(POT_PIN_G); // Read the potentiometer value (0 to 1023) 33 greenColor = map(potValue, 0, 1023, 0, 255); // Map the value to a brightness level (0 to 255) 34 potValue = analogRead(POT_PIN_B); // Read the potentiometer value (0 to 1023) 35 blueColor = map(potValue, 0, 1023, 0, 255); // Map the value to a brightness level (0 to 255) 36 37 //int potValue = analogRead(POT_PIN_Br); // Read the potentiometer value (0 to 1023) 38 //int brightness = map(potValue, 0, 1023, 0, 255); // Map the value to a brightness level (0 to 255) 39 //pixels.setBrightness(brightness); // Set the brightness for the entire strip 40 41 // Prints the serial values of each potentiometer (Red, green, Blue). This is optional. 42 Serial.print("r: "); Serial.println(redColor); 43 Serial.print("g: "); Serial.println(greenColor); 44 Serial.print("b: "); Serial.println(blueColor); 45 //Serial.print("brightness: "); Serial.println(brightness); 46 Serial.println(""); 47 48 for (int i=0; i < NUMPIXELS; i++) { 49 // Pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 50 pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor)); 51 }; 52 53 pixels.show(); // This sends the updated pixel color to the hardware. 54 delay(10); // delay for a period of time (in milliseconds). Small delay for stability 55}; 56 57 58//// Rainbow cycle along whole strip. Pass delay time (in ms) between frames. 59void rainbow(int wait) { 60 for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) { 61 pixels.rainbow(firstPixelHue); 62 pixels.show(); // Update strip with new contents 63 delay(wait); // Pause for a moment 64 }; 65};
Comments
Only logged in users can leave comments