I have written this bit of code for my LED lights for my aquarium using Blynk as the UI. it works as expected except for 1 issue.
I have created a state machine whereby a button is checked along with the temperature of the heatsink. the issue I have is that once I am within the AUTO state which is the state whereby the LEDS are doing their thin I need to keep an eye on the heatsink temperature and if it gets too hot I need to jump out of the state and go to the LEDSAFE state wich is a procedure of automatically dimming all the leds to a level to allow for passive cooling of the heatsink. When temp of the heatsink is drops to is acceptable value I need to return to the Auto state.
I have tied various options but cant seem to get out of the AUTO state to go to LEDSAFE state.
I am using an ESP8266 D1 mini so all my bits are 1023 as opposed to 255.
Any suggestions please?
void LEDFunction()
{
switch (state)
{
case BUTTCHECK:
{
if (BTNval == 1 && TempAlarm == false) // button is 1 and the heatsink temp is OK
{
state = AUTO;
}
else if (BTNval == 0 && TempAlarm == false) //button is 0 and the heatsink temp is OK
{
state = MANUAL;
}
else if (BTNval == 1 && TempAlarm == true) //button is 1 but heatsink temp is NOT OK
{
state = LEDSAFE;
}
else
{
state = LEDSAFE; //button is 0 but heatsink temp is NOT OK
}
break;
}
case MANUAL: //manual mode to set the LEDS brightness
{
analogWrite(CH1, maxPWM1);
analogWrite(CH2, maxPWM2);
analogWrite(CH3, maxPWM3);
analogWrite(CH4, maxPWM4);
analogWrite(CH5, maxPWM5);
analogWrite(CH6, maxPWM6);
state = BUTTCHECK;
break;
}
case AUTO: //AUTO mode the function of the leds
{
if (TempAlarm == false)
{
sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
Serial.println(currentTime);
TotalMillis = (((hour()*3600) + (minute()*60) + second()) * 1000);
CH1Period = CH1End - CH1Start;
CH2Period = CH2End - CH2Start;
CH3Period = CH3End - CH3Start;
CH4Period = CH4End - CH4Start;
CH5Period = CH5End - CH5Start;
CH6Period = CH6End - CH6Start;
SetLed(CH1, TotalMillis, CH1Start, FadeINmillis1, CH1Period, FadeOUTmillis1, maxPWM1, V38); //UV
SetLed(CH2, TotalMillis, CH2Start, FadeINmillis2, CH2Period, FadeOUTmillis2, maxPWM2, V39); //RBLUE/BLUE
SetLed(CH3, TotalMillis, CH3Start, FadeINmillis3, CH3Period, FadeOUTmillis3, maxPWM3, V40); //BLUE
SetLed(CH4, TotalMillis, CH4Start, FadeINmillis4, CH4Period, FadeOUTmillis4, maxPWM4, V41); //OCW
SetLed(CH5, TotalMillis, CH5Start, FadeINmillis5, CH5Period, FadeOUTmillis5, maxPWM5, V42); //AMBER
SetLed(CH6, TotalMillis, CH6Start, FadeINmillis6, CH6Period, FadeOUTmillis6, maxPWM6, V55); //LIME
state = BUTTCHECK; //THIS IS WHERE I HAVE THE ISSUE. WHAT I WANT IS IF THE
HEATSINK TEMP IS OK CONTINUE THROUGH AS NORMAL BUT IF BY CHANCE THE HEATSINK FAN IS BROKEN AND TEMP RISES THEN IT MUST EMMEDIATLY GO TO THE LEDSAFE OPTION.
if (TempAlarm == true)
{
state = LEDSAFE;
}
}
break;
}
case LEDSAFE: //ledsafe option dim all channels down to 300
{
analogWrite(CH1, 300);
analogWrite(CH2, 300);
analogWrite(CH3, 300);
analogWrite(CH4, 300);
analogWrite(CH5, 300);
analogWrite(CH6, 300);
state = BUTTCHECK;
break;
}
}
}