I'm trying to get an LED to slowly brighten based on the time and then fade out later in the day at another specific time. The problem I'm having is that when I create the if statement the test LED fails to work. Removing the if statement and the LED reacts as it should gradually increasing in brightness and then dimming out.
The part I'm having issue with:
if (time_t() == "23:04:00") {
while (PWM1_DutyCycle < 255)
ledcWrite(PWM1_Ch, PWM1_DutyCycle++);
delay(100);
}
if (time_t() == "23:15:00") {
while (PWM1_DutyCycle > 0)
ledcWrite(PWM1_Ch, PWM1_DutyCycle--);
delay(100);
}
First - even leaving aside that strings cannot be compared in this way, as said in post#2,
this two lines in combination won't work
if (time_t() == "23:15:00") {
....
delay(1000);
because one second delay makes it unlikely that your program will have time to catch the moment when the seconds are exactly "00"
And in general, using the time as a string and compare it with another string is extremely bad idea.
As I see in the code - you have a hour, minute and second as integers, what prevent you from using it for time comparison?
Okay, so I figured how to use time hour and min to get the LED to fade in and out but it doesn't completely fade out but I also remember something about the frequency and resolution and while dimming the LED it doesn't completely take it to 0 so that's why the LED is still faintly lit. I'm also goign to swap out the delay for mills its just taking a bit or research since I'm still very new to this.
if (hour_ && minute_ == (20, 9))
while (PWM1_DutyCycle < 255){
ledcWrite(PWM1_Ch, PWM1_DutyCycle++);
delay (100);
}
else if (hour_ && minute_ == (20, 15))
while (PWM1_DutyCycle > 0){
ledcWrite(PWM1_Ch, PWM1_DutyCycle--);
delay (100);
#if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc
typedef unsigned long time_t;
#endif
Thanks, question though, is comparing the string the best way or should I use time in a different way?
It looks like you are attempting to program by guessing. That never works. Look at the example sketches from the DataTime library to see how to handle time.
Using both the time and the DateTime library at the same time, is unusual. Why are you doing that? What processor/board do you have?
ESP32 is the board I'm using and the if statement works now, the LED ramps up when its supposed to and the same for when it is supposed to ramp down except for a faint glow because its not taken down to 0.
Crackpot lol Thanks your a big help! I figured it was only a matter of time before someone with absolutely nothing constructive to add to a conversation shows up just to add to their post count...
Again you're offering nothing constructive to the conversation besides your own self gratification by spewing out how garbage someone's code is. Perhaps stick with the topics where someone isn't trying to learn so you wont be so annoyed at how beneath you their coding abilities are.
Instead of getting your back up and barking, maybe ask what is wrong with the code and how to fix it. Some of the most knowledgeable members have contributed to this thread, I suggest that you take advantage. Alienate them and you are on your own.
I'm going to have to disagree. Just today a handful of WAGs went well. (With the obvious caveat that protecting property and life are generally a mutual exclusion.)
I'm going to have to disagree. @anon57585045, has offered good constructive criticism.
No one, including you, was born able to program.
The rhetoric is bordering on ad hominem. By more than one. There will be no more of that.
So I've managed to get the time statement down so the LED turns on and off at the specified time and found out why the LED doesn't completely turn off so those issues are resolved but my next step is to replace the delays with mills. My question though is that the right approach especially if I want to draw out the fade in and out cycle to more closely simulate sunrise and sunset?
if (hour_ == 12 && minute_ == 4)
while (PWM1_DutyCycle < 255){
ledcWrite(PWM1_Ch, PWM1_DutyCycle++);
delay (100);
}
if (hour_ >= 12 && minute_ >= 30)
while (PWM1_DutyCycle > 0){
ledcWrite(PWM1_Ch, PWM1_DutyCycle--);
delay (100);