Light Fade In and Out Program

I made the program below that at 12:00 it starts fading the light to switch on and at 18:00 it starts to fade them off. It also read the pin output value and if the pin is off it allows it to switch on, if the time is between the set values(it is used incase of a power failure). It is currently set at 100% brightness and when I change the brightness to any other value, it reads that the pin is off (‘digitalRead(led)’) and restart the sequence to switch on again every minute. Any ideas how to fix it?

#include <DS3231.h>

//Changable Vars
int fadeTime = 1; //Fade Time
int onHour = 12; //Light On Hour
int onMin = 00; //Light On Minute
int offHour = 18; //Light On Hour
int offMin = 00; //Light On Minute
int led = 9; //Set pinout with PWM
int maxBrightness = 100; //LED Brightness Percentage
int val = 0; //Store value

//Set up Vars
DS3231  rtc(SDA, SCL);
Time t;
void start();
 
void setup()
{
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  rtc.begin();
  maxBrightness = map(maxBrightness,0,100,0,255); // Map percentage to output range.
}

void loop()
{
  t = rtc.getTime();
  
  // Send Day-of-Week
  //Serial.print(rtc.getDOWStr());
  //Serial.print(" "); 
  // Send date
  Serial.print(rtc.getDateStr());
  Serial.print(" -- ");
  // Send time
  Serial.print(rtc.getTimeStr());
  Serial.print(" -- ");
  val = digitalRead(led);    // read the input pin
  Serial.println(val);       // debug value

  if (val == 0 )
    {
      if (t.hour >= onHour && t.min >= onMin && t.hour < offHour)  //Check Time
        {
          on();
        } 
    }

  if (val == 1)
    {
      if (t.hour == offHour && t.min == offMin) //Check Time
        {
        off();
        }
    }

  
  // Wait one second before repeating
  delay (1000);
}

void on()
{
  // Fade script
  for (int i = 0 ; i <= maxBrightness; i++)
  {
    analogWrite(led, i);
    delay(((fadeTime * 60000)/306));
    Serial.println(" mil sec ");
    Serial.print(((fadeTime * 60000)/maxBrightness));
    Serial.print(" PWM " );
    Serial.print(i);
  }

  delay(60000); // Stay On
  analogWrite(led, maxBrightness);
}

void off()
{
  // Fade script
  for (int i = maxBrightness ; i >= 0; i--)
  {
    analogWrite(led, i);
    delay(((fadeTime * 60000)/306));
    Serial.println(" mil sec ");
    Serial.print(((fadeTime * 60000)/maxBrightness));
    Serial.print(" PWM " );
    Serial.print(i);
  }

  delay(60000); // Stay Off
  analogWrite(led, 0);
}

dmallia:
I change the brightness to any other value, it reads that the pin is off ('digitalRead(led)') and restart the sequence to switch on again every minute. Any ideas how to fix it?

you are performing a digitalRead() on a pin outputting PWM, and you don't want to do that.

instead, just "remember" if you left it in an on state or an off state

The RTC will let you know if the output is supposed to be ON or OFF or fading between. You know when the fade starts and can calculate when the fade ends. Set the brightness to the appropriate value for the time of day and you don’t have to worry about power failures.