shapingthings

Neopixel Example with Pot - http://www.drumminhands.com/2015

Jan 30th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. /*
  2. * Code created by Chris Evans @drumminhands
  3. * Used to map a potentiometer to color hue
  4. *
  5. */
  6.  
  7. // sensor input from http://www.arduino.cc/en/Tutorial/AnalogInput
  8. // NeoPixels from here: http://learn.adafruit.com/adafruit-neopixel-uberguide/overview
  9.  
  10. #include <Adafruit_NeoPixel.h>
  11.  
  12. //constansts won't change
  13. const uint8_t neoPin = 6; // Neopixels Pin
  14. const uint8_t pxls = 12; // number of pixels in the neopixel ring
  15. const uint8_t maxBright = 255; // the default brightness between 0 and 255
  16.  
  17. // variables that will change
  18. int sensorPin = A0; // the input pin for the potentiometer
  19. int sensorValue = 0; // variable to store the value coming from the sensor
  20. int neoPixelHue = 0; // stores current hue of the neoPixels during the rainbow cycles
  21.  
  22. // Parameter 1 = number of pixels in strip
  23. // Parameter 2 = Arduino pin number (most are valid)
  24. // Parameter 3 = pixel type flags, add together as needed:
  25. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  26. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  27. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  28. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  29. Adafruit_NeoPixel strip = Adafruit_NeoPixel(pxls, neoPin, NEO_GRB + NEO_KHZ800);
  30.  
  31. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  32. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  33. // and minimize distance between Arduino and first pixel. Avoid connecting
  34. // on a live circuit...if you must, connect GND first.
  35.  
  36. void setup() {
  37. strip.begin();
  38. strip.setBrightness(maxBright); // initialize brightness
  39. strip.show(); // Initialize all pixels to 'off'
  40. }
  41.  
  42. void loop() {
  43. sensorValue = analogRead(sensorPin); // read the value from the sensor
  44. sensorValue = map(sensorValue, 0, 1023, 0, 255); // change value porportionally to be between 0 and 255
  45. colorSet(Wheel(sensorValue)); // set the color based on the pot
  46. }
  47.  
  48. // sets all pixels to one given color
  49. void colorSet(uint32_t c){
  50. for(uint16_t i=0; i<strip.numPixels(); i++) {
  51. strip.setPixelColor(i, c);
  52. }
  53. strip.show();
  54. }
  55.  
  56. // Input a value 0 to 255 to get a color value.
  57. // The colours are a transition r - g - b - back to r.
  58. uint32_t Wheel(byte WheelPos) {
  59. WheelPos = 255 - WheelPos;
  60. if(WheelPos < 85) {
  61. return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  62. } else if(WheelPos < 170) {
  63. WheelPos -= 85;
  64. return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  65. } else {
  66. WheelPos -= 170;
  67. return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  68. }
  69. }
Add Comment
Please, Sign In to add comment