// https://forum.arduino.cc/t/looping-leds-until/1411057/
#include <Keypad.h>
#include <FastLED.h>
// int last_pressed = 0; // not used. "catched_button" is available
const byte rows = 4; // compiler warning. changed char to byte
const byte columns = 4; // compiler warning. changed char to byte
byte pinrows[rows] = {4, 5, 6, 7}; // compiler warning. changed char to byte
byte pincolumns[columns] = {8, 9, 10, 11}; // compiler warning. changed char to byte
char button[rows][columns] = { // this keypad reqires R1..R4 = 11..8, C1..C4 = 7..4
{'D', 'C', 'B', 'A'},
{'#', '9', '6', '3'},
{'0', '8', '5', '2'},
{'*', '7', '4', '1'}
};
Keypad k = Keypad(makeKeymap(button), pinrows, pincolumns, rows, columns);
#define LED_PIN 3
#define NUM_LEDS 115
#define BRIGHTNESS 64
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define UPDATES_PER_SECOND 100
CRGBPalette16 currentPalette;
TBlendType currentBlending;
unsigned long timer, timeout = 1000UL / UPDATES_PER_SECOND;
int i;
void setup() {
delay( 3000 ); // power-up safety delay
Serial.begin(9600);
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness(BRIGHTNESS );
currentPalette = RainbowColors_p;
currentBlending = LINEARBLEND;
}
void loop()
{
char catched_button = k.getKey();
if (catched_button) {
Serial.print("Performed function: ");
Serial.println(catched_button);
}
switch (catched_button) {
case '1': fun1(); break;
case '2': fun2(); break;
case '3': fun3(); break;
default: fun0(); break;
}
}
void fun1() {
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
uint8_t colorIndex;
for ( int i = NUM_LEDS; i > -1; --i) {
leds[i] = ColorFromPalette( currentPalette, colorIndex, 255, currentBlending);
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
colorIndex += 3;
}
}
void fun2() {
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
uint8_t colorIndex;
for ( int i = 0; i < NUM_LEDS; ++i) {
leds[i] = ColorFromPalette( currentPalette, colorIndex, 255, currentBlending);
FastLED.show();
FastLED.delay(1000 / UPDATES_PER_SECOND);
colorIndex += 3;
}
}
void fun3() {
for (uint8_t i = 0; i < 20; i++) {
for (uint8_t j = 0; j < NUM_LEDS; j++) {
leds[j] = CRGB(0, 0, BRIGHTNESS);
}
FastLED.show();
delay(UPDATES_PER_SECOND);
for (uint8_t j = 0; j < NUM_LEDS; j++) {
leds[j] = CRGB(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS);
}
FastLED.show();
delay(UPDATES_PER_SECOND);
}
}
void fun0() {
if (millis() - timer > timeout) { // wait between leds
timer = millis(); // reset time before next led
// a random led (0 to NUM_LEDS) will be colored (random red, random green, random blue)
leds[random(NUM_LEDS)] = CRGB(random(256) * random(2), random(256) * random(2), random(256) * random(2));
FastLED.show();
}
}