Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Last active December 27, 2023 04:48
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 brennanMKE/b4b69c89f7e6d0241b4d4936ab441a12 to your computer and use it in GitHub Desktop.
Save brennanMKE/b4b69c89f7e6d0241b4d4936ab441a12 to your computer and use it in GitHub Desktop.
LED light control without delays
/**
LED lights control with no delays.
The Blink example Sketch for Arduino uses delays to control the built-in LED to turn
it on and off repeatedly. Since the next loop is delayed nothing else can be done
while until the delay expires. This Sketch controls the built-in LED and a strip of
10 LED lights without delays. It sets timeouts so that once the current clock passes
the timeout it can trigger an action. There is an interval for the blink and color
change which cycles through the list of colors and blinks a number of times for the
current color position.
*/
#include <FastLED.h>
#define BLINK_LED 8 // Built-in LED
#define LED_PIN 0 // GPIO pin connected to the LED strip.
#define LED_COUNT 10 // Number of LEDs in the strip.
#define COLOR_ORDER GRB // Color order of your LED strip
#define CHIPSET WS2812B // Chipset of your LED strip
#define COLOR_INTERVAL 1500
#define COLOR_COUNT 7
#define COLOR_INDIGO CRGB(30, 0, 40)
#define COLOR_VIOLET CRGB(125,13,152)
CRGB leds[LED_COUNT];
int colorIndex = 0;
int blinkInterval = INT_MAX;
CRGB colors[] = { CRGB::Red, CRGB::Orange, CRGB::Yellow, CRGB::Green, CRGB::Blue, COLOR_INDIGO, COLOR_VIOLET };
char * names[] = { "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet" };
unsigned long changeColorTimeoutMillis = UINT_MAX;
unsigned long blinkTimeoutMillis = UINT_MAX;
bool ledOn = false;
int blinkCount = 0;
void setup() {
Serial.begin(115200);
pinMode(BLINK_LED, OUTPUT);
FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, LED_COUNT);
FastLED.setBrightness(25);
blinkInterval = COLOR_INTERVAL / COLOR_COUNT / 2;
changeColorTimeoutMillis = millis() + COLOR_INTERVAL;
blinkTimeoutMillis = millis() + blinkInterval;
Serial.println("Ready!");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis >= changeColorTimeoutMillis) {
changeColor();
}
if (currentMillis >= blinkTimeoutMillis) {
blink();
}
}
void changeColor() {
Serial.print("Color: [");
Serial.print(colorIndex);
Serial.print("] ");
Serial.println(names[colorIndex]);
fill_solid(leds, LED_COUNT, colors[colorIndex]);
FastLED.show();
// set the next timeout
changeColorTimeoutMillis = millis() + COLOR_INTERVAL;
colorIndex++;
if (colorIndex == COLOR_COUNT) {
colorIndex = 0;
}
blinkCount = colorIndex + 1;
}
void blink() {
if (!ledOn && blinkCount > 0) {
// on
digitalWrite(BLINK_LED, LOW);
ledOn = true;
blinkTimeoutMillis = millis() + blinkInterval;
} else {
// off
digitalWrite(BLINK_LED, HIGH);
ledOn = false;
blinkCount--;
if (blinkCount > 0) {
blinkTimeoutMillis = millis() + blinkInterval;
} else {
// sync with color change timeout
blinkTimeoutMillis = changeColorTimeoutMillis;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment