Advertisement
icebowl

fade-function-v1

Jun 29th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. /*
  2. Fade
  3.  
  4. This example shows how to fade an LED on pin 9
  5. using the analogWrite() function.
  6.  
  7. The analogWrite() function uses PWM, so if
  8. you want to change the pin you're using, be
  9. sure to use another PWM capable pin. On most
  10. Arduino, the PWM pins are identified with
  11. a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
  12.  
  13. This example code is in the public domain.
  14. */
  15.  
  16. int led9 = 9;
  17. int led10 = 10;
  18.  
  19. int brightness = 0; // how bright the LED is
  20. int fadeAmount = 5; // how many points to fade the LED by
  21.  
  22. // the setup routine runs once when you press reset:
  23. void setup() {
  24. // declare pin 9 to be an output:
  25. pinMode(led9, OUTPUT);
  26. pinMode(led10, OUTPUT);
  27. }
  28.  
  29. // the loop routine runs over and over again forever:
  30. void loop() {
  31. fade (9,0);
  32. fade (10,127);
  33. }
  34.  
  35. void fade (int led, int b){
  36. int i;
  37. for (i = 0; i < 512; i++){
  38. analogWrite(led, brightness);
  39. // change the brightness for next time through the loop:
  40. brightness = brightness + fadeAmount;
  41.  
  42. // reverse the direction of the fading at the ends of the fade:
  43. if (brightness <= 0 || brightness >= 255) {
  44. fadeAmount = -fadeAmount;
  45. }
  46. // wait for 30 milliseconds to see the dimming effect
  47. delay(30);
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement