Skip to content

Instantly share code, notes, and snippets.

@atuline
Last active May 12, 2018 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atuline/98cbe51de4f35c73dcadc057d54b692b to your computer and use it in GitHub Desktop.
Save atuline/98cbe51de4f35c73dcadc057d54b692b to your computer and use it in GitHub Desktop.
/* mycircle
*
* By: Andrew Tuline
*
* Date: May, 2018
*
* Connect your LED's end to end and this routine goes round and round.
*
* There are 2 methods for timing:
*
* 1) Use EVERY_N_MILLIS() to call mycircle() every 50ms.
* 2) Use millis() as a counter and divide by 50.
*
* Adjust fade value to suit. Low value = slow fade.
*
* Neither of these use a delay() statement, thus ensuring everything is running at full speed.
*
*/
#include "FastLED.h" // FastLED library.
#if FASTLED_VERSION < 3001000
#error "Requires FastLED 3.1 or later; check github for latest code."
#endif
// Fixed definitions cannot change on the fly.
#define LED_DT 12 // Data pin to connect to the strip.
#define LED_CK 11 // Clock pin for WS2801 or APA102.
#define COLOR_ORDER BGR // It's GRB for WS2812 and BGR for APA102.
#define LED_TYPE APA102 // Using APA102, WS2812, WS2801. Don't forget to change LEDS.addLeds.
#define NUM_LEDS 60 // Number of LED's.
// Global variables can be changed on the fly.
uint8_t max_bright = 255; // Overall brightness definition. It can be changed on the fly.
struct CRGB leds[NUM_LEDS]; // Initialize our LED array.
// Palette definitions
CRGBPalette16 currentPalette;
void setup() {
Serial.begin(57600); // Initialize serial port for debugging.
delay(1000); // Soft startup to ease the flow of electrons.
LEDS.addLeds<LED_TYPE, LED_DT, LED_CK, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2801 or APA102
// LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS); // Use this for WS2812
currentPalette = RainbowColors_p;
FastLED.setBrightness(max_bright);
set_max_power_in_volts_and_milliamps(5, 500); // FastLED Power management set at 5V, 500mA.
} // setup()
void loop() {
EVERY_N_MILLIS(50) { // Use EVERY_N_MILLIS() instead of delay(50). Delays are the work of the devil.
mycircle();
}
mycircle2(); // This method uses millis() as a counter.
FastLED.show();
EVERY_N_MILLIS(20) {
fadeToBlackBy(leds, NUM_LEDS, 64); // Lots of fade. Adjust to suit.
}
} // loop()
void mycircle() {
static uint16_t i = 0; // Just in case the length is > 255.
i++;
i = i % NUM_LEDS;
leds[i] = ColorFromPalette( currentPalette, 0, 255, LINEARBLEND); // Use palette lookup for a greater possibility of colours if required.
} // mycircle()
void mycircle2() {
uint16_t i = millis()/50 % NUM_LEDS; // Use millis() as a counter and use modulo to limit maximum value.
leds[i] = ColorFromPalette( currentPalette, 64, 255, LINEARBLEND); // Use palette lookup for a greater possibility of colours if required.
} // mycircle2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment