#include <Keypad.h>
#include <FastLED.h>
#define NUM_LEDS 9
#define DATA_PIN 3
CRGB leds[NUM_LEDS];
const byte ROWS = 3; // three rows
const byte COLS = 3; // three columns
// Define the symbols on the buttons of the keypad
char hexaKeys[ROWS][COLS] = {
{'0', '1', '2'},
{'3', '4', '5'},
{'6', '7', '8'}
};
byte rowPins[ROWS] = {12, 11, 10}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {A0, A1, A2}; // connect to the column pinouts of the keypad
// Initialize an instance of class NewKeypad
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// Mapping from keypad position to LED position
int ledMapping[NUM_LEDS] = {2, 1, 0, 5, 4, 3, 8, 7, 6};
// Define the number of available effects
#define NUM_EFFECTS 7
int currentEffect = 0; // Current effect mode
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.clear();
FastLED.show();
}
void loop() {
char customKey = customKeypad.getKey();
if (customKey) {
Serial.print("Key pressed: ");
Serial.println(customKey);
int keyIndex = customKey - '0'; // Convert character to corresponding key index
if (keyIndex >= 0 && keyIndex < NUM_LEDS) {
int ledIndex = ledMapping[keyIndex]; // Map key index to LED index
switch (customKey) {
case '8':
currentEffect = (currentEffect + 1) % NUM_EFFECTS; // Increment effect mode
break;
default:
executeEffect(ledIndex, customKey);
break;
}
}
}
}
void executeEffect(int ledIndex, char customKey) {
switch (currentEffect) {
case 0:
blinkingEffect(ledIndex, CRGB::Green, 500); // Green blinking effect
break;
case 1:
colorCyclingEffect(50); // Color cycling effect
break;
case 2:
underglowEffect(CRGB::Blue, 500); // Blue underglow effect
break;
case 3:
strobeEffect(500); // Strobe effect
break;
case 4:
runningEffect(CRGB::Yellow, 500); // Yellow running effect
break;
case 5:
morphingEffect(500); // Morphing effect
break;
case 6:
movingLightEffect(500); // Morphing effect
break;
case 7:
rainbowEffect(500); // Morphing effect
break;
default:
break;
}
}
void blinkingEffect(int ledIndex, CRGB color, int duration) {
leds[ledIndex] = color;
FastLED.show();
delay(duration);
leds[ledIndex] = CRGB::Black;
FastLED.show();
}
void colorCyclingEffect(int duration) {
unsigned long startTime = millis(); // Lưu thời gian bắt đầu
int hue = 0;
while (millis() - startTime < duration) {
fill_rainbow(leds, NUM_LEDS, hue, 8);
FastLED.show();
hue++; // Tăng giá trị hue
delay(50); // Điều chỉnh tốc độ chuyển đổi màu
}
}
void underglowEffect(CRGB color, int duration) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = color;
FastLED.show();
delay(duration / NUM_LEDS);
}
}
void strobeEffect(int duration) {
for (int i = 0; i < 5; i++) {
fill_solid(leds, NUM_LEDS, CRGB::White);
FastLED.show();
delay(duration / 10);
FastLED.clear();
FastLED.show();
delay(duration / 10);
}
}
void runningEffect(CRGB color, int duration) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = color;
FastLED.show();
delay(duration / NUM_LEDS);
leds[i] = CRGB::Black;
}
}
void morphingEffect(int duration) {
CRGB startColor = CRGB::Red;
CRGB endColor = CRGB::Blue;
for (int i = 0; i <= 255; i++) {
CRGB color = blend(startColor, endColor, i);
fill_solid(leds, NUM_LEDS, color);
FastLED.show();
delay(duration / 255);
}
}
void movingLightEffect(int duration) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Black;
FastLED.show();
delay(duration / NUM_LEDS);
}
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::White;
FastLED.show();
delay(duration / NUM_LEDS);
leds[i] = CRGB::Black;
}
}
void rainbowEffect(int duration) {
uint8_t hue = 0;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue, 255, 255);
hue += 256 / NUM_LEDS;
}
FastLED.show();
delay(duration);
// Rotate the colors
for (int j = 0; j < 256; j++) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(hue + j, 255, 255);
}
FastLED.show();
delay(duration / 256);
}
}