Created
November 15, 2018 04:50
-
-
Save soswow/0c6fea50248d6a49d03690d202f94b8f to your computer and use it in GitHub Desktop.
LED Cap Project Arduino Project code
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 <Bounce2.h> | |
#include <FastLED.h> | |
#define BRIGHTNESS_PIN A6 | |
#define NUM_LEDS 15 | |
#define DATA_PIN 7 | |
#define BUTTON_PIN 8 | |
#define COLUMNS 6 | |
#define ROWS 6 | |
int stripIndexToGrid[] = {28, 34, 35, 29, 22, 15, 20, 26, 31, 30, 25, 19, 14, 8, 3}; | |
int gridIndexToStrip[COLUMNS * ROWS]; | |
// = { | |
// -1, -1, -1, 14, -1, -1, | |
// -1, -1, 13, -1, -1, -1, | |
// -1, -1, 12, 5, -1, -1, | |
// -1, 11, 6, -1, 4, -1, | |
// -1, 10, 7, -1, 0, 3, | |
// 9, 8, -1, -1, 1, 2 | |
// } | |
CRGBArray<NUM_LEDS> leds; | |
Bounce debouncer = Bounce(); | |
byte MAX_MODE = 5; | |
byte mode = 1; | |
int prevButtonValue; | |
int * getRandomLedsIndecies() { | |
static int indecies[NUM_LEDS] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14}; | |
// for(int k = 0; k < NUM_LEDS; k++) { | |
// indecies[k] = k; | |
// } | |
for(int i = NUM_LEDS - 1; i > 0; i--){ | |
int j = random(0, NUM_LEDS); | |
int aj = indecies[j]; | |
int ai = indecies[i]; | |
indecies[j] = ai; | |
indecies[i] = aj; | |
} | |
return indecies; | |
} | |
int *randomIndicies; | |
void setup() { | |
Serial.begin(9600); | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for native USB port only | |
} | |
randomIndicies = getRandomLedsIndecies(); | |
initGridIndexToStrip(); | |
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS); | |
debouncer.attach(BUTTON_PIN); | |
debouncer.interval(5); // interval in ms | |
pinMode(BUTTON_PIN, INPUT_PULLUP); | |
prevButtonValue = debouncer.read(); | |
} | |
void initGridIndexToStrip() { | |
for (int i = 0; i < COLUMNS * ROWS; i++) { | |
gridIndexToStrip[i] = -1; | |
} | |
for (int i = 0; i < NUM_LEDS; i++) { | |
gridIndexToStrip[stripIndexToGrid[i]] = i; | |
} | |
} | |
long interval = 90; | |
long previousMillis = 0; | |
int MAX_SENSOR_VALUE = 800; | |
// fillWithLines variables | |
bool fillingIn = true; // As oppose to emptying out | |
int rowsFilled = 0; | |
int currentRunningRow = 0; | |
void loop() { | |
int sensorValue = analogRead(BRIGHTNESS_PIN); | |
if (sensorValue > MAX_SENSOR_VALUE) { | |
MAX_SENSOR_VALUE = sensorValue; | |
} | |
int brightness = map(sensorValue, 0, MAX_SENSOR_VALUE, 0, 255); | |
LEDS.setBrightness(brightness); | |
// Uncomment following lines if you want to see potentiometer values in Serial Plotter | |
// Serial.print(sensorValue); | |
// Serial.print(" "); | |
// Serial.print(brightness); | |
// Serial.print(" "); | |
// Serial.println(MAX_SENSOR_VALUE); | |
debouncer.update(); | |
int buttonValue = debouncer.read(); | |
if (prevButtonValue == LOW && buttonValue == HIGH) { | |
fillingIn = true; | |
rowsFilled = 0; | |
currentRunningRow = 0; | |
mode += 1; | |
if (mode >= MAX_MODE) { | |
mode = 0; | |
} | |
} | |
prevButtonValue = buttonValue; | |
unsigned long currentMillis = millis(); | |
// Using non-blocking delay for faster button feedback. | |
if (currentMillis - previousMillis >= interval) { | |
previousMillis = currentMillis; | |
if (mode == 0) { | |
interval = 90; | |
scrollUpAndDown(CRGB::Green); | |
} else if (mode == 1) { | |
interval = 90; | |
scrollUpAndDown(CRGB::Red); | |
} else if (mode == 2) { | |
lgbtColors(); | |
} else if (mode == 3) { | |
interval = 60; | |
sparkling(); | |
} else if (mode == 4) { | |
interval = 120; | |
fillWithLines(CRGB::Blue); | |
} | |
FastLED.show(); | |
} | |
} | |
void fillWithLines(CRGB color) { | |
for (int y = 0; y < ROWS; y++) { | |
for (int x = 0; x < COLUMNS; x++) { | |
int stripIndex = gridIndexToStrip[y * COLUMNS + x]; | |
if (stripIndex > -1) { | |
if ((fillingIn && ((y >= ROWS - rowsFilled) || y == currentRunningRow)) || | |
(!fillingIn && ((y < rowsFilled) || y == currentRunningRow))) { | |
leds[stripIndex] = CRGB::Blue; | |
} else { | |
leds[stripIndex] = CRGB::Black; | |
} | |
} | |
} | |
} | |
if (fillingIn) { | |
currentRunningRow++; | |
if(currentRunningRow >= ROWS - rowsFilled){ | |
currentRunningRow = 0; | |
rowsFilled++; | |
} | |
if(rowsFilled > ROWS){ | |
fillingIn = false; | |
rowsFilled = ROWS; | |
currentRunningRow = ROWS; | |
} | |
} else { | |
currentRunningRow++; | |
if (currentRunningRow > ROWS) { | |
currentRunningRow = rowsFilled; | |
rowsFilled--; | |
} | |
if (rowsFilled < 0) { | |
rowsFilled = 0; | |
fillingIn = true; | |
} | |
} | |
} | |
int nextRandomIndex = 0; | |
int spraklingCiclesCounter = 0; | |
int sparksAtATime = 2; | |
void sparkling() { | |
if(spraklingCiclesCounter % 2 == 0) { | |
for(int i=0;i<sparksAtATime;i++){ | |
leds[randomIndicies[nextRandomIndex]] = CRGB::White; | |
nextRandomIndex++; | |
if(nextRandomIndex >= NUM_LEDS){ | |
nextRandomIndex = 0; | |
randomIndicies = getRandomLedsIndecies(); | |
} | |
} | |
} | |
spraklingCiclesCounter++; | |
leds.fadeToBlackBy(128); // 25% | |
} | |
bool movingDown = true; | |
int row = 0; | |
void scrollUpAndDown(CRGB color) { | |
leds.fadeToBlackBy(128); // 25% | |
for (int x = 0; x < COLUMNS; x++) { | |
int stripIndex = gridIndexToStrip[row * COLUMNS + x]; | |
if (stripIndex > -1) { | |
leds[stripIndex] = color; | |
} | |
} | |
if (movingDown) { | |
row++; | |
if (row == ROWS - 1) { | |
movingDown = false; | |
} | |
} else { | |
row--; | |
if (row == 0) { | |
movingDown = true; | |
} | |
} | |
} | |
// int stripIndexToGrid[] = {28, 34, 35, 29, 22, 15, 20, 26, 31, 30, 25, 19, 14, 8, 3}; | |
int stripIndexPerRow[6][4] = { | |
{14, -1, -1, -1}, | |
{13, -1, -1, -1}, | |
{12, 5, -1, -1}, | |
{11, 6, 4, -1}, | |
{10, 7, 0, 3}, | |
{9, 8, 1, 2}, | |
}; | |
int countPerRow[6] = {1,1,2,3,4,4}; | |
int lgbtFilledIndeciesCounter = 0; | |
int lgbtFilledIndecies[NUM_LEDS]; | |
int lgbtRunningFallingPath = 0; | |
int indexInRunningFallingPath = 0; | |
int framesPause = 0; | |
int lgbtCurrentFallingPaths[NUM_LEDS][6] = { | |
// 6th Row | |
{14, 13, 5, 4, 3, 2}, | |
{14, 13, 12, 11, 7, 8}, | |
{14, 13, 12, 6, 10, 9}, | |
{14, 13, 5, 4, 0, 1}, | |
// 5th Row | |
{14, 13, 5, 4, 3, -1}, | |
{14, 13, 12, 6, 7, -1}, | |
{14, 13, 5, 4, 0, -1}, | |
{14, 13, 12, 6, 10, -1}, | |
// 4th Row | |
{14, 13, 12, 6, -1, -1}, | |
{14, 13, 5, 4, -1, -1}, | |
{14, 13, 5, 11, -1, -1}, | |
// 3th Row | |
{14, 13, 12, -1, -1, -1}, | |
{14, 13, 5, -1, -1, -1}, | |
// 2th Row | |
{14, 13, -1, -1, -1, -1}, | |
// 1th Row | |
{14, -1, -1, -1, -1, -1}, | |
}; | |
void resetLgbtState() { | |
lgbtFilledIndeciesCounter = 0; | |
lgbtRunningFallingPath = 0; | |
indexInRunningFallingPath = 0; | |
} | |
void lgbtColors() { | |
if (framesPause > 0) { | |
framesPause--; | |
if(framesPause == 0){ | |
resetLgbtState(); | |
} | |
return; | |
} | |
CRGB colorsByGrid[] = { | |
CRGB::Magenta, CRGB::Magenta, CRGB::Magenta, CRGB::Magenta, | |
CRGB::Blue, CRGB::Blue, CRGB::Blue, CRGB::Blue, | |
CRGB::Green, CRGB::Green, CRGB::Green, | |
CRGB::Yellow, CRGB::Yellow, | |
CRGB::Orange, | |
CRGB::Red | |
}; | |
CRGB rowColors[] = { | |
CRGB::Red, | |
CRGB::Orange, | |
CRGB::Yellow, | |
CRGB::Green, | |
CRGB::Blue, | |
CRGB::Magenta | |
}; | |
fill_solid(leds, NUM_LEDS, CRGB::Black); | |
for(int i=0; i < lgbtFilledIndeciesCounter; i++){ | |
leds[lgbtFilledIndecies[i]] = colorsByGrid[i]; | |
} | |
int runnerIndex = lgbtCurrentFallingPaths[lgbtRunningFallingPath][indexInRunningFallingPath]; | |
leds[runnerIndex] = CRGB::White; //rowColors[indexInRunningFallingPath]; | |
indexInRunningFallingPath++; | |
bool isEndOfPath = indexInRunningFallingPath >= 6 || lgbtCurrentFallingPaths[lgbtRunningFallingPath][indexInRunningFallingPath] == -1; | |
if (isEndOfPath) { | |
lgbtRunningFallingPath++; | |
indexInRunningFallingPath = 0; | |
lgbtFilledIndecies[lgbtFilledIndeciesCounter] = runnerIndex; | |
lgbtFilledIndeciesCounter++; | |
} | |
if(lgbtFilledIndeciesCounter > NUM_LEDS-1){ | |
framesPause = 30; | |
leds[lgbtCurrentFallingPaths[5][0]] = CRGB::Red; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment