Advertisement
icebowl

fade2.ino

Jun 28th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 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 led10 = 10; // the PWM pin the LED is attached to
  17. int led9 = 9;
  18. int brightness = 0; // how bright the LED is
  19. int fadeAmount = 5; // how many points to fade the LED by
  20.  
  21. // the setup routine runs once when you press reset:
  22. void setup() {
  23. // declare pin 9 to be an output:
  24. pinMode(led10, OUTPUT);
  25. pinMode(led9, OUTPUT);
  26. Serial.begin(9600);
  27. }
  28.  
  29. // the loop routine runs over and over again forever:
  30. void loop() {
  31. int b = 0;
  32. one(0);
  33. delay(250);
  34. two(0);
  35. }
  36.  
  37. void one(int b){
  38. int count = 0;
  39. brightness = b;
  40. while (count < 100){
  41. count++;
  42. Serial.println(count);
  43.  
  44. // set the brightness of pin 9:
  45. analogWrite(led10, brightness);
  46.  
  47. // change the brightness for next time through the loop:
  48. brightness = brightness + fadeAmount;
  49.  
  50. // reverse the direction of the fading at the ends of the fade:
  51. if (brightness <= 0 || brightness >= 255) {
  52. fadeAmount = -fadeAmount;
  53. }
  54. // wait for 30 milliseconds to see the dimming effect
  55. delay(30);
  56. analogWrite(led10, 0);
  57. }
  58. }
  59.  
  60. void two (int b){
  61. int count = 0;
  62. brightness = b;
  63. while (count < 100){
  64. count++;
  65. // set the brightness of pin 9:
  66. analogWrite(led9, brightness);
  67. Serial.println(brightness);
  68. // change the brightness for next time through the loop:
  69. brightness = brightness + fadeAmount;
  70.  
  71. // reverse the direction of the fading at the ends of the fade:
  72. if (brightness <= 0 || brightness >= 255) {
  73. fadeAmount = -fadeAmount;
  74. }
  75. // wait for 30 milliseconds to see the dimming effect
  76. delay(30);
  77.  
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement