Advertisement
UnaClocker

Valentine's Heart

Jan 29th, 2013
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.89 KB | None | 0 0
  1. // Valentine's Day Heart Shaped PCB
  2. // Uses an ATTiny45 at 1MHz to Charlieplex 20 LED's
  3. // Design and program by Brian Schulteis 2013
  4. // www.neonsquirt.com
  5.  
  6. // Using the 5 lowest bits of PORT B
  7. // I use binary in the array below because it's
  8. // really clear which bits are set to what.
  9. const byte portInOrOut[] = { // 20 LEDs, 10 different pinModes to get them lit
  10.   B00000011, B00000110, B00001100, B00011000, B00000101,
  11.   B00001010, B00010100, B00001001, B00010010, B00010001
  12. };
  13.  
  14. const byte portOnOrOff[] = { // 20 LEDs, 20 different states to get them lit
  15.   B00000010, B00000001, B00000100, B00000010, B00001000,    
  16.   B00000100, B00010000, B00001000, B00000100, B00000001,
  17.   B00001000, B00000010, B00010000, B00000100, B00001000,
  18.   B00000001, B00010000, B00000010, B00010000, B00000001
  19. };
  20.  
  21. // Each LED has a number, thes
  22. const byte sequenceA[] = {
  23.   1,  2,  4,  3,  9, 10, 15, 16, 19, 13,  // Left Side Downward
  24.   20, 12, 19, 16, 14, 10,  9,  3,  4,  2, // Left Side Upward
  25. };
  26.  
  27. const byte sequenceB[] = {
  28.   1,  5,  6,  7,  8, 11, 14, 17, 18, 12, // Right Side Downward
  29.   20, 13, 18, 17, 15, 11,  8,  7,  6,  5, // Right Side Upward
  30. };
  31.  
  32. const byte sequenceC[] = {
  33.   10,  9, 15, 3, 16, 4, 19, 2, 13, 1,  // Left to right
  34.   20,  5, 12, 6, 18, 7, 17, 8, 14, 11, 11 // or vice versa
  35. };
  36.  
  37. const byte sequenceD[] = {
  38.   20, 13, 12, 19, 18, 16, 17, 15, 14, 10, // Up/down
  39.   11,  9,  8,  3,  7,  4,  6,  2,  5,  1  // or down/up
  40. };
  41.  
  42. const byte sequenceE[] = {
  43.   1, 6, 8, 14, 18, 20, 19, 15, 9, 4, // Every other
  44.   5, 7, 11, 17, 12, 13, 16, 10, 3, 2
  45. };
  46.  
  47. const byte sequenceF[] = {
  48.   1,5,6,7,8,11,14,17,18,12,20,13,19,16,15,10,9,3,4,2,1
  49. };
  50.  
  51. const int speed=200; // int because it's quite likely I might increase this past 255
  52. byte i=0;
  53. unsigned long timeTrackerOne = 0;
  54.  
  55. void setup() {
  56.   // PUD Bit MCUCR Register - disable all pull-ups
  57.   MCUCR |= 64;  // Set the 6th bit HIGH
  58. }
  59.  
  60. void lightLED(byte whichOne) {
  61.   whichOne--; // Array starts at 0, LED numbering starts at 1.
  62.   PORTB=0;
  63.   DDRB = portInOrOut[whichOne/2]; // Sets pinModes (input or output)
  64.   // the divide by 2 is because
  65.   PORTB = portOnOrOff[whichOne]; // Set pinStates (HIGH or LOW)
  66. }
  67.  
  68. void patternModeA() {
  69.   // Two bulbs at a time
  70.   for (int indexer=0; indexer < 20; indexer++) {
  71.   timeTrackerOne=millis();
  72.   while ((millis()-timeTrackerOne)<speed) {
  73.     lightLED(sequenceA[indexer]); // Having two arrays of numbers
  74.     lightLED(sequenceB[indexer]); // Made the counting simpler
  75.   }
  76.   PORTB=0;
  77. }
  78. }
  79.  
  80. void patternModeB() {
  81.   // First, we'll turn them all on from left to right
  82.   for (byte l=0; l<20; l=l+2) { // work through the pattern to turn them all on.
  83.     timeTrackerOne=millis();
  84.     while ((millis()-timeTrackerOne)<speed) {
  85.       for (byte r=0; r<=l; r++) { // We need this loop to keep them refreshed (on)
  86.         lightLED(sequenceC[r]);  
  87.       }
  88.     }
  89.   }
  90. }
  91.  
  92. void patternModeC() {
  93.   // Next, we'll turn them all off from top to bottom
  94.   for (byte l=20; l>0; l=l-2) { // work through the pattern to turn them all on.
  95.     timeTrackerOne=millis();
  96.     while ((millis()-timeTrackerOne)<speed) {
  97.       for (byte r=0; r<=l; r++) { // We need this loop to keep them refreshed (on)
  98.         lightLED(sequenceD[r]);  
  99.       }
  100.     }
  101.   }
  102.   lightLED(20);
  103.   delay(speed);
  104.   PORTB=0;
  105.   delay(speed);
  106. }
  107.  
  108. void patternModeD() {
  109.   // Next, we'll turn them all on from right to left
  110.   PORTB=0;
  111.   for (byte l=21; l>=1; l--) { // work through the pattern to turn them all on.
  112.     timeTrackerOne=millis();
  113.     while ((millis()-timeTrackerOne)<speed) {
  114.       for (byte r=19; r>=l; r--) { // We need this loop to keep them refreshed (on)
  115.         lightLED(sequenceC[r]);  
  116.       }
  117.     }
  118.     if (l>1) l--;
  119.   }
  120.   timeTrackerOne=millis();
  121.   while ((millis()-timeTrackerOne)<speed) {
  122.     for (byte r=0; r<19; r++) { // We need this loop to keep them refreshed (on)
  123.       lightLED(r);  
  124.     }
  125. }
  126. }
  127.  
  128. void patternModeE() {
  129.   // Finally, we'll turn them all off from bottom to top
  130.   for (byte l=0; l<20; l++) { // work through the pattern to turn them all on.
  131.     timeTrackerOne=millis();
  132.     while ((millis()-timeTrackerOne)<speed) {
  133.       for (byte r=l; r<=20; r++) { // We need this loop to keep them refreshed (on)
  134.         lightLED(sequenceD[r]);
  135. //        if ((r<19) and (r>0)) {
  136. //        lightLED(sequenceD[r+1]);
  137. //        }  
  138.       }
  139.     }
  140.     if ((l<19) and (l>0)) l++;
  141.   }
  142. }
  143.  
  144. void patternModeF() { // Alternate Red or white
  145.   for (byte r=0; r<20; r++) { // blink 20 times
  146.     timeTrackerOne=millis();
  147.     while ((millis()-timeTrackerOne)<speed) {
  148.       for (byte l=0; l<10; l++) { // We need this loop to keep them refreshed (on)
  149.         lightLED(sequenceE[l]);  
  150.       }
  151.     }
  152.     delay(speed/2);
  153.     timeTrackerOne=millis();
  154.     while ((millis()-timeTrackerOne)<speed) {
  155.       for (byte l=10; l<20; l++) { // We need this loop to keep them refreshed (on)
  156.         lightLED(sequenceE[l]);  
  157.       }
  158.     }
  159.   }
  160. }
  161.  
  162. void patternModeG() { // Basic chaser mode
  163.   for (int hi=0; hi<21; hi++) { // Start with a clockwise rotation
  164.     timeTrackerOne=millis();
  165.     while ((millis()-timeTrackerOne)<speed) {
  166.       lightLED(sequenceF[hi]);
  167.     }
  168.     PORTB=0;
  169.   }
  170.   delay(speed*2);
  171.   for (int hi=20; hi>=0; hi--) { // End with a CCW rotation
  172.     timeTrackerOne=millis();
  173.     while ((millis()-timeTrackerOne)<speed) {
  174.       lightLED(sequenceF[hi]);
  175.     }
  176.     PORTB=0;
  177.   }
  178.   delay(speed*2);
  179. }
  180.  
  181. void loop() {
  182.   for (byte l=0; l<3; l++) { // 3 iterations of the up/down pattern
  183.     patternModeA();
  184.   }
  185.   for (byte l=0; l<3; l++) { // 3 iterations of the chaser pattern
  186.     patternModeG();
  187.   }
  188.   patternModeB(); // Start off, go left to right turning on
  189.   patternModeC(); // Start on, go top to bottom turning off
  190.   patternModeD(); // Start off, go right to left turn on
  191.   patternModeE(); // Start on, go bottom to top turning off
  192.   PORTB=0;
  193.   delay(speed);
  194.   patternModeF();
  195.   PORTB=0;
  196.   delay(speed);
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement