#include <FastLED.h>
#include "Arduino.h"
#include <SoftwareSerial.h>
//#include "DFRobotDFPlayerMini.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define NUM_LEDS 39
#define DATA_PIN 13
#define BUTTON_PIN 2
const byte rxPin = 9;
const byte txPin = 8;
SoftwareSerial mySerial(rxPin, txPin);
//DFRobotDFPlayerMini myDFPlayer;
Adafruit_SSD1306 display(128, 32, &Wire, -1);
const unsigned char logo32[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x80, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x03, 0xf0, 0x00, 0x00, 0x01, 0xf8, 0x00,
0x00, 0x20, 0xfc, 0x00, 0x00, 0x30, 0x7c, 0x00, 0x00, 0x78, 0x3c, 0x00, 0x00, 0x3c, 0x1c, 0x00,
0x00, 0x3e, 0x0c, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0x07, 0xc0, 0x00,
0x00, 0x03, 0xe0, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x20, 0x7c, 0x00,
0x00, 0x30, 0x3c, 0x00, 0x00, 0x78, 0x1c, 0x00, 0x00, 0x3c, 0x0c, 0x00, 0x00, 0x3e, 0x04, 0x00,
0x00, 0x1f, 0x00, 0x00, 0x00, 0x0f, 0x80, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x01, 0x80, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const int logoallArray_LEN = 1;
const unsigned char* logoallArray[1] = {
logo32
};
CRGB leds[NUM_LEDS];
uint8_t waveSpeed = 5; //Tốc độ của hiệu ứng sóng màu
uint8_t colorHue = 25; //Màu sắc (0-255) (Màu cam)
uint8_t density = 255; //(0-255)
CRGB on_color;
unsigned long previousMillis = 0; // Lưu thời gian kể từ khi cập nhật cuối cùng
int buttonState = 0;
int police_on;
void setup() {
mySerial.begin(9600);
//myDFPlayer.begin(mySerial);
//myDFPlayer.volume(30); //Volume 0-30
pinMode(BUTTON_PIN, INPUT_PULLUP);
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(255);
//myDFPlayer.play(3);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(100);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setCursor(8, 14);
display.setTextSize(1);
display.println("Protogen OS");
display.display();
delay(1000);
display.clearDisplay();
display.drawBitmap(-8, 0, logo32, 32, 32, 1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(24, 14);
display.setTextSize(1);
display.println("SOLARIS Darksaber");
display.display();
delay(500);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= waveSpeed) {
previousMillis = currentMillis;
static unsigned long buttonTimer = 0;
static bool buttonActive = false;
static bool programRunning = false;
// Kiểm tra nút nhấn và xử lý debounce
if (digitalRead(BUTTON_PIN) == LOW) {
//display.clearDisplay();
//display.setCursor(8, 14);
//display.setTextSize(1);
//display.println("<><><><><><><><><><>");
//display.display();
//display.startscrollleft(0x00, 0x0F);
if (buttonTimer == 0) {
buttonTimer = millis();
} else if ((millis() - buttonTimer) > 500) { // Nếu nút được giữ trong 500ms
display.stopscroll();
buttonState++;
buttonTimer = 0;
buttonActive = true;
}
} else {
buttonTimer = 0;
}
if (buttonActive) {
switch (buttonState) {
case 1:
on_color = CRGB(255, 107, 0); //Cam
colorHue = 25;
On(); // Chạy chương trình "On"
programRunning = true;
break;
case 2:
Off(); // Chạy chương trình "Off"
programRunning = false;
break;
//--------------------------------
case 3:
on_color = CRGB(255, 0, 0); //Đỏ
colorHue = 0;
On(); // Chạy chương trình "On"
police_on = 1;
/*while (police_on = 1) {
Police();
}*/
programRunning = true;
break;
case 4:
Off(); // Chạy chương trình "Off"
programRunning = false;
break;
//--------------------------------
case 5:
on_color = CRGB(255, 255, 0); //Vàng
colorHue = 60;
On(); // Chạy chương trình "On"
programRunning = true;
break;
case 6:
Off(); // Chạy chương trình "Off"
programRunning = false;
break;
//--------------------------------
case 7:
on_color = CRGB(0, 255, 0); //Xanh lá
colorHue = HUE_GREEN;
On(); // Chạy chương trình "On"
programRunning = true;
break;
case 8:
Off(); // Chạy chương trình "Off"
programRunning = false;
break;
//--------------------------------
case 9:
on_color = CRGB(0, 255, 184); //Aqua
colorHue = HUE_AQUA;
On(); // Chạy chương trình "On"
programRunning = true;
break;
case 10:
Off(); // Chạy chương trình "Off"
programRunning = false;
break;
//--------------------------------
case 11:
on_color = CRGB(255, 0, 150); //Hồng
colorHue = HUE_PINK;
On(); // Chạy chương trình "On"
programRunning = true;
break;
case 12:
Off(); // Chạy chương trình "Off"
programRunning = false;
break;
//--------------------------------
case 13:
on_color = CRGB(255, 107, 0); //Cam
colorHue = 25;
On(); // Chạy chương trình "On" như lần đầu tiên
buttonState = 1; // Đặt lại trạng thái nút nhấn
programRunning = true;
break;
}
buttonActive = false;
}
if (programRunning) {
Colorwaves(); // Chạy "Colorwaves" nếu chương trình đang hoạt động
}
}
}
void Colorwaves() {
uint16_t duration = 20;
static uint16_t sPseudotime = 0;
static uint16_t sHue16 = 0;
uint8_t brightdepth = beatsin88(341, 96, 224);
//uint16_t brightnessthetainc16 = beatsin88(203, (25 * 256), (40 * 256));
uint16_t brightnessthetainc16 = 6000; //Độ thưa
uint8_t msmultiplier = beatsin88(147, 23, 60);
uint16_t hue16 = sHue16;
uint16_t hueinc16 = beatsin88(113, 60, 300) * 10;
sPseudotime += duration * msmultiplier;
sHue16 += duration * beatsin88(400, 5, 9);
uint16_t brightnesstheta16 = sPseudotime;
for (int i = NUM_LEDS - 1; i >= 0; i--) {
hue16 += hueinc16;
uint8_t hue8 = hue16 / 256;
brightnesstheta16 += brightnessthetainc16;
uint16_t b16 = sin16(brightnesstheta16) + 32768;
uint16_t bri16 = ((uint32_t)b16 * (uint32_t)b16) / 65536;
uint8_t bri8 = (((uint32_t)bri16) * brightdepth) / 65536;
bri8 += (255 - brightdepth);
// Áp dụng màu sắc và độ thưa
leds[i] = CHSV(colorHue, 255, scale8(bri8, density));
}
FastLED.show();
}
void Police() {
for (int y = 0; y <= 2; y++) {
for (int i = 0; i <= 19; i++) {
leds[i] = CRGB(255, 0, 0); //Đỏ
}
for (int i = 20; i <= 39; i++) {
leds[i] = CRGB(0, 0, 0); //Đen
}
FastLED.show();
delay(20);
for (int b = 0; b <= 39; b++) {
leds[b] = CRGB(0, 0, 0); //Tắt
}
FastLED.show();
}
for (int y = 0; y <= 2; y++) {
for (int i = 0; i <= 19; i++) {
leds[i] = CRGB(0, 0, 0); //Đen
}
for (int i = 20; i <= 39; i++) {
leds[i] = CRGB(0, 0, 255); //Xanh dương
}
FastLED.show();
delay(20);
for (int b = 0; b <= 39; b++) {
leds[b] = CRGB(0, 0, 0); //Tắt
}
FastLED.show();
}
}
void On() {
//myDFPlayer.play(1);
delay(500);
for (int i = 0; i < NUM_LEDS; i++) { //On effect when press button first time
leds[i] = on_color;
FastLED.show();
delay(30); // Adjust for desired speed
}
}
void Off() {
//myDFPlayer.play(2);
for (int i = NUM_LEDS - 1; i >= 0; i--) { //Off effect when press button second time
leds[i] = CRGB::Black;
FastLED.show();
delay(30); // Adjust for desired speed
}
}