Hello!
I'm currently working on a LED time changing program. What I would like it to do is change the brightness of the LED as time passes by with a DS3231 RTC module. I have done this with two different functions, which I believe are not the problem. What I'm having trouble with is that the LED does not change the brightness.
I would really appreciate your help.
Here is my program:
#include <RTClib.h>
#include <Wire.h>
int led = 9;
int brightness1 = 0;
int brightness2 = 0;
float hora = 0;
RTC_DS3231 rtc;
void setup () {
Serial.begin(9600);
pinMode(led, OUTPUT);
if (! rtc.begin()) {
Serial.println("No hi ha un mòdul RTC");
while (1);
}
}
void loop () {
DateTime now = rtc.now();
hora = now.hour();
while((hora >= 6) && (hora <= 12)){
brightness1 = int(255*exp(-sq(hora-12)/8));
analogWrite(led, brightness1);
}
while((hora >= 12) && (hora <= 16)){
analogWrite(led, 255);
}
while((hora >= 16) && (hora <= 22)){
brightness2 = int(255*exp((-sq(hora-16))/8));
analogWrite(led, brightness2);
}
}
Hi,
as @anon73444976 warned, the code enters the while and doesn't come out anymore because it doesn't update the time variable.
And what to do if the time is for example 16?
has two valid options, but the code will enter the first option.
while((time >= 12) && (time <= 16)) validates for 16 hours;
while((time >= 16) && (time <= 22)) validates for 16 hours;
#include <RTClib.h>
#include <Wire.h>
DateTime now;
int led = 9;
int brightness1 = 0;
int brightness2 = 0;
float hora = 0;
int minu;
RTC_DS3231 rtc;
void setup ()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
if (! rtc.begin())
{
Serial.println("No hi ha un mòdul RTC");
while (1);
}
}
void loop () {
now = rtc.now();
hora = now.hour();
Serial.println(hora);
while((hora >= 6) && (hora < 12))
{
brightness1 = int(255*exp(-sq(hora-12)/8));
Serial.println(brightness1);
analogWrite(led, brightness1);
now = rtc.now();
hora = now.hour();
}
while((hora >= 12) && (hora < 16))
{
analogWrite(led, 255);
now = rtc.now();
hora = now.hour();
}
while((hora >= 16) && (hora <= 22))
{
brightness2 = int(255*exp((-sq(hora-16))/8));
Serial.println(brightness2);
analogWrite(led, brightness2);
now = rtc.now();
hora = now.hour();
}
}