#include "define.h"
#include "light.h"
#include "saveMode.h"
#include "teris.h"
#include "mario.h"
#include "happyBirthday.h"
#include "HarryPotter.h"
#include "MerryChristmas.h"
#include "internetRelated.h"
void setup() {
Serial.begin(9600);
Serial.print("Connecting to WiFi");
wifiConnect();
mqttClient.setServer(mqttServer, port);
mqttClient.setCallback(callback);
mqttClient.setKeepAlive( 90 );
strip_1.begin();
strip_1.show();
strip_2.begin();
strip_2.show();
strip_3.begin();
strip_3.show();
strip_4.begin();
strip_4.show();
pinMode(BUTTONLIGHT, INPUT);
pinMode(BUTTONMUSIC, INPUT);
pinMode(BUTTONSAVEMODE_ON, INPUT);
pinMode(BUTTONSAVEMODE_OFF, INPUT);
pinMode(BUZZER, OUTPUT);
pinMode(LDR_PIN, INPUT);
pinMode(PIR_PIN, INPUT);
countButtonLight = esp_random() % 6;
countButtonMusic = esp_random() % 5;
// Phát bài nhạc ngẫu nhiên
if (countButtonMusic % 5 == 0) {
curMelody = melody_tetris;
curTempo = tempo_tetris;
curNotes = notes_tetris;
}
else if (countButtonMusic % 5 == 1) {
curMelody = melody_mario;
curTempo = tempo_mario;
curNotes = notes_mario;
}
else if (countButtonMusic % 5 == 2) {
curMelody = melody_happybd;
curTempo = tempo_happybd;
curNotes = notes_happybd;
}
else if (countButtonMusic % 5 == 3) {
curMelody = melody_HarryPotter;
curTempo = tempo_HarryPotter;
curNotes = notes_HarryPotter;
}
else if (countButtonMusic % 5 == 4) {
curMelody = melody_MCM;
curTempo = tempo_MCM;
curNotes = notes_MCM;
}
isPlayingMusic = true;
curNote = 0;
notePlaying = false;
// Hiển thị nhạc và mode đèn đầu tiên lên web
String pubLed = String(countButtonLight);
String pubSong = String(countButtonMusic);
mqttConnect();
mqttClient.publish("LightModePRJ", pubLed.c_str());
mqttClient.publish("MusicModePRJ", pubSong.c_str());
color_wipe(strip_1.Color(255, 255, 255), 20); //hiệu ứng khởi động
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Reconnecting to WiFi");
wifiConnect();
}
if(!mqttClient.connected()) {
mqttConnect();
}
mqttClient.loop();
int lightButt = digitalRead(BUTTONLIGHT);
int musicButt = digitalRead(BUTTONMUSIC);
int saveButton = digitalRead(BUTTONSAVEMODE_ON);
int saveButtoff = digitalRead(BUTTONSAVEMODE_OFF);
int haveMove = digitalRead(PIR_PIN);
int brightness = analogRead(LDR_PIN);
//ThingSpeak chặn spam <= 15s
//Thời gian hoạt động -> Web
if (millis() - timeLine_Activated >= 60000) { // cứ 60s truyền thời gian lên cloud
String data = String(0.0167);
if (mqttClient.connected())
mqttClient.publish("TimeActivePRJ", data.c_str());
timeLine_Activated = millis();
}
// Cường độ sáng -> Web
if (millis() - timeLine_ADC >= 45000) { // cứ 45s truyền cường độ lên cloud
String lightLEVEL = String(brightness);
if (mqttClient.connected())
mqttClient.publish("lightLEVEL", lightLEVEL.c_str());
timeLine_ADC = millis();
}
// => Va chạm 20 lần 1 giờ
if (lightButt == HIGH) { // Nút -> Đèn
countButtonLight++;
Serial.println("Light button pressed!");
// Mode đèn -> Web
String msg = String(countButtonLight % 6);
if (mqttClient.connected())
mqttClient.publish("LightModePRJ", msg.c_str());
delay(150);
}
if (musicButt == HIGH) { // Nút -> Nhạc
countButtonMusic++;
Serial.println("Music button pressed!");
//Bài nhạc -> Web
String msg = String(countButtonMusic % 5);
if (mqttClient.connected())
mqttClient.publish("MusicModePRJ", msg.c_str());
// 5 bài nhạc
if (isPlayingMusic) { // Dừng nhạc đang chơi nếu có
noTone(BUZZER);
isPlayingMusic = false;
}
if (countButtonMusic % 5 == 0) {
curMelody = melody_tetris;
curTempo = tempo_tetris;
curNotes = notes_tetris;
}
else if (countButtonMusic % 5 == 1) {
curMelody = melody_mario;
curTempo = tempo_mario;
curNotes = notes_mario;
}
else if (countButtonMusic % 5 == 2) {
curMelody = melody_happybd;
curTempo = tempo_happybd;
curNotes = notes_happybd;
}
else if (countButtonMusic % 5 == 3) {
curMelody = melody_HarryPotter;
curTempo = tempo_HarryPotter;
curNotes = notes_HarryPotter;
}
else if (countButtonMusic % 5 == 4) {
curMelody = melody_MCM;
curTempo = tempo_MCM;
curNotes = notes_MCM;
}
isPlayingMusic = true;
curNote = 0;
notePlaying = false;
delay(150);
}
if (saveButton == HIGH) { // Nút -> Bật savemode
startTrack = true;
// Bật savemode -> Web
if (mqttClient.connected())
mqttClient.publish("SaveModeON", "1");
Serial.println("Button save mode on pressed: Save mode start track");
delay(200);
}
if (saveButtoff == HIGH) { // Nút -> Tắt savemode
startTrack = false;
// Bật savemode -> Web
if (mqttClient.connected())
mqttClient.publish("SaveModeOFF", "0");
if (powerSaveMode) {
saveModeOff();
// Trạng thái của save mode -> Web
String saveModeState = String(powerSaveMode);
if (mqttClient.connected())
mqttClient.publish("saveModeState", saveModeState.c_str());
}
Serial.println("Button save mode off pressed: Stop save mode");
delay(200);
}
if(startTrack) { // Bắt đầu theo xem xét có kích hoạt savemode chưa
if(!powerSaveMode && haveMove == LOW && brightness > WEAKBRIGHT) { // bật tiết kiệm
saveModeOn();
// Trạng thái của save mode -> Web
String saveModeState = String(powerSaveMode);
if (mqttClient.connected())
mqttClient.publish("saveModeState", saveModeState.c_str());
}
if (powerSaveMode && (haveMove == HIGH || brightness <= WEAKBRIGHT)) { // thoát tiết kiệm
saveModeOff();
// Trạng thái của save mode -> Web
String saveModeState = String(powerSaveMode);
if (mqttClient.connected())
mqttClient.publish("saveModeState", saveModeState.c_str());
}
}
if(powerSaveMode) {
return;
}
// Phát nhạc
if (isPlayingMusic) {
int wholenote = (60000 * 4) / curTempo;
if (!notePlaying) {
int divider = curMelody[curNote + 1];
if (divider > 0) {
noteDur = wholenote / divider;
}
else if (divider < 0) {
noteDur = wholenote / abs(divider);
noteDur *= 1.5;
}
tone(BUZZER, curMelody[curNote] + changeTone, noteDur);
startNote = millis();
notePlaying = true;
}
else {
if (millis() - startNote >= noteDur) {
curNote += 2;
notePlaying = false;
}
}
if (curNote >= curNotes * 2) {// Loop bài
curNote = 0;
}
}
{ //6 hiệu ứng đèn
if (countButtonLight % 6 == 0)
scanEffect(strip_1.Color(255, 33, 196), 45);
else if (countButtonLight % 6 == 1) { // 6 màu
party(strip_1.Color(255, 33, 196), strip_1.Color(174, 33, 255),
strip_1.Color(255, 33, 92), strip_1.Color(10, 49, 245),
strip_1.Color(32, 152, 212), strip_1.Color(153, 212, 242));
}
else if (countButtonLight % 6 == 2) { //4 màu
circle(strip_1.Color(93, 232, 153), strip_1.Color(127, 226, 238),
strip_1.Color(9, 39, 235), strip_1.Color(23, 126, 235));
}
else if (countButtonLight % 6 == 3) {
meteorEffect(strip_1.Color(153, 212, 242),strip_1.Color(255,255,200), 45);
}
else if (countButtonLight % 6 == 4) {
sparkleEffect(strip_1.Color(190, 73, 40),
strip_1.Color(255,255,200), strip_1.Color(127, 226, 238), 70);
}
else if (countButtonLight % 6 == 5) { // 4 màu
waveEffect(strip_1.Color(93, 232, 153), strip_1.Color(127, 226, 238),
strip_1.Color(255, 100, 0), strip_1.Color(255, 0, 150), 70);
}
}
delay(50);
}