/**
* Esp32_p9813 / Neopixel
* Gestisce un LED in PWM per simulare alba e tramonto
* accende le luci delle case
* simula un fuoco
* simula le stelle
* sole: GPIO27 sole
* case: GPIO14 case
* stelle: GPIO12 stelle
* fuoco: GPIO13 fuoco
* relè K1: GPIO 32
* relè K2: GPIO 33
*
* con mp3 player (dfminiplayer) collegato sui pin GPIO:
* 16 rx <- tx player
* 17 tx -> rx player
*
* neopixel su GPIO 19
* oppure
* P9813 data su GPIO 19 e clk su GPIO 21
*/
#include <HardwareSerial.h>
#include "driver/ledc.h"
#include "FastLED.h"
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <stdio.h>
#include <stdlib.h> // per array dinamici
#include <string.h>
// libreria che contiene le funzioni sulle strighe: qui la utiliziamo per la funzionione strcmp()
#include <SoftwareSerial.h>
#define NUM_LEDS 1
#define DATA_PIN 19
#define CLOCK_PIN 21
CRGB leds[NUM_LEDS];
int h = 0;
int s = 255;
int volume = 5;
int sole = 27;
//int stelle = 12;
//int fuoco = 13;
float xt = 1; //regolare tempo
float xv = 1; //regolare volume
SoftwareSerial mySoftwareSerial(16, 17); // RX, TX
HardwareSerial MySerial(1);
DFRobotDFPlayerMini myDFPlayer;
int trid = 0;
#define MAXTRACKS 18
int tracks[] = { 1, 9, 56, 3, 51, 11, 31, 8, 54, 30, 64, 25, 68, 23, 57, 65, 42, 70 };
const int ledPin1 = 27; // GPIO27 sole
const int ledPin2 = 12; // GPIO12 stelle
const int ledPin3 = 13; // GPIO13 fuoco
// setting PWM properties
const int freq = 5000;
const int ledChannelsole = 0;
const int ledChannelstelle = 0;
const int ledChannelfuoco = 0;
const int resolution = 8;
const int dutyCycle = 256;
//void printDetail(uint8_t type, int value);
void setup() {
mySoftwareSerial.begin(9600);
Serial.begin(115200);
pinMode(27, OUTPUT);
pinMode(14, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(33, OUTPUT);
pinMode(32, OUTPUT);
// configure LED PWM functionalitites
ledcSetup(ledChannelsole, freq, resolution);
ledcSetup(ledChannelstelle, freq, resolution);
ledcSetup(ledChannelfuoco, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin1, ledChannelsole);
ledcAttachPin(ledPin2, ledChannelstelle);
ledcAttachPin(ledPin3, ledChannelfuoco);
FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
//FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
randomSeed(analogRead(18));
//mySoftwareSerial.begin(9600);
Serial.begin(115200);
mySoftwareSerial.begin(9600);
while (!mySoftwareSerial) delay(10);
Serial.println(F("Presepe V2.0"));
delay(1000);
/*
if (!myDFPlayer.begin(mySoftwareSerial)) {
Serial.println(F("Error"));
while (true)
;
}
Serial.println(F("ready!"));
volume = volume * xv;
//Serial.print("volume ");
//Serial.println(volume);
myDFPlayer.volume(volume);
delay(1000);
myDFPlayer.play(tracks[7]);
trid++;
}
*/
}
unsigned long tc, t1, t2;
void loop() {
int sensorValue = analogRead(4);
//float xt = (sensorValue * 0.0025);
float xt = (1);
int AudioLevel = analogRead(15);
volume = map(AudioLevel, 0, 1023, 0, 30);
//myDFPlayer.volume(volume * xv); // valore volume 0 - 30 max
tc = millis();
if ((tc - t1) > 60000 * xt) {
Serial.print("t1/1000 ciclo in secondi : ");
Serial.println(t1 / 1000);
t1 = millis();
}
if ((tc - t1) < 6000 * xt) {
Serial.print("alba ");
}
if ((tc - t1) > 6000 * xt && (tc - t1) < 24000 * xt) {
Serial.print("giorno ");
}
if ((tc - t1) > 24000 * xt && (tc - t1) < 34000 * xt) {
Serial.print("tramonto ");
}
if ((tc - t1) > 34000 * xt) {
Serial.print("notte ");
}
Serial.print("ciclo sec : ");
Serial.print((tc - t1) / 1000);
Serial.print(" ");
Serial.print("xt : ");
Serial.print(xt);
Serial.print(" ");
Serial.print("traccia: ");
Serial.println(tracks[trid]);
//accende e spegne D27 sole
if ((tc - t2) > 100 * xt) {
//alba
taskPwm(27, tc - t1, 0, 6000 * xt, 0, 255);
//giorno
taskPin(27, tc - t1, 6000 * xt, 24000 * xt, HIGH);
//tramonto
taskPwm(27, tc - t1, 24000 * xt, 34000 * xt, 255, 0);
//notte
taskPin(27, tc - t1, 34000 * xt, 60000 * xt, LOW);
//accende e spegne D12 stelle
taskPin(12, tc - t1, 0, 3000 * xt, HIGH);
taskPin(12, tc - t1, 3000 * xt, 34000 * xt, LOW);
taskPin(12, tc - t1, 34000 * xt, 60000 * xt, HIGH);
if ((tc - t1) > 34000 * xt or (tc - t1) < 5000 * xt) {
stelle(12);
//il fuoco
fuoco(13);
}
//neopixel
//alba
taskNPAlba1(tc - t1, 0, 3000 * xt);
taskNPAlba2(tc - t1, 3000 * xt, 6000 * xt);
//giorno
taskNPGiorno(tc - t1, 6000 * xt, 24000 * xt);
//tramonto
taskNPTramonto1(tc - t1, 24000 * xt, 26000 * xt);
taskNPTramonto2(tc - t1, 26000 * xt, 29000 * xt);
taskNPTramonto3(tc - t1, 29000 * xt, 34000 * xt);
//notte
taskNPNotte(tc - t1, 34000 * xt, 60000 * xt);
//accende e spegne le case
taskPin(14, tc - t1, 0, 3000 * xt, HIGH);
taskPin(14, tc - t1, 28000 * xt, 60000 * xt, HIGH);
//accende e spegne K1
taskPin(32, tc - t1, 0, 3000 * xt, LOW);
taskPin(32, tc - t1, 3000 * xt, 28000 * xt, HIGH);
taskPin(32, tc - t1, 28000 * xt, 60000 * xt, LOW);
//accende e spegne K2
taskPin(33, tc - t1, 0, 5000 * xt, LOW);
taskPin(33, tc - t1, 5000 * xt, 35000 * xt, HIGH);
taskPin(33, tc - t1, 35000 * xt, 60000 * xt, LOW);
//musica
//music();
Serial.println("tc - t1 ");
Serial.println(tc - t1);
t2 = millis();
}
}
void taskPwm(int pin, unsigned long t, unsigned long t1, unsigned long t2, int l1, int l2) {
if (t >= t1 && t < t2) {
int dutyCycle = map(t - t1, 0, t2 - t1, l1, l2);
Serial.print(" dutyCycle ");
Serial.println(dutyCycle);
ledcWrite(pin, dutyCycle);
}
}
void taskPin(int pin, unsigned long t, unsigned long t1, unsigned long t2, int stato) {
if (t >= t1 && t < t2) {
ledcWrite(pin, stato);
}
}
void stelle(int pin) {
ledcWrite(pin, random(25));
ledcWrite(pin, random(89));
}
void fuoco(int pin) {
ledcWrite(pin, random(256));
}
void music() {
//suona una dopo l'altra le tracce presenti sulla sdcard
int st = myDFPlayer.readState();
//Serial.println(st);
//513 è in play
//512 ha finito
if ((st == 512)) {
//ha terminato
myDFPlayer.play(tracks[trid]);
Serial.print("track: ");
Serial.println(tracks[trid]);
trid++;
if (trid >= MAXTRACKS) trid = 0;
} else if (st == 513) {
//playing...
}
}
void taskNPAlba1(unsigned long t, unsigned long t1, unsigned long t2) {
if (t >= t1 && t < t2) {
h = map(t - t1, 0, t2 - t1, 160, 200);
fill_solid(leds, NUM_LEDS, CHSV(h, s, 255));
FastLED.show();
}
}
void taskNPAlba2(unsigned long t, unsigned long t1, unsigned long t2) {
if (t >= t1 && t < t2) {
s = map(t - t1, 0, t2 - t1, 255, 0);
fill_solid(leds, NUM_LEDS, CHSV(h, s, 255));
FastLED.show();
}
}
void taskNPGiorno(unsigned long t, unsigned long t1, unsigned long t2) {
if (t >= t1 && t < t2) {
h = 64; //giallo
s = 0;
fill_solid(leds, NUM_LEDS, CRGB::White);
FastLED.show();
}
}
void taskNPNotte(unsigned long t, unsigned long t1, unsigned long t2) {
if (t >= t1 && t < t2) {
h = 160; //giallo
fill_solid(leds, NUM_LEDS, CRGB::Blue);
FastLED.show();
}
}
void taskNPTramonto1(unsigned long t, unsigned long t1, unsigned long t2) {
if (t >= t1 && t < t2) {
s = map(t - t1, 0, t2 - t1, 0, 255);
fill_solid(leds, NUM_LEDS, CHSV(h, s, 255));
FastLED.show();
}
}
void taskNPTramonto2(unsigned long t, unsigned long t1, unsigned long t2) {
if (t >= t1 && t < t2) {
h = map(t - t1, 0, t2 - t1, 64, 0);
fill_solid(leds, NUM_LEDS, CHSV(h, s, 255));
FastLED.show();
}
}
void taskNPTramonto3(unsigned long t, unsigned long t1, unsigned long t2) {
if (t >= t1 && t < t2) {
h = map(t - t1, 0, t2 - t1, 255, 60);
fill_solid(leds, NUM_LEDS, CHSV(h, s, 255));
FastLED.show();
}
}