User Tools

Site Tools


courses:intro.prototyping.neopixels

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
courses:intro.prototyping.neopixels [2019/01/22 02:56]
rickard
courses:intro.prototyping.neopixels [2019/01/22 05:32] (current)
rickard
Line 1: Line 1:
 +==== Neopixels ====
 +
 +
 +Neopixel is [[http://​www.adafruit.com|Adafruit'​s]] name for a type of RGB-leds that can communicate with each other, making it fairly easy to set up a bunch and control them without needing as many cables as you would with conventional LEDs.
 +
 +In their section for tutorials, Adafruit has a comprehensive tutorial on Neopixels in their [[https://​learn.adafruit.com/​adafruit-neopixel-uberguide|Überguide]],​ including how to add more power if you end up with more lights than the Arduino can feed.
 +
 +
 +Here is a bare minimum example of blinking a Neopixel:
 +<code java>
 +#include <​Adafruit_NeoPixel.h>​
 +
 +int pixelPin = 3;
 +int amountNeopixels = 3;
 +int interval = 500;
 +
 +// Parameter 1 = number of pixels in strip
 +// Parameter 2 = pin number (most are valid)
 +// Parameter 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)
 +Adafruit_NeoPixel strip = Adafruit_NeoPixel(amountNeopixels,​ pixelPin, NEO_GRB + NEO_KHZ800);​
 +
 +void setup()
 +{
 +  strip.begin();​
 +  strip.show();​ // Initialize all pixels to '​off'​
 +}
 +
 +void loop()
 +{
 +  //​setPixelColor(index,​ R, G, B); //0-255
 +
 +  strip.setPixelColor(0,​ 255, 255, 255);
 +  strip.show();​
 +
 +  delay(interval);​
 +
 +  strip.setPixelColor(0,​ 0, 255, 0);
 +  strip.show();​
 +
 +  delay(interval);​
 +}
 +
 +</​code>​
 +
 +First and foremost you need to download Adafruits Neopixel-library. Menu choice "​sketch"​->​ include library -> manage libraries. Search and install Adafruit Neopixel.
 +Early in the code you create a representative object called "​strip"​ which we use to control our length of Neopixels. In this case it is only one, but we could control a lot more. In the IxL windows there is a stretch of 300 Neopixels. Using that many, though, means that they also need external powersources.
 +
 +
 +Once we have our "​strip"​ we can start defining how we want the Neopixel to light up next. The command strip.setPixelColor() needs four parameters. Pixel-index,​ and levels of red, green, and blue. Pixel-index is just which RGB-led in order we are configuring. Remember that a computer starts counting on 0, so the first one is 0, the second is 1 and so on. The levels of color is a number ranging from 0 to 255. In the example we are only setting up one Neopixel, but we can follow that up with any number of configurations. Each one with a new call of strip.setPixelColor(). If you want some randomness to your color, try the command random() as your color level.
 +
 +
 +Once we've configured the lights we use strip.show() to apply the colors and this is when they actually light up. This combination of setPixelColor() and show() makes it possible for us to define a complex color scheme before showing it.
 +
 +
 +The example uses these commands to set up the Neopixel to first show white, using delay() it pauses so we can see it, then setting the Neopixel to show green, and then another pause. ​
 +
 +==== Progression ====
 +
 +But what if you want to fade up? Or down?
 +
 +<code java>
 +void loop()
 +{
 +  //​setPixelColor(index,​ R, G, B); //0-255
 +
 +  for(int i=0; i<255; i++)
 +  {
 +    strip.setPixelColor(0,​ i, i, i);
 +    strip.show();​
 +    delay(10);
 +  }
 +
 +  delay(500);
 +
 +  for(int i=255; i>0; i--)
 +  {
 +    strip.setPixelColor(0,​ i, i, i);
 +    strip.show();​
 +    delay(10);
 +  }
 +
 +  delay(500);
 +}
 +</​code>​
 +
 +For this, we can use the for-loop. A function we've touched on in Processing for when we want to do something many times. A for-loop has three defining parts. ​
 +for(A;​B;​C) ​
 +
 +* A is defining a counter variable and setting a start value, in our example it is an integer called i set to 0.
 +
 +* B is the circumstance that limits the for-loop. In this case, we run while i is less than 255.
 +
 +* C is what we do with i each time around. This time it's adding one to whatever I currently is. i++
 +
 +
 +For the second for-loop we are counting down from 255 to 0 instead. The curly brackets following for-loop are as usual there to show what piece of code is repeated.
 +
 +As we loop through this 255 times we then use the value our variable i has each round to set the color in strip.setPixelColor. Looping from 0 to 255 and then from 255 to 0.
  
courses/intro.prototyping.neopixels.txt · Last modified: 2019/01/22 05:32 by rickard