Set led brightness and fade to set value without delay (PWM)
hey guys,
first of all, want appreciate taking personal time answer question. anyway here's issue.
i want fade led brightness set value without delay. code:
**'dim' actual led brightness
**'set_value ' brightness level set through serial
ps: serial function has been omitted intentionally.
//////////////////////////
int fadeamount = 1; // how many points fade led by
unsigned long currenttime;
unsigned long looptime;
int set_value = 115; // brightness level set serial
int dim = 0;
void setup()
{
currenttime = millis();
looptime = currenttime;
}
void loop()
{
currenttime = millis();
if(currenttime >= (looptime + 50))
{
if(set_value < dim)
{
fadeamount = -fadeamount;
dim+=fadeamount;
}
if(set_value > dim)
{
fadeamount = 1;
dim+=fadeamount;
}
looptime = currenttime; // updates looptime
}
}
/////////////////////////
what doing wrong?
first of all, want appreciate taking personal time answer question. anyway here's issue.
i want fade led brightness set value without delay. code:
**'dim' actual led brightness
**'set_value ' brightness level set through serial
ps: serial function has been omitted intentionally.
//////////////////////////
int fadeamount = 1; // how many points fade led by
unsigned long currenttime;
unsigned long looptime;
int set_value = 115; // brightness level set serial
int dim = 0;
void setup()
{
currenttime = millis();
looptime = currenttime;
}
void loop()
{
currenttime = millis();
if(currenttime >= (looptime + 50))
{
if(set_value < dim)
{
fadeamount = -fadeamount;
dim+=fadeamount;
}
if(set_value > dim)
{
fadeamount = 1;
dim+=fadeamount;
}
looptime = currenttime; // updates looptime
}
}
/////////////////////////
what doing wrong?

try ...
the code bit easier understand if used better name dim. maybe currentledvalue.
and please use code tags when post code
(the "#" button above row of smileys).
regards
ray
code: [select]
if (set_value < dim)
{
dim -= fadeamount;
}
if (set_value > dim)
{
dim += fadeamount;
}
the code bit easier understand if used better name dim. maybe currentledvalue.
and please use code tags when post code

regards
ray
Arduino Forum > Using Arduino > Programming Questions > Set led brightness and fade to set value without delay (PWM)
arduino
Comments
Post a Comment