Advertisement
Guest User

Arduino ShiftPWM (Fade in, fade out LEDs one by one)

a guest
Sep 13th, 2012
1,826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. /************************************************************************************************************************************
  2. Fade in LED's one by one using ShiftPWM with one shift register
  3. ************************************************************************************************************************************/
  4.  
  5. // You can choose the latch pin yourself.
  6. const int ShiftPWM_latchPin=8;
  7.  
  8. #define SHIFTPWM_NOSPI
  9. const int ShiftPWM_dataPin = 11;
  10. const int ShiftPWM_clockPin = 12;
  11.  
  12.  
  13. // If your LED's turn on if the pin is low, set this to true, otherwise set it to false.
  14. const bool ShiftPWM_invertOutputs = false;
  15.  
  16. // You can enable the option below to shift the PWM phase of each shift register by 8 compared to the previous.
  17. // This will slightly increase the interrupt load, but will prevent all PWM signals from becoming high at the same time.
  18. // This will be a bit easier on your power supply, because the current peaks are distributed.
  19. const bool ShiftPWM_balanceLoad = false;
  20.  
  21. #include <ShiftPWM.h> // include ShiftPWM.h after setting the pins!
  22.  
  23. // Here you set the number of brightness levels, the update frequency and the number of shift registers.
  24. // These values affect the load of ShiftPWM.
  25. // Choose them wisely and use the PrintInterruptLoad() function to verify your load.
  26. // There is a calculator on my website to estimate the load.
  27.  
  28. unsigned char maxBrightness = 255;
  29. unsigned char pwmFrequency = 75;
  30. int numRegisters = 1;
  31. int numRGBleds = numRegisters*8/3;
  32.  
  33. void setup(){
  34.  
  35. // Sets the number of 8-bit registers that are used.
  36. ShiftPWM.SetAmountOfRegisters(numRegisters);
  37.  
  38. // SetPinGrouping allows flexibility in LED setup.
  39. // If your LED's are connected like this: RRRRGGGGBBBBRRRRGGGGBBBB, use SetPinGrouping(4).
  40. //ShiftPWM.SetPinGrouping(3); //This is the default, but I added here to demonstrate how to use the funtion
  41.  
  42. ShiftPWM.Start(pwmFrequency,maxBrightness);
  43. }
  44.  
  45.  
  46. void loop()
  47. {
  48. // Turn all LED's off.
  49. ShiftPWM.SetAll(0);
  50.  
  51. //ShiftPWM.SetOne(0, 255);
  52.  
  53. // For every led
  54. for(int pin = 0; pin < 8; pin++){
  55.  
  56. // Fade in
  57. for(unsigned char brightness = 0; brightness < 255; brightness++){
  58. ShiftPWM.SetOne(pin, brightness);
  59. delayMicroseconds(2000);
  60. }
  61.  
  62. // Fade out
  63. for(unsigned char brightness = 255; brightness > 0; brightness--){
  64. ShiftPWM.SetOne(pin, brightness);
  65. delayMicroseconds(2000);
  66. }
  67.  
  68. }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement