I am working an arduino project in which I want to brighten or dim the led, linearly between this time.
Example:
At intitial time T0 the PWM is at 20, at T1 it supposed to be at 120. Time difference between TO and T1 is 1 min ( 60,000 millis).
T1-T0=100 (+ve increments, )
Each interval = 60,000/100
So I want to increment the led brightness by one every 600mills. And since there are many such sub programs I can't run delay the function.
I amnot sure what I missing out as the code is not running as required. Could you please help. Please find below my code for your reference.
String fromAndroid;
int pwm02 = 2;
int white_t0 = 0;
int white_t1 = 50;
int white_c;
int white_d;
int white_e;
int white_f;
int white_g;
int white_millinow = 0;
int white_Lastrun = 0;
int hour;
int minute;
int sec;
Here is the code formatted better and in code tags (OP TAKE NOTE)
int pwm02 = 2;
int white_t0 = 0;
int white_t1 = 50;
int white_c;
int white_d;
int white_e;
int white_f;
int white_g;
int white_millinow = 0;
int white_Lastrun = 0;
int hour;
int minute;
int sec;
void setup()
{
Serial.begin(9600);
Serial.println("Program Start..");
pinMode(pwm02, OUTPUT);
}
void loop()
{
// analogWrite(pwm02,white_t0);
//DateTime now = rtc.now();
//hour = now.hour();
//minute = now.minute();
//sec = now.second();
white_c = white_t1 - white_t0;
white_g = white_t0;
if (white_c < 0)
{
white_d = -1;
}
else
{
white_d = 1;
}
white_e = 20000 / abs(white_c);
white_f = millis();
analogWrite(pwm02, 0);
do
{
white_millinow = millis();
if (millis() >= ((white_millinow + white_e)))
{
white_g = white_g + 1;
analogWrite(pwm02, white_g);
white_millinow = millis() + white_e;
}
if (millis() < ((white_millinow + white_e)))
{}
}
while (white_g <= white_t1);
}
The first thing to say is that better variable names would help a lot in understanding the program as would the occasional comment. Note the advice about using unsigned long as the data type for variables used with millis() timing. Note too that using addition for timing calculations will fail when the value of millis() overflows to zero.
Thank you for your responses. I will try to use more descriptive variable names. I also considered what UKHeliBob said on miilis overflowing to 0. since i am also using an RTC (ds1307) in the project, is these some code which could convert an hour to 3600 seconds, so instead on the millis i can use that as a reference.
Im new but the while loops might cause issues for you it your timing it tight like you say. while loops are almost same as delay as they stay inside loop and don't loop main loop until condition is met.
do
{
white_millinow = millis();
if (millis() >= ((white_millinow + white_e)))
{
white_g = white_g + 1;
analogWrite(pwm02, white_g);
white_millinow = millis() + white_e;
}
if (millis() < ((white_millinow + white_e)))
{}
}
while (white_g <= white_t1);
Notice that white_millinow is set to millis() inside the loop. Then it is immediately checked to see if millis() has advanced to white_millinow + white_e. I can guarantee you that this will never be the case.
There is an easier way to avoid the millis() rollover issue than using the RTC. Just use subtraction instead of addition. For example: if (millis() - white_millinow >= white_e)The subtraction of unsigned integers works across rollover boundaries.
Also, comdirect is correct. If you need to do something else in your loop() function, this do-while loop will lock the processor up as badly as delay().