Created
November 5, 2024 21:31
-
-
Save koalahamlet/683e95129da2ec41ec51c65463a88534 to your computer and use it in GitHub Desktop.
strobing LED problem
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> | |
#include <stdio.h> | |
#include <SPI.h> | |
#define LED_PIN_DATA 11 | |
#define LED_PIN_CLOCK 13 | |
#define NUM_LEDS 128 | |
#define BRIGHTNESS 155 | |
#define POT_PIN A0 | |
#define POT_PIN_COLOR A3 | |
#define LED_TYPE APA102 | |
#define COLOR_ORDER BGR | |
CRGB leds[NUM_LEDS]; | |
CRGB colorArray1[] = {CRGB::Red, CRGB::Blue, CRGB::Red, CRGB::Black}; // sanity | |
CRGB colorArray2[] = {CRGB::Green, CRGB::Blue, CRGB::Green, CRGB::Black}; | |
CRGB colorArray3[] = {CRGB::Green, CRGB::Red, CRGB::Blue, CRGB::Black}; | |
CRGB colorArray4[] = { | |
CRGB(173, 216, 230), // Light blue | |
CRGB(255, 255, 0), // Yellow | |
CRGB(0, 255, 216), | |
CRGB::Black // Off/black | |
}; | |
CRGB* colorArrays[] = {colorArray1, colorArray2, colorArray3, colorArray4}; | |
int prevColorIndex = 0; | |
int prevBaseInterval = 0; | |
void setup() { | |
delay(1000); | |
FastLED.addLeds<LED_TYPE, LED_PIN_DATA, LED_PIN_CLOCK, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(UncorrectedColor); | |
FastLED.setBrightness(BRIGHTNESS); | |
FastLED.setDither(false); | |
Serial.begin(9600); // Initialize serial communication for debugging | |
} | |
void loop() { | |
pulseColors(); | |
} | |
void pulseColors() { | |
static unsigned long lastChange = 0; | |
static int colorIndex = 0; | |
unsigned long now = millis(); | |
// read the values | |
int potRefreshSpeedValue = analogRead(POT_PIN); | |
int potColorValue = analogRead(POT_PIN_COLOR); | |
// map the values. Base interval should be between 1 and 6 milliseconds | |
int baseInterval = map(potRefreshSpeedValue, 0, 1023, 1, 6); | |
int colorArrayIndex = map(potColorValue, 0, 1023, 0, 4); | |
int interval = baseInterval; | |
CRGB* currentColors = colorArrays[colorArrayIndex]; | |
// if the color is "black" show it 3 times as long! | |
if (currentColors[colorIndex] == CRGB::Black) { | |
interval = baseInterval * 3; // Black color takes longer, e.g., 3 times the base interval | |
} | |
if (now - lastChange >= interval) { | |
lastChange = now; | |
colorIndex = (colorIndex + 1) % 4; // Cycle through colors | |
for (int i = 0; i < NUM_LEDS; i++) { | |
leds[i] = currentColors[colorIndex]; | |
} | |
FastLED.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment