Hallo zusammen,
ich habe eine Kleine Lampe entwickelt bei der ein Timer abläuft sobald sie auf einem Untergrund steht. Sollte die Lampe stromlos sein, Soll der Timer ab dem Punkt der Stromunterbrechung weiterlaufen. Aus diesem Grund speichere ich den Status zwischen. Das Problem ist, dass die Lampe nach dem Neustart erst angehoben werden muss damit die Lampe weiter läuft. Sie soll aber sofort nachdem der Strom ausgefallen ist weiter laufen. Ich bin jetzt nicht der Programmierprofi und der Code ist sicher nicht das gelbe vom Ei. Kann mir Jemand einen Tip geben wie ich es hin bekomme, dass die Lampe nach Stromausfall so weiter läuft? Das ganze läuft aus einem Wemos D1 Hier der Code:
#include <NeoAnimationFX.h>
#include <EEPROM.h>
#include <BH1750FVI.h>
#define numPixels 16
#define PIN 2 //GPIO3
BH1750FVI myLux(0x23);
uint32_t lastUpdate = 0;
#ifdef ESP8266
typedef NeoPixelBrightnessBus<NeoGrbFeature, Neo800KbpsMethod> NEOMETHOD; //uses GPIO3/RX
#endif
#ifdef ESP32
typedef NeoPixelBrightnessBus<NeoGrbFeature, NeoEsp32I2s1800KbpsMethod> NEOMETHOD;
#endif
NEOMETHOD strip(numPixels, PIN); // PIN is ignored for ESP8266
NeoAnimationFX<NEOMETHOD> myPixelRef(strip);
int addr = 0;
int i=0,r=0,g=0,b=0,z=1,f=1,fz=0; // Farbe des LED Ring
long lastModeT = 0; // interne Variable
long minuten; //Hier Zeit einstellen
long case1=3; // Zeit Stick 1 in Minuten
// long case2=40; // Zeit Stick 2 in Minuten
// long case3=1; // Zeit Sick 3 in Minuten
int timer;// interne Variable
int wert = 10;//Lux sensibilitaet
int zwischen;
void EEPROMWriteInt(int p_address, int p_value)
{
byte lowByte = ((p_value >> 0) & 0xFF);
byte highByte = ((p_value >> 8) & 0xFF);
EEPROM.write(p_address, lowByte);
EEPROM.write(p_address + 1, highByte);
}
//This function will read a 2 byte integer from the eeprom at the specified address and address + 1
unsigned int EEPROMReadInt(int p_address)
{
byte lowByte = EEPROM.read(p_address);
byte highByte = EEPROM.read(p_address + 1);
return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
}
void setup() {
Wire.begin();
myLux.powerOn();
myLux.setContLowRes();
myPixelRef.service();
myPixelRef.init();
myPixelRef.setBrightness(255);
myPixelRef.setColor(r,g,b);
myPixelRef.start();
myPixelRef.clear();
myPixelRef.setMode(i);
zwischen = EEPROMReadInt(addr);
if(zwischen > 0) // Zwischenspeicher Wert laden
{
timer = zwischen;
i=3;
f=1;
myPixelRef.init();
myPixelRef.setBrightness(255);
myPixelRef.setColor(r,g,b);
myPixelRef.start();
myPixelRef.clear();
myPixelRef.setMode(i);
}
else
{
timer = 0;
}
EEPROM.begin(512);
/* myPixelRef.init();
myPixelRef.setColor(r,g,b);
myPixelRef.start();
*/
}
void pixelmode() // 1 Sekunden Timer
{
myPixelRef.service();
if (millis() - lastModeT > 1000) {
if(myLux.getLux() < wert){
lastModeT = millis();
timer++;
EEPROMWriteInt(addr, timer);
EEPROM.commit();
EEPROM.end();
}
}
}
void lux()
{
if((myLux.getLux() < wert) || (timer > 0)) // Sensor Ausgabe Lampe auf Fahrzeug
{
if(timer < minuten*60){
i=3;
}
if(timer >= minuten*60){
timer=minuten*60;
f=0;
EEPROMWriteInt(addr, 0);
EEPROM.commit();
EEPROM.end();
i=1;//Rot Blinken
}
}
if(myLux.getLux() > wert)// Sensor Ausgabe Lampe nicht auf Fahrzeug
{
if(timer >= minuten*60){
f=fz+1;
i=0; //Nur An
}
timer=0;
EEPROMWriteInt(addr, timer);
EEPROM.commit();
EEPROM.end();
f=1;
i=0; //Nur An
}
}
void farbwechsel() //Neuinitialisierung bei Modewechsel
{
if(i != z)
{
myPixelRef.init();
myPixelRef.setBrightness(255);
myPixelRef.setColor(r,g,b);
myPixelRef.start();
myPixelRef.clear();
myPixelRef.setMode(i);
z=i;
}
}
void loop() // Routinen aufrufen Endlosschleife
{
lux();
farbe();
pixelmode();
farbwechsel();
}
void farbe()
{
if(f == 1) //tuerkis
{
r=64;
g=224;
b=208;
fz=f;
minuten=case1;
}
/* if(f == 2) //Gelb
{
r=255;
g=255;
b=0;
fz=f;
minuten=case2;
} */
/* if(f == 3) // Aktivieren wenn Stick 3 dazu kommt
{
r=0;
g=255;
b=0;
fz=f;
minuten=case3;
} */
if(f == 0)
{
r=255;
g=0;
b=0;
}
if(f > 2) // 2 auf 3 erhoehen wenn Stick 3 dazu kommt
{
f=0;
i=0;
}
}