#include <Arduino.h>
#include <FastLED.h>
#define LED_PIN 3 // LED Strip Signal Connection
#define COLOR_UP_BUTTON_PIN 5 // Push Button for changing colors forward
#define RESET_COLOR_BUTTON_PIN 6 // Push Button for resetting color to initial
#define NUM_LEDS 24 // Total number of LEDs in the LED strip. You can safely go up to 80 LEDs.
//#define led Brightness 100
CRGB leds[NUM_LEDS];
int r, g, b;
int numb = 1;
int pres = 0;
int SPEED_KITT1 = 25; //toc do dem led
void setup() {
Serial.begin(9600);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.clear();
pinMode(COLOR_UP_BUTTON_PIN, INPUT_PULLUP);
pinMode(RESET_COLOR_BUTTON_PIN, INPUT_PULLUP);
// Sáng hết LED trắng
for (int i = 0; i < (NUM_LEDS / 2); i++) {
leds[(NUM_LEDS / 2) + i] = CRGB::White;
leds[((NUM_LEDS / 2) - 1) - i] = CRGB::White;
FastLED.show();
ButtonScan();
delay(100);
}
// Sáng LED đỏ từ LED đầu tiên và LED cuối cùng đến giữa
for (int i = 0; i < (NUM_LEDS / 2); i++) {
leds[i] = CRGB::Red; // Sáng từ LED đầu tiên đến giữa
leds[NUM_LEDS - 1 - i] = CRGB::Red; // Sáng từ LED cuối cùng đến giữa
FastLED.show();
ButtonScan();
delay(100);
}
}
void loop() {
ButtonScan();
switch (numb) {
case 1:
Pattern1(1);
break;
case 2:
Pattern1(2);
break;
case 3:
Pattern1(3);
break;
case 4:
Pattern1(4);
break;
}
}
void Pattern1(int ColourTrig) {
switch (ColourTrig) {
case 1:
r = 200;
g = b = 0;
break;
case 2:
r = g = b = 255;
break;
case 3:
b = g = 255;
r = 0;
break;
case 4:
r = g = b = 0;
break;
}
fadeToBlackBy(leds, NUM_LEDS, 1); //SO cang to duoi cang ngan
uint8_t u = beat8(SPEED_KITT1, 0); // toc do dem led
uint8_t pos1 = map(u, 255, 0, 0, NUM_LEDS);
leds[pos1] = CRGB(r, g, b);
uint8_t pos2 = map(u, 0, 255, 0, NUM_LEDS);
leds[pos2] = CRGB(r, g, b);
FastLED.show();
ButtonScan();
delay(30);
}
void ButtonScan() {
if (digitalRead(COLOR_UP_BUTTON_PIN) == 0) {
if (pres == 0) {
numb++;
pres = 1;
Serial.println("color");
}
} else if (digitalRead(RESET_COLOR_BUTTON_PIN) == 0) {
if (pres == 0) {
numb = 1; // Reset to the initial color
pres = 1;
Serial.println("reset");
}
} else {
pres = 0;
}
if (numb == 22) {
numb = 1;
}
}