Advertisement
UnaClocker

Snowman 2013

Nov 2nd, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. // Christmas tree ornament 2013
  2. // Software version 2.1, revised last on November 28th, 2013
  3. // Uses an ATTiny45 at 1MHz to Charlieplex 20 LED's
  4. // Design and program by Brian Schulteis 2013
  5. // www.neonsquirt.com
  6.  
  7. const byte speed=100;
  8. unsigned long timeTrackerOne = 0;
  9.  
  10. // Using the 5 lowest bits of PORT B
  11. // I use binary in the array below because it's
  12. // really clear which bits are set to what.
  13.  
  14. const byte portInOrOut[] = {
  15.   B00010001, B00010001, B00010010, B00010010, B00010100, B00010100,
  16.   B00000101, B00000101, B00000011, B00011000, B00000110, B00011000,
  17.   B00001100, B00001010, B00001100, B00001010, B00001001, B00000011,
  18.   B00001001, B00000110
  19. };
  20.  
  21.  
  22. const byte portOnOrOff[] = {
  23.   B00010000, B00000001, B00000010, B00010000, B00010000, B00000100,
  24.   B00000001, B00000100, B00000010, B00010000, B00000010, B00001000,
  25.   B00000100, B00000010, B00001000, B00001000, B00001000, B00000001,
  26.   B00000001, B00000100
  27. };
  28.  
  29. const byte patternA[] = { //Chaser pattern, an array is simpler than the alternative.
  30.   1,3,5,7,9,11,13,15,17,19,18,16,14,12,10,8,6,4,2,0
  31. };
  32.  
  33. void setup() {
  34.   // PUD Bit MCUCR Register - disable all pull-ups
  35.   MCUCR |= 64;  // Set the 6th bit HIGH
  36. }
  37.  
  38. void lightLED(byte whichOne) {
  39.   PORTB=0;// Turn off all the outputs first, odd bulbs come on at odd times if you don't.
  40.   DDRB = portInOrOut[whichOne]; // Sets pinModes (input or output)
  41.   // the divide by 2 is because
  42.   PORTB = portOnOrOff[whichOne]; // Set pinStates (HIGH or LOW)
  43. }
  44.  
  45. void patternModeA() { // 2 by 2 going down
  46.   for (byte indexA=0; indexA < 20; indexA+=2) {
  47.     // Two bulbs at a time
  48.     timeTrackerOne=millis();
  49.     while ((millis()-timeTrackerOne)<speed) {
  50.       lightLED(indexA);
  51.       delay(1);
  52.       PORTB=0; // Having two arrays of numbers
  53.       lightLED(indexA+1);
  54.       delay(1);
  55.       PORTB=0; // Made the counting simpler
  56.     }
  57.     //PORTB=0;
  58.   }
  59. }
  60.  
  61. void patternModeB() { // Different body parts
  62.   timeTrackerOne=millis();
  63.   while ((millis()-timeTrackerOne)<(speed*5)) {
  64.     for (byte indexA=0; indexA<6; indexA++) { // LED 0 - 5 is the head
  65.       lightLED(indexA);
  66.       delayMicroseconds(8);
  67.       PORTB=0;
  68.     }
  69.   }
  70.   PORTB=0;
  71.   timeTrackerOne=millis();
  72.   while ((millis()-timeTrackerOne)<(speed*5)) {
  73.     for (byte indexA=6; indexA<12; indexA++) { // LED 6-11 is the middle
  74.       lightLED(indexA);
  75.       delayMicroseconds(8);
  76.       PORTB=0;
  77.     }
  78.   }
  79.   PORTB=0;
  80.   timeTrackerOne=millis();
  81.   while ((millis()-timeTrackerOne)<(speed*5)) {
  82.     for (byte indexA=12; indexA<20; indexA++) { // LED 12-20 is the bottom
  83.       lightLED(indexA);
  84.       delayMicroseconds(50);
  85.       PORTB=0;
  86.     }
  87.   }
  88. }
  89.  
  90. void patternModeC() { // Kind of a snake eating it's tail mode
  91.   byte indexA=0;
  92.   byte indexB=0;
  93.   for (indexA=0; indexA<21; indexA++) { // Start with turning them all on, one by one.
  94.     timeTrackerOne=millis();
  95.     while ((millis()-timeTrackerOne)<speed) {
  96.       for (byte indexC=0; indexC<indexA; indexC++) {
  97.         lightLED(patternA[indexC]);
  98.         delayMicroseconds(25);
  99.         PORTB=0;
  100.       }
  101.     }
  102.   }
  103.   for (indexB=0; indexB<indexA; indexB++) { // Start with turning them all on, one by one.
  104.     timeTrackerOne=millis();
  105.     while ((millis()-timeTrackerOne)<speed) {
  106.       for (byte indexC=indexB; indexC<20; indexC++) {
  107.         lightLED(patternA[indexC]);
  108.         delayMicroseconds(25);
  109.         PORTB=0;
  110.       }
  111.     }
  112.   }
  113. }
  114.  
  115.  
  116. void patternModeE() {  // Left/Right Flip flopper
  117.   for (byte l=0; l<2; l++) { // work through the pattern to turn them all on.
  118.     timeTrackerOne=millis();
  119.     while ((millis()-timeTrackerOne)<speed*2) {
  120.       if (l<1) {
  121.         for (byte indexA=1; indexA<20; indexA+=2) { // We need this loop to keep them refreshed (on)
  122.           lightLED(indexA);
  123.           delay(1);
  124.           PORTB=0;
  125.         }
  126.       }
  127.       else {
  128.         for (byte indexA=0; indexA<20; indexA+=2) { // We need this loop to keep them refreshed (on)
  129.           lightLED(indexA);
  130.           delay(1);
  131.           PORTB=0;
  132.         }
  133.       }
  134.     }
  135.   }
  136. }
  137.  
  138. void patternModeF() { // Startup mode - all on
  139.   for (byte indexA=20; indexA>0; indexA--) { // Timing multiplier
  140.     timeTrackerOne=millis();
  141.     while ((millis()-timeTrackerOne)<((speed/7) * indexA)) {
  142.       for (byte indexB=0; indexB<20; indexB++) { // Run through all of the LEDs
  143.         lightLED(indexB);
  144.         delayMicroseconds(30);
  145.         PORTB=0;
  146.       }
  147.     }
  148.     timeTrackerOne=millis();
  149.     while ((millis()-timeTrackerOne)<((speed/7) * indexA)) {
  150.     }
  151.   }
  152. }
  153.  
  154.  
  155. void patternModeG() { // Twin chaser mode
  156.   byte indexB=11;
  157.   for (byte indexA=0; indexA<20; indexA++) {
  158.     timeTrackerOne=millis();
  159.     while ((millis()-timeTrackerOne)<(speed*.5)) {
  160.       lightLED(patternA[indexA]);
  161.       delay(1);
  162.       PORTB=0;
  163.       lightLED(patternA[indexB]);
  164.       delay(1);
  165.       PORTB=0;
  166.     }
  167.     if (indexB==0) {
  168.       indexB=19;
  169.     }
  170.     else indexB--;
  171.     // delay(speed*2);
  172.   }
  173. }
  174.  
  175. void loop() {
  176.   patternModeF(); // All lights, blinking on off faster and faster.
  177.   delay(speed*5);
  178.   patternModeB(); // Body parts
  179.   patternModeB(); // Body parts
  180.   patternModeB(); // Body parts
  181.   delay(speed*5);
  182.   patternModeG(); // Twin Chaser
  183.   patternModeG(); // Twin Chaser
  184.   patternModeG(); // Twin Chaser
  185.   delay(speed*5);
  186.   patternModeA(); // 2 by 2 going down.
  187.   patternModeA(); // 2 by 2 going down.
  188.   patternModeA(); // 2 by 2 going down.
  189.   delay(speed*5);
  190.   patternModeC();
  191.   patternModeC();
  192.   delay(speed*5);  
  193.   patternModeE(); // Flip Flop - Left/Right
  194.   patternModeE(); // Flip Flop - Left/Right
  195.   patternModeE(); // Flip Flop - Left/Right
  196.   patternModeE(); // Flip Flop - Left/Right
  197.   patternModeE(); // Flip Flop - Left/Right
  198.   delay(speed*5);
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement