LED not fading

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
}

Your LED fade routines don’t contain any loop code, so they are going through their code once, then not repeating.

Pete.

Ok… so where would you put them in? Also I was tinkering with cos fading but could find a way to implement it into my code. Any ideas?

The normal way would be to have a ‘for’ loop within your LED fade routines.
The issue from a Blynk perspective is that if these aren’t written very carefully they can cause Blynk disconnections, as the code won’t return to the void loop (and the Blynk.run that’s contained in the void loop) until the fade routine has executed.
If you search the forum you’ll find a variety of fade routines done in various ways.

Pete.

1 Like

Ok I have a working code now. I have used LEDFader library which works well. If anybody is familiar with this library any insight as to hoe to use the Curve.h feature will be much appreciated.
Thanks

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>
#include <LEDFader.h>
//#include <Curve.h>
WidgetRTC rtc;
BlynkTimer timer;

//WIFI Variables
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
LEDFader CH1;
#define Channel1 D1
#define FadeUp 1
#define FadeDown -1
int FadeIN = 0;
int FadeOUT = 0;
int minPWM = 0;   //Variable for minimum PWW value.
int maxPWM = 0;
int desCH1Lv = 0;
unsigned long FadeInTime;
unsigned long FadeOutTime;

/*void setLed()
{
  fadeSpeed = maxPWM / FadeIN;
  maxPWM = fadeSpeed * FadeIN;
  maxPWM = currFadePos;
  CH1.get_value();
  {
    if (CH1.get_value() != currFadePos)
    {
      CH1.set_value(currFadePos);
    }
  }
}
*/
void LedFadeIn ()
{
  CH1.fade(maxPWM, FadeInTime);
}

void LedFadeOut ()
{
  CH1.fade(minPWM, FadeOutTime);
}

    
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 FADEIN TIME WIDGET
{
  FadeIN = param.asInt();
  FadeInTime = map(FadeIN, 0, 120, 0, 7200000);    //2 hour max fade duration
}

BLYNK_WRITE(V7)   //CH1 FADEOUT TIME WIDGET
{
  FadeOUT = param.asInt();
  FadeOutTime = map(FadeOUT, 0, 120, 0, 7200000);    //2 hour max fade duration
}

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)
      {
        LedFadeIn();
        Serial.println("Doing something now");
        }
        else if (hour() == SPhour)
        {
          if (minute() == SPmin)
          {
            LedFadeOut();
            Serial.println("Stopping something now");
          }
        }
    }
  }
}

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);
  CH1 = LEDFader(Channel1);
  CH1.set_value(0);
  rtc.begin();
  setSyncInterval(30);
  timer.setInterval(1000L, ClockDisplay);
  timer.setInterval(500L, TimeCheck);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  CH1.update();  
}