SHARE
TWEET

Clever Arduino PWM Larson Scanner

a guest Apr 14th, 2014 165 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  <span class="skimlinks-unlinked">www.blinkenlight.net</span>
  3. //
  4. //  Copyright 2011 Udo Klein
  5. //
  6. //  This program is free software: you can redistribute it and/or modify
  7. //  it under the terms of the GNU General Public License as published by
  8. //  the Free Software Foundation, either version 3 of the License, or
  9. //  (at your option) any later version.
  10. //
  11. //  This program is distributed in the hope that it will be useful,
  12. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. //  GNU General Public License for more details.
  15. //
  16. //  You should have received a copy of the GNU General Public License
  17. //  along with this program. If not, see <span class="skimlinks-unlinked">http://www.gnu.org/licenses</span>/
  18.  
  19. //Mods by Hockeyrink for figuring out how da hek did Mr. Klein do this? - April 2014
  20.  
  21. //Assign ring levels to appropriate I/O line
  22. #define r1 13
  23. #define r2 12
  24. #define r3 11
  25. #define r4 10
  26. #define r5 9
  27. #define r6 8
  28. #define r7 7
  29. const int ledRings[] = {r1, r2, r3, r4, r5, r6, r7};  //Set me up the list of pins to be used with the LEDs
  30. int ringCount = 7; // Number of elements we're USING in array (0 to 6)
  31.  
  32.  
  33. void setup()
  34. {
  35.    for (int thisRing = 0; thisRing < ringCount; thisRing++) {
  36.       pinMode(ledRings[thisRing], OUTPUT);
  37.    }
  38. }
  39.  
  40. byte brightness(const int led, const int pos) {  //Test each LED for distance from focused position. The further way it is, ramp the brightness down
  41.    switch (abs(led-pos)) {
  42.       case 0:     return 32;               // LED IS the Pos'n? Make it full bright (32/32)
  43.       case 1:     return 16;               // 1 LED away from Pos'n? Make it 1/2 bright (16/32)
  44.       case 2:     return 6;               // 2 LEDs away? 6/32 (19% bright)
  45.       case 3:     return 1;               // 3 LEDs away? 1/32 on (3% bright)
  46.       default:    return 1;               // Even further away? Just make it barely on (3%)
  47.    }
  48. }
  49.  
  50. void pulse_width_modulation(const byte position) {                  //Position is the present focused LED location
  51.    for(byte times=0; times<80; ++times) {                          // Loop "times" higher for a slower action
  52.       for (byte brightLevel=0; brightLevel<32; ++brightLevel) {    // Start of "fake" PWM. Looping thru all 32 levels of
  53.                                                                    //brightness that are available, from off (0) to full on (32)
  54.          for (int led=0; led<ringCount; ++led) {                   //Check each LED against this level of brightness. Supposed to be on?
  55.             digitalWrite(ledRings[led], (brightness(led, position) > brightLevel)); //If tested value > brightLevel, turn it on
  56.          }
  57.       }
  58.    }
  59. }
  60.  
  61. void loop() {
  62.    static int pos=0;
  63.    
  64.    while(pos<7) {
  65.       pulse_width_modulation(pos);
  66.       ++pos;
  67.    }
  68.  
  69.  
  70.    while(pos>0) {
  71.       --pos;
  72.       pulse_width_modulation(pos);
  73.    }
  74.  
  75. }
  76.  
  77. /*
  78. Let’s analyze the sketch.
  79. The underlying idea is that the glowing effect is some kind of cursor that has a position that moves back and forth.
  80. This is what the main loop takes care of.
  81.  
  82. The brightness function will compute the desired brightness of an LED depending on the LED’s number and the position
  83. of the cursor.
  84.  
  85. In between is the pulse_width_modulation. This function will cycle 10 times through 32 phases.
  86. For each phase it will cycle through all LEDs.
  87. Depending on any LED’s desired brightness and the phase it will switch the LED on and off.
  88.  
  89. For example: Suppose the brightness function returns 16 for an LED.
  90. In this case the LED will be on for phase in 0..15 and off for phase in 16..31.
  91. Thus the LED would be on half of the time. Accordingly it will be visibly on but dimmer as an LED that is always on.
  92. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top