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? :smiley-eek:

try ...

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 :) (the "#" button above row of smileys).

regards

ray



Arduino Forum > Using Arduino > Programming Questions > Set led brightness and fade to set value without delay (PWM)


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support