// C++ code
#include "TM1637TinyDisplay.h"
//#include <Adafruit_NeoPixel.h>
#include <FastLED.h>
#define PIN 3
#define NUMPIXELS 4
#define CLK 4
#define DIO 5
#define PINBUTTON 0
TM1637TinyDisplay display(CLK, DIO);
CRGB leds[NUMPIXELS];
CRGB soleil = CRGB(255,160,0);
CRGB nuit = CRGB(255,50,4);
int range;
int etat = 0;
unsigned long tempsRestant = 0;
int unitesRestantes = 0;
int milliRestant = 0;
int tempsDebut = -1;
unsigned long tempsFin = 0;
unsigned long delayEcran = 0;
// unit of time
// set 2000ms for tests
// set 3600000 for prod (unit=1hour)
long unite = 2000;
int tempsModifie = false;
int creditUnites;
bool ecranAllume =false;
bool veilleuseAllumee = false;
bool soleilAllume = false;
// LEDS
int etoile = 2;
int lune = 0;
int soleil1 = 1;
int soleil2 = 3;
bool premierTour=true;
unsigned long deltaT=0;
void setup()
{
FastLED.addLeds <WS2812, PIN, GRB>(leds, NUMPIXELS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness( 20 );
Serial.begin(9600);
delay(2000);
Serial.println("Initialisation");
//Serial.print("Valeur de NUMPIXELS");
//Serial.print(NUMPIXELS);
display.begin();
pinMode(PINBUTTON, INPUT);
display.setBrightness(0x02);
}
void AfficheTempsRestant (int etatactuel) {
Serial.println("affichage");
if (etatactuel == 1) {
display.showNumber(creditUnites);
} else {
tempsRestant = (tempsDebut+deltaT)-millis();
unitesRestantes = (tempsRestant)/unite;
if (millis()-tempsDebut<deltaT )
{
display.showNumber(unitesRestantes);
} else {
display.showString("----");
}
}
}
void gestionBouton()
{
int duree;
ecranAllume = true;
delayEcran=millis();
// etat 0 : l'écran est éteint.
// On le rallume et on affiche le temps restant
// en nombre d'unités
if (etat==0) {
tempsRestant = (tempsDebut + deltaT)-millis();
if (millis()-tempsDebut<=deltaT) {
creditUnites=(tempsRestant)/unite;
} else {
creditUnites = 0;
}
AfficheTempsRestant(etat);
etat = 1 ;
tempsModifie=false;
// Sino etat 1 : on est en phase de réglage
// On augmente par unités de 1
} else {
tempsModifie=true;
// on relance l'affichage
// Si la nuit est en cours
// (le temps de fin est dans le futur)
if (creditUnites>0)
{
// on ajoute une unité au temps restant et on arrondi
creditUnites += 1;
creditUnites = creditUnites%24;
} else {
creditUnites = 1;
}
AfficheTempsRestant(etat);
}
}
void SortieEtat1 () {
display.clear();
ecranAllume = false ;
etat=0;
if (tempsModifie)
{
// Serial.println("credit");
// Serial.println(creditUnites);
if (creditUnites>0) {
veilleuseAllumee = false;
tempsDebut = millis();
} else {
tempsDebut = millis()-unite/2;
}deltaT = creditUnites * unite;
}
//Serial.println("temps actuel : ");
//Serial.println(millis());
//Serial.println("temps restant : ");
tempsRestant=tempsFin-millis();
//Serial.println(tempsRestant);
}
void gestionLumiere()
{
//Serial.println("gestion lumiere");
//Serial.println(tempsFin);
if ((veilleuseAllumee) & (millis()-tempsDebut>deltaT) & (millis()-tempsDebut<deltaT+unite/2)) {
//veilleuseAllumee = false;
for (int i=0;i<=NUMPIXELS-1;i++) {
leds[i]=soleil;
}
//Serial.println("Je passe par le choix 1");
FastLED.show();
} else if ((not(veilleuseAllumee)) & (millis()-tempsDebut<=deltaT)) {
veilleuseAllumee = true;
leds[etoile]=nuit;
leds[lune]=nuit;
leds[soleil1]=CRGB::Black;
leds[soleil2]=CRGB::Black;
//
// for (int i=0;i<=NUMPIXELS-3;i++) {
// leds[i]=nuit;
// delay(100);
// }
// leds[NUMPIXELS-1]=CRGB::Black;
// leds[NUMPIXELS-2]=CRGB::Black;
// Serial.println("Couleur");
// Serial.println(leds[1].r);
// Serial.println(leds[1].g);
// Serial.println(leds[1].b);
//
FastLED.show();
} else if ((veilleuseAllumee)&(millis()-tempsDebut>=deltaT-unite/2)&(millis()-tempsDebut<deltaT)) {
FastLED.setBrightness(10);
float lambda = 2*float(millis()-tempsDebut-deltaT+unite/2)/float(unite);
int r = int(round((1-lambda)*nuit.r+lambda*soleil.r));
int g = int(round((1-lambda)*nuit.g+lambda*soleil.g));
int b = int(round((1-lambda)*nuit.b+lambda*soleil.b));
for (int i=0;i<=NUMPIXELS-1;i++) {
leds[i]=CRGB(r,g,b);
}
FastLED.show();
} else if ((veilleuseAllumee)&(millis()-tempsDebut>deltaT+(unite/2))){
// Serial.println("Je passe par le choix 4");
veilleuseAllumee=false;
for (int i=0;i<=NUMPIXELS-1;i++) {
leds[i]=CRGB::Black;
}
FastLED.show();
}
FastLED.show();
}
void loop()
{
if (premierTour) {
//Serial.println(NUMPIXELS);
for (int i=0;i<=NUMPIXELS-1;i++) {
leds[i]=CRGB::Black;
}
premierTour=false;
FastLED.show();
}
//display.showString("help");
if (ecranAllume & (millis()>delayEcran+1000) )
{
SortieEtat1();
delay(200);
}
gestionLumiere();
int val;
val = analogRead(PINBUTTON);
//Serial.println(val);
if (val>500) {
gestionBouton();
delay(300);
}
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1