Created
February 29, 2024 18:11
-
-
Save Aleksi44/d8b0e53cdca1acffe52ba03c64e42486 to your computer and use it in GitHub Desktop.
Arduino FastLed linear blinking animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FastLED.h> | |
// change these values | |
#define DATA_PIN_PHOTO_1 3 | |
#define DATA_PIN_PHOTO_2 4 | |
#define DATA_PIN_PHOTO_3 5 | |
#define DATA_PIN_PHOTO_4 6 | |
#define ANIMATION_DURATION 10000 | |
// max offset is 1 | |
#define TICK_1_OFFSET 0.2 | |
#define TICK_2_OFFSET 0.4 | |
#define TICK_3_OFFSET 0.6 | |
#define TICK_4_OFFSET 0.8 | |
#define PHOTO_1_START_OFFSET 0 | |
#define PHOTO_2_START_OFFSET 0.2 | |
#define PHOTO_3_START_OFFSET 0.4 | |
#define PHOTO_4_START_OFFSET 0.6 | |
// do not change these values | |
#define TICK_TOTAL (ANIMATION_DURATION / ANIMATION_REFRESH_INTERVAL) | |
#define TICK_1 (TICK_TOTAL * TICK_1_OFFSET) | |
#define TICK_2 (TICK_TOTAL * TICK_2_OFFSET) | |
#define TICK_3 (TICK_TOTAL * TICK_3_OFFSET) | |
#define TICK_4 (TICK_TOTAL * TICK_4_OFFSET) | |
// do not set a value below 100 (freeze risk) | |
#define ANIMATION_REFRESH_INTERVAL 100 | |
CRGB photo1[1]; | |
CRGB photo2[1]; | |
CRGB photo3[1]; | |
CRGB photo4[1]; | |
int currentTick = 0; | |
void setup() { | |
// init logs | |
Serial.begin(9600); | |
while (!Serial); | |
Serial.println("Setup..."); | |
// change these values depending on the led strip | |
FastLED.addLeds<NEOPIXEL, DATA_PIN_PHOTO_1>(photo1, 1); | |
FastLED.addLeds<NEOPIXEL, DATA_PIN_PHOTO_2>(photo2, 1); | |
FastLED.addLeds<NEOPIXEL, DATA_PIN_PHOTO_3>(photo3, 1); | |
FastLED.addLeds<NEOPIXEL, DATA_PIN_PHOTO_4>(photo4, 1); | |
photo1[0] = CHSV(255, 0, 0); | |
photo2[0] = CHSV(255, 0, 0); | |
photo3[0] = CHSV(255, 0, 0); | |
photo4[0] = CHSV(255, 0, 0); | |
setup_debug(); | |
Serial.println("Setup OK"); | |
} | |
void setup_debug() { | |
Serial.print("TICK_1"); | |
Serial.println(TICK_1); | |
Serial.print("TICK_2="); | |
Serial.println(TICK_2); | |
Serial.print("TICK_3="); | |
Serial.println(TICK_3); | |
Serial.print("TICK_4="); | |
Serial.println(TICK_4); | |
Serial.print("TICK_TOTAL="); | |
Serial.println(TICK_TOTAL); | |
} | |
void loop() { | |
animation(photo1, currentTick + (TICK_TOTAL * PHOTO_1_START_OFFSET)); | |
animation(photo2, currentTick + (TICK_TOTAL * PHOTO_2_START_OFFSET)); | |
animation(photo3, currentTick + (TICK_TOTAL * PHOTO_3_START_OFFSET)); | |
animation(photo4, currentTick + (TICK_TOTAL * PHOTO_4_START_OFFSET)); | |
delay(ANIMATION_REFRESH_INTERVAL); | |
currentTick += 1; | |
currentTick %= TICK_TOTAL; | |
FastLED.show(); | |
} | |
void animation(CRGB *photo, int tick) { | |
uint8_t brightness; | |
if (tick <= TICK_1) { | |
// Light on | |
brightness = (tick / TICK_1) * 255; | |
photo[0] = CHSV(255, 0, brightness); | |
} | |
else if (tick <= TICK_2) { | |
// Light off | |
brightness = 255 - (((tick - TICK_1) / (TICK_2 - TICK_1)) * 255); | |
photo[0] = CHSV(255, 0, brightness); | |
} | |
else if (tick <= TICK_3) { | |
// Light on | |
brightness = ((tick - TICK_2) / (TICK_3 - TICK_2)) * 255; | |
photo[0] = CHSV(255, 0, brightness); | |
} | |
else if (tick <= TICK_4) { | |
// Light off | |
brightness = 255 - (((tick - TICK_3) / (TICK_4 - TICK_3)) * 255); | |
photo[0] = CHSV(255, 0, brightness); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment