Neopixel

From STEAMwiki
Jump to: navigation, search

Neopixel Library can be downloaded here

Sample Code:

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif


#define PIN            6
#define NUMPIXELS      16


Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500; // delay for half a second

void setup() {

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

  setAll(255,255,255);
  delay(5000);
  setRange(0,30,255,0,0);
  delay(5000);

}



void setAll(int red, int green, int blue){

  for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(red,green,blue)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
}

void setRange(int startPix, int endPix, int red, int green, int blue){

  for(int i=startPix;i<endPix;i++){

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(red,green,blue)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
}

8x8 grid with sample writing:

// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif


#define PIN            6
#define NUMPIXELS      64


Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 500; // delay for half a second

void setup() {

  pixels.begin(); // This initializes the NeoPixel library.
}

void loop() {

  write3(10, 10, 10);
  delay(1000);
  write2(0,10,0);
  delay(1000);
  write1(0, 0, 10);
  delay(1000);

}



void setAll(int red, int green, int blue) {

  for (int i = 0; i < NUMPIXELS; i++) {

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(red, green, blue)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

  }
}

void write3 (int red, int green, int blue) {

  setAll(0,0,0);
  
  pixels.setPixelColor(2, pixels.Color(red, green, blue));
  pixels.setPixelColor(3, pixels.Color(red, green, blue));
  pixels.setPixelColor(4, pixels.Color(red, green, blue));
  pixels.setPixelColor(5, pixels.Color(red, green, blue));

  pixels.setPixelColor(10, pixels.Color(red, green, blue));
  pixels.setPixelColor(11, pixels.Color(red, green, blue));
  pixels.setPixelColor(12, pixels.Color(red, green, blue));
  pixels.setPixelColor(13, pixels.Color(red, green, blue));

  pixels.setPixelColor(20, pixels.Color(red, green, blue));
  pixels.setPixelColor(21, pixels.Color(red, green, blue));

  pixels.setPixelColor(26, pixels.Color(red, green, blue));
  pixels.setPixelColor(27, pixels.Color(red, green, blue));
  pixels.setPixelColor(28, pixels.Color(red, green, blue));
  pixels.setPixelColor(29, pixels.Color(red, green, blue));

  pixels.setPixelColor(34, pixels.Color(red, green, blue));
  pixels.setPixelColor(35, pixels.Color(red, green, blue));
  pixels.setPixelColor(36, pixels.Color(red, green, blue));
  pixels.setPixelColor(37, pixels.Color(red, green, blue));

  pixels.setPixelColor(43, pixels.Color(red, green, blue));
  pixels.setPixelColor(42, pixels.Color(red, green, blue));

  pixels.setPixelColor(50, pixels.Color(red, green, blue));
  pixels.setPixelColor(51, pixels.Color(red, green, blue));
  pixels.setPixelColor(52, pixels.Color(red, green, blue));
  pixels.setPixelColor(53, pixels.Color(red, green, blue));

  pixels.setPixelColor(58, pixels.Color(red, green, blue));
  pixels.setPixelColor(59, pixels.Color(red, green, blue));
  pixels.setPixelColor(60, pixels.Color(red, green, blue));
  pixels.setPixelColor(61, pixels.Color(red, green, blue));


  pixels.show(); // This sends the updated pixel color to the hardware.

}

void write2 (int red, int green, int blue) {
  setAll(0,0,0);
  pixels.setPixelColor(2, pixels.Color(red, green, blue));
  pixels.setPixelColor(3, pixels.Color(red, green, blue));
  pixels.setPixelColor(4, pixels.Color(red, green, blue));
  pixels.setPixelColor(5, pixels.Color(red, green, blue));

  pixels.setPixelColor(10, pixels.Color(red, green, blue));
  pixels.setPixelColor(11, pixels.Color(red, green, blue));
  pixels.setPixelColor(12, pixels.Color(red, green, blue));
  pixels.setPixelColor(13, pixels.Color(red, green, blue));

  pixels.setPixelColor(18, pixels.Color(red, green, blue));
  pixels.setPixelColor(19, pixels.Color(red, green, blue));

  pixels.setPixelColor(26, pixels.Color(red, green, blue));
  pixels.setPixelColor(27, pixels.Color(red, green, blue));
  pixels.setPixelColor(28, pixels.Color(red, green, blue));
  pixels.setPixelColor(29, pixels.Color(red, green, blue));

  pixels.setPixelColor(34, pixels.Color(red, green, blue));
  pixels.setPixelColor(35, pixels.Color(red, green, blue));
  pixels.setPixelColor(36, pixels.Color(red, green, blue));
  pixels.setPixelColor(37, pixels.Color(red, green, blue));

  pixels.setPixelColor(43, pixels.Color(red, green, blue));
  pixels.setPixelColor(42, pixels.Color(red, green, blue));

  pixels.setPixelColor(50, pixels.Color(red, green, blue));
  pixels.setPixelColor(51, pixels.Color(red, green, blue));
  pixels.setPixelColor(52, pixels.Color(red, green, blue));
  pixels.setPixelColor(53, pixels.Color(red, green, blue));

  pixels.setPixelColor(58, pixels.Color(red, green, blue));
  pixels.setPixelColor(59, pixels.Color(red, green, blue));
  pixels.setPixelColor(60, pixels.Color(red, green, blue));
  pixels.setPixelColor(61, pixels.Color(red, green, blue));


  pixels.show(); // This sends the updated pixel color to the hardware.

}

void write1 (int red, int green, int blue) {
  setAll(0,0,0);
  
  pixels.setPixelColor(3, pixels.Color(red, green, blue));
  pixels.setPixelColor(11, pixels.Color(red, green, blue));
  pixels.setPixelColor(19, pixels.Color(red, green, blue));
  pixels.setPixelColor(27, pixels.Color(red, green, blue));

  pixels.setPixelColor(35, pixels.Color(red, green, blue));
  pixels.setPixelColor(43, pixels.Color(red, green, blue));
  pixels.setPixelColor(51, pixels.Color(red, green, blue));
  pixels.setPixelColor(59, pixels.Color(red, green, blue));

  pixels.setPixelColor(4, pixels.Color(red, green, blue));
  pixels.setPixelColor(12, pixels.Color(red, green, blue));

  pixels.setPixelColor(20, pixels.Color(red, green, blue));
  pixels.setPixelColor(28, pixels.Color(red, green, blue));
  pixels.setPixelColor(36, pixels.Color(red, green, blue));
  pixels.setPixelColor(44, pixels.Color(red, green, blue));

  pixels.setPixelColor(52, pixels.Color(red, green, blue));
  pixels.setPixelColor(60, pixels.Color(red, green, blue));
 

  pixels.show(); // This sends the updated pixel color to the hardware.

}