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:
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
#define PIN 1
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!
// SPDX-FileCopyrightText: 2017 Mikey Sklar for Adafruit Industries // // SPDX-License-Identifier: MIT #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; } }
This code works equally well on an Adafruit Hallowing board with a NeoPixel strip plugged into the NEOPIX port. Just change the number on the “N_LEDS” line to match your strip length. No need to change PIN, the default (4) already matches the NeoPixel connector on this board.
Text editor powered by tinymce.