I am making an Aquarium Computer and i am currently trying to address the LED dimming procedure. I have used an advance Time Input Widget which gives a start/stop Schedule. This info is then taken to a TimeCheck() function which checks the RTC against the Time Input Widget and determines if further action is required. If it is it will activate either a LedFadeIn() or LedFadeOut() function. 2 further widgets are used 1 to input the LED intensity and the other to input the fade time. A further function setLed() which handles the mathematical equation to get stepWaitTime. The problem is the led turns on (dimly) and turns off at the correct times depending on the Time Input but does not fade in or out.
Can someone please kindly guide me where I am going wrong. My suspicion is that the function setLed() is not correctly place and thus the maths is not working.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>
WidgetRTC rtc;
BlynkTimer timer;
char auth[] = "";
char ssid[] = "";
char pass[] = "";
//RTC/Time Input Widget Variables
char currentTime[9];
char currentDate[11];
char startTime[9];
char stopTime[9];
int SThour;
int STmin;
int STsec;
int SPhour;
int SPmin;
int SPsec;
int dayNumber;
bool activeToday = false;
bool scheduler = true;
bool schedulerStatusChange = false;
//LED FADE ATTRIBUTES & VARIABLES
int fadetime = 0;
long fadesec = 0;
long fadeMillis = 0;
int minPWM = 1; //Variable for minimum PWW value. Keep at 1 to avoid crashing the fade routine.
byte fadeInc = 1; //Fade increment 1/1023.
int maxPWM = 0;
int currFadePos = 0;
unsigned long prevFadeMillis;
long stepWaitTime = 0;
int desCH1Lv = 0;
#define CH1 D1
//int CH1FadeTime;
//#define FadeUp 1
//#define FadeDown -1
//LEDFader led1;
//int direction = FadeUp;
void setLed()
{
if (currFadePos <= 1)
{
analogWrite(CH1, 0);
}
else
{
stepWaitTime = (fadeMillis/(maxPWM/fadeInc));
analogWrite(CH1, currFadePos);
}
}
void LedFadeIn ()
{
unsigned long thisMillis = millis();
if (thisMillis - prevFadeMillis >= stepWaitTime)
{
currFadePos = currFadePos + fadeInc;
if (currFadePos >= maxPWM)
{
currFadePos = maxPWM;
}
analogWrite(CH1, currFadePos);
prevFadeMillis = thisMillis;
}
}
void LedFadeOut ()
{
unsigned long thisMillis = millis();
if (thisMillis - prevFadeMillis >= stepWaitTime)
{
currFadePos = currFadePos - fadeInc;
if (currFadePos <= minPWM)
{
currFadePos = minPWM;
}
analogWrite(CH1, currFadePos);
prevFadeMillis = thisMillis;
}
}
BLYNK_WRITE(V10) //CH1 TIME IMPUT WIDGET
{
TimeInputParam t(param);
SThour = t.getStartHour();
STmin = t.getStartMinute();
STsec = t.getStartSecond();
SPhour = t.getStopHour();
SPmin = t.getStopMinute();
SPsec = t.getStopSecond();
dayNumber = weekday();
activeToday = false;
unsigned int OnDays = 0;
for (int i = 1; i <= 7; i++) // Process weekdays (1-Mon, 2-Tues, 3-Wed, 4-Thurs, 5-Fri, 6-Sat, 7-Sun)
{
if (t.isWeekdaySelected(i))
{
OnDays++;
if(i==dayNumber)
{
activeToday = true;
}
Serial.println(String("Day ") + i + " is selected");
}
}
if (OnDays == 0)
{
scheduler = false;
}
else
{
scheduler = true;
}
if (scheduler != schedulerStatusChange)
{
schedulerStatusChange = scheduler;
}
}
BLYNK_WRITE(V9) //CH1 FADE TIME WIDGET
{
fadetime = param.asInt();
fadesec = map(fadetime, 0, 120, 0, 7200); //2 hour max fade duration
fadeMillis = map(fadetime, 0, 120, 0, 7200000);
}
BLYNK_WRITE(V8) //CH1 UV INTENSITY WIDGET
{
desCH1Lv = param.asInt();
maxPWM = map(desCH1Lv, 0, 100, minPWM, 1023);
}
void TimeCheck()
{
sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second()); //Get RTC time
Serial.print("Current Time: ");
Serial.println(currentTime);
sprintf(startTime, "%02d:%02d:%02d", SThour, STmin, STsec); //Get start time from widget
Serial.print("Start Time: ");
Serial.println(startTime);
sprintf(stopTime, "%02d:%02d:%02d", SPhour, SPmin, SPsec); //Get stop time from widget
Serial.print("Stop Time: ");
Serial.println(stopTime);
if (activeToday == true)
{
if (hour() == SThour)
{
if (minute() == STmin)
{
setLed();
LedFadeIn();
Serial.println("Doing something now");
//analogWrite(LED, 255); // Turn ON built-in LED
}
else if (minute() < STmin)
{
Serial.println("Will do something");
}
else if (minute() > STmin)
{
Serial.println("Did something");
}
else
{
Serial.println("Clueless");
}
}
if (hour() == SPhour)
{
if (minute() == SPmin)
{
LedFadeOut();
Serial.println("Stopping something now");
//analogWrite(LED, 0); // Turn OFF built-in LED
}
else if (minute() < SPmin)
{
Serial.println("Will stop something");
}
else if (minute() > SPmin)
{
Serial.println("Stopped something");
}
else
{
Serial.println("Clueless");
}
}
Serial.println("----------");
}
}
void ClockDisplay()
{
sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
sprintf(currentDate, "%02d/%02d/%04d", day(), month(), year());
Blynk.virtualWrite(V0, currentTime); //Send time to TIME widget
Blynk.virtualWrite(V1, currentDate); //Send date to DATE widget
}
BLYNK_CONNECTED() //Connect to Blynk server
{
Blynk.syncAll();
TimeCheck();
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
analogWrite (CH1, 0);
rtc.begin();
setSyncInterval(30);
timer.setInterval(1000L, ClockDisplay);
timer.setInterval(500L, TimeCheck);
}
void loop()
{
Blynk.run();
timer.run(); // Initiates BlynkTimer
}