Skip to content

Instantly share code, notes, and snippets.

@cashlo
Created November 7, 2024 16:04
Show Gist options
  • Save cashlo/36a002ab1efa5d24e62c5a77443272ca to your computer and use it in GitHub Desktop.
Save cashlo/36a002ab1efa5d24e62c5a77443272ca to your computer and use it in GitHub Desktop.
Couleur Clarity LED sign
#include "M5Atom.h"
#include <FastLED.h>
#define NUM_LEDS 140 // Number of LEDs in your strip
#define DATA_PIN 25 // Data pin connected to your LED strip
#define BUTTON_PIN G39
#define STAR_START 57 // Start index of the "star" effect
#define STAR_END 134 // End index of the "star" effect
CRGB leds[NUM_LEDS]; // Create an array of LEDs
int activeStars = 0;
volatile bool modeChanged = false;
int mode = 0;
int animationCounter = 0;
void IRAM_ATTR handleButtonPress() {
modeChanged = true;
}
void setup() {
M5.begin();
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS); // Initialize FastLED
FastLED.clear(); // Clear the strip initially
pinMode(BUTTON_PIN, INPUT_PULLUP);
attachInterrupt(BUTTON_PIN, handleButtonPress, FALLING); // Attach interrupt to button
// animateCouleurClarity(CRGB::White, true);
}
void loop() {
if (modeChanged) { // Check if button interrupt set the flag
modeChanged = false;
mode = (mode + 1) % 8; // Cycle through modes
animationCounter = 0;
}
switch (mode) {
case 0:
memberAnimation(CRGB::White);
break;
case 1:
memberAnimation(CRGB::Yellow);
break;
case 2:
memberAnimation(CRGB::Red);
break;
case 3:
memberAnimation(CRGB::Purple);
break;
case 4:
memberAnimation(CRGB::DarkOrange);
break;
case 5:
memberAnimation(CRGB::Green);
break;
case 6:
memberAnimation(CRGB::Blue);
break;
case 7:
memberAnimation(CRGB::DeepPink);
break;
}
delay(50);
}
void memberAnimation(CRGB color){
animateCouleurClarity(color , false);
starEffect();
if (animationCounter> 100) {
if (color == CRGB::DeepPink) {
taigaMomo();
} else if (color == CRGB::White)
rainbowEffect();
else {
animateCouleurClarity(color, true);
}
animationCounter = 0;
} else {
animationCounter++;
}
}
const int segment1[] = {7, 8, 9, 10, 11}; // Replace with actual indices
const int segment2[] = {12, 13, 14 };
const int segment3[] = {15, 16, 17, 18, 19, 20, 21};
const int segment4[] = {34, 28, 27, 26, 25};
const int segment5[] = {35, 36, 37, 38, 39};
const int segment6[] = {42, 43, 44, 45, 46};
const int segment7[] = {49, 50, 51, 52, 53};
const int segmentSizes[] = {5,3,7,5,5,5,5}; // Each segment size
const int* segments[] = {segment1, segment2, segment3, segment4, segment5, segment6, segment7};
int segmentOrder[] = {0, 1, 2, 3, 4, 5, 6};
void rainbowEffect(){
int ledStart = 6;
int ledEnd = 56;
CRGB segmentColors[] = {CRGB::Purple, CRGB::Blue, CRGB::Green, CRGB::DeepPink, CRGB::Red, CRGB::DarkOrange, CRGB::Yellow};
int segmentLength = (ledEnd - ledStart + 1) / 7;
shuffleSegments();
FastLED.clear();
// Stage 1: Flash each segment in random order
for (int s = 0; s < 7; s++) {
int segmentIndex = segmentOrder[s]; // Get the randomized segment index
for (int j = 0; j < segmentSizes[segmentIndex]; j++) {
leds[segments[segmentIndex][j]] = segmentColors[segmentIndex];
}
FastLED.show();
delay(300); // Delay between each segment flash
FastLED.clear(); // Clear before the next segment flash
}
// Stage 2: Display all segments with their colors at once
for (int s = 0; s < 7; s++) {
for (int j = 0; j < segmentSizes[s]; j++) {
leds[segments[s][j]] = segmentColors[s];
}
}
FastLED.show();
delay(2000); // Hold the full color display for 1 second
// Stage 3: Fade all segments to white to create the "melting" effect
for (int fadeLevel = 0; fadeLevel <= 255; fadeLevel += 1) {
for (int i = ledStart; i <= ledEnd; i++) {
leds[i] = blend(leds[i], CRGB::White, fadeLevel); // Blend each LED towards white
}
FastLED.show();
delay(50); // Adjust this for slower or faster fade
}
}
void shuffleSegments() {
// Simple Fisher-Yates shuffle to randomize segment order
for (int i = 7 - 1; i > 0; i--) {
int j = random(0, i + 1);
int temp = segmentOrder[i];
segmentOrder[i] = segmentOrder[j];
segmentOrder[j] = temp;
}
}
// Function to randomly fade in and fade out LEDs with a maximum of 2 active stars
void starEffect() {
activeStars = 0; // Reset active star count each time we start updating
for (int i = STAR_START; i <= STAR_END; i++) {
// Check if the LED is "on" (not fully faded out) and increase the active star count
if (leds[i].getAverageLight() > 0) {
activeStars++;
}
}
for (int i = STAR_START; i <= STAR_END; i++) {
// Only start a new star if there are fewer than 2 active stars
if (activeStars < 2 && random(0, 100) >= 99) {
leds[i] = CRGB::White; // Turn on the LED as a "star"
leds[i].fadeToBlackBy(random(5, 50)); // Random fade-out rate
activeStars++; // Increment active star count
} else {
leds[i].fadeToBlackBy(random(5, 50)); // Slowly fade out other LEDs
}
}
FastLED.show();
delay(50); // Adjust delay to control the speed of the effect
}
void taigaMomo(){
animateCouleurClarity(CRGB::White, false);
animateTai(CRGB::DeepPink);
delay(500);
animateGa(CRGB::DeepPink);
delay(500);
animateMo1(CRGB::DeepPink, false);
delay(500);
animateMo2(CRGB::DeepPink);
for (int i = 0; i <600; i++) {
if (modeChanged) { // Check if button interrupt set the flag
modeChanged = false;
mode = (mode + 1) % 8; // Cycle through modes
FastLED.clear();
return;
}
delay(100);
}
}
void animateMo1(CRGB color, bool flash) {
int ledOrder[] = {113, 114, 115, 116, 117, 118, 119, 120, -1, 124, 123, 122, 121, -1, 125, 126, 127, 128};
if (flash) {
// Flash all LEDs in ledOrder at once
for (int i = 0; i < sizeof(ledOrder) / sizeof(ledOrder[0]); i++) {
if (ledOrder[i] != -1) {
leds[ledOrder[i]] = color; // Flash with full brightness (or use any color)
}
}
FastLED.show();
delay(100); // Flash duration
// Fade out all LEDs at once
for (int i = 0; i <10; i++) {
for (int i = 0; i < sizeof(ledOrder) / sizeof(ledOrder[0]); i++) {
if (ledOrder[i] != -1) {
leds[ledOrder[i]].fadeToBlackBy(10); // Adjust fade speed
}
}
delay(50);
}
FastLED.show();
} else {
// Regular animation (lighting up each LED in sequence)
for (int i = 0; i < sizeof(ledOrder) / sizeof(ledOrder[0]); i++) {
if (ledOrder[i] == -1) {
delay(100);
continue;
}
leds[ledOrder[i]] = CRGB::Red; // Set the color to red initially
FastLED.show();
delay(50);
leds[ledOrder[i]] = color;
FastLED.show();
}
}
}
void animateMo2(CRGB color) {
int ledOrder[] = {101, 102, 103, 104, 105, 106, 107, 108, -1, 112, 111, 110, 109, -1, 131, 132, 133, 134 };
for (int i = 0; i < sizeof(ledOrder)/sizeof(ledOrder[0]); i++) {
if ( ledOrder[i] == -1 ){
delay(100);
continue;
}
leds[ledOrder[i]] = CRGB::Red; // Set the color to red
//leds[ledOrder[i]].fadeToBlackBy(200); // Adjust brightness
FastLED.show(); // Update the strip
delay(50); // Wait for a moment
leds[ledOrder[i]] = color;
//leds[ledOrder[i]] = CRGB::Black; // Turn off the LED after lighting it
}
}
void animateGa(CRGB color) {
int ledOrder[] = {79, -1, 77, -1, 74, 75, 76, -1, 80, 81, 82, 83, 84, -1, 96, 97, 98, -1, 95, 94, 93, 92, -1, 99, 100, -1, 85, 86, 87, 88, 89, 90, 91};
for (int i = 0; i < sizeof(ledOrder)/sizeof(ledOrder[0]); i++) {
if ( ledOrder[i] == -1 ){
delay(100);
continue;
}
leds[ledOrder[i]] = CRGB::Red; // Set the color to red
//leds[ledOrder[i]].fadeToBlackBy(200); // Adjust brightness
FastLED.show(); // Update the strip
delay(50); // Wait for a moment
leds[ledOrder[i]] = color;
//leds[ledOrder[i]] = CRGB::Black; // Turn off the LED after lighting it
}
FastLED.show();
}
void animateTai(CRGB color) {
int ledStart1 = 56;
int ledEnd1 = 62;
int ledStart2 = 62;
int ledEnd2 = 69;
int ledStart3 = 69;
int ledEnd3 = 74;
for (int i = ledStart3; i < ledEnd3; i++) {
leds[i] = CRGB::Red; // Set the color to red
FastLED.show(); // Update the strip
delay(50); // Wait for a moment
leds[i] = color;
//leds[i] = CRGB::Black; // Turn off the LED after lighting it
}
for (int i = ledEnd1; i > ledStart1; i--) {
leds[i] = CRGB::Red; // Set the color to red
FastLED.show(); // Update the strip
delay(50); // Wait for a moment
leds[i] = color;
//leds[i] = CRGB::Black; // Turn off the LED after lighting it
}
for (int i = ledStart2; i < ledEnd2; i++) {
leds[i] = CRGB::Red; // Set the color to red
FastLED.show(); // Update the strip
delay(50); // Wait for a moment
leds[i] = color;
//leds[i] = CRGB::Black; // Turn off the LED after lighting it
}
}
void animateCouleurClarity(CRGB color, bool animate) {
int ledStart = 6;
int ledEnd = 56;
if (animate) {
FastLED.clear();
}
for (int i = ledEnd; i > ledStart; i--) {
leds[i] = CRGB::White; // Set the color to red
if(animate){
FastLED.show(); // Update the strip
delay(50); // Wait for a moment
}
leds[i] = color;
//leds[i] = CRGB::Black; // Turn off the LED after lighting it
}
if(!animate){
FastLED.show(); // Update the strip
delay(50); // Wait for a moment
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment