Question with fade.


if use
code: [select]
/*
fade

example shows how fade led on pin 9
using analogwrite() function.

example based on arduino example fade sketch
modified use timing instead of delay() function

*/
int brightness = 0;    // how bright led is
int fadeamount = 5;    // how many points fade led by
unsigned long currenttime;
unsigned long looptime;

void setup()  {
 // declare pin 9 output:
 pinmode(9, output);
 currenttime = millis();
 looptime = currenttime;
}

void loop()  {
 currenttime = millis();
 if(currenttime >= (looptime + 20)){  
   // set brightness of pin 9:
   analogwrite(9, brightness);    

   // change brightness next time through loop:
   brightness = brightness + fadeamount;

   // reverse direction of fading @ ends of fade:
   if (brightness == 0 || brightness == 255) {
     fadeamount = -fadeamount ;
   }    
   looptime = currenttime;  // updates looptime
 }
 // other processing can done here
                         
}
led fades half brightness.
but if use
code: [select]
/*
fade

example shows how fade led on pin 9
using analogwrite() function.

example based on arduino example fade sketch
modified use timing instead of delay() function

*/
int brightness = 0;    // how bright led is
int fadeamount = 5;    // how many points fade led by
unsigned long currenttime;
unsigned long looptime;

void setup()  {
 // declare pin 9 output:
 pinmode(9, output);
   serial.begin(9600);
 currenttime = millis();
 looptime = currenttime;
}

void loop()  {
 currenttime = millis();
 if(currenttime >= (looptime + 20)){  
   // set brightness of pin 9:
   analogwrite(9, brightness);    

   // change brightness next time through loop:
   brightness = brightness + fadeamount;
   serial.println(brightness);
   // reverse direction of fading @ ends of fade:
   if (brightness == 0 || brightness == 255) {
     fadeamount = -fadeamount ;
   }    
   looptime = currenttime;  // updates looptime
 }
 // other processing can done here
                         
}

add     serial.begin(9600); setup ,     serial.println(brightness); loop, fade full brightness.  why?

because adding serial.print adds delay loop , allows led remain @ set time long enough see it.

increases time between updates in first code.


Arduino Forum > Using Arduino > LEDs and Multiplexing > Question with fade.


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support