Arduino Code

The Arduino code presented below works equally well on all versions of GEMMA: v2 and M0. It also works on all versions of Trinket and Trinket M0. But if you have an M0 board, consider using the CircuitPython code on the next page of this guide, no Arduino IDE required! Click to Download the NeoPixel Library

If this is your first time using Trinket or Gemma, work through the guides first:

You need to customize some settings in the Arduino IDE. Once you have it up and running (test the 'blink' sketch), then download and install the NeoPixel library:

Installing Arduino libraries is a frequent stumbling block. If this is your first time, or simply needing a refresher, please read the All About Arduino Libraries tutorial.

If the library is correctly installed (and the Arduino IDE is restarted), you should be able to navigate through the “File” rollover menus as follows:

File→Sketchbook→Libraries→Adafruit_NeoPixel→strandtest

You’ll need to change one line in this code. If using Trinket, change line #4 to:

#define PIN 4
And for Gemma, use:
#define PIN 1
From the Tools→Board menu, select Adafruit Trinket 8 MHz or Adafruit Gemma as appropriate. Connect the USB cable between the computer and Trinket, press the reset button on the board, then click the upload button (right arrow icon) in the Arduino IDE. When the battery is connected, you should get a light show from the LEDs.

If that’s working, you can then copy and paste this Larson scanner code into a new sketch. Change N_LEDS to match the number of LEDs in your strip, and if using Gemma change PIN to 1. Upload and get ready to be a Cylon!
#include <Adafruit_NeoPixel.h>

#define N_LEDS 22
#define PIN     4

Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
}

int pos = 0, dir = 1; // Position, direction of "eye"

void loop() {
  int j;

  // Draw 5 pixels centered on pos.  setPixelColor() will clip any
  // pixels off the ends of the strip, we don't need to watch for that.
  strip.setPixelColor(pos - 2, 0x100000); // Dark red
  strip.setPixelColor(pos - 1, 0x800000); // Medium red
  strip.setPixelColor(pos    , 0xFF3000); // Center pixel is brightest
  strip.setPixelColor(pos + 1, 0x800000); // Medium red
  strip.setPixelColor(pos + 2, 0x100000); // Dark red

  strip.show();
  delay(30);

  // Rather than being sneaky and erasing just the tail pixel,
  // it's easier to erase it all and draw a new one next time.
  for(j=-2; j<= 2; j++) strip.setPixelColor(pos+j, 0);

  // Bounce off ends of strip
  pos += dir;
  if(pos < 0) {
    pos = 1;
    dir = -dir;
  } else if(pos >= strip.numPixels()) {
    pos = strip.numPixels() - 2;
    dir = -dir;
  }
}
Last updated on 2017-11-18 at 10.02.29 PM Published on 2013-09-23 at 04.55.27 PM