Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Christmas tree ornament 2013
- // Software version 2.1, revised last on November 28th, 2013
- // Uses an ATTiny45 at 1MHz to Charlieplex 20 LED's
- // Design and program by Brian Schulteis 2013
- // www.neonsquirt.com
- const byte speed=100;
- unsigned long timeTrackerOne = 0;
- // Using the 5 lowest bits of PORT B
- // I use binary in the array below because it's
- // really clear which bits are set to what.
- const byte portInOrOut[] = {
- B00010001, B00010001, B00010010, B00010010, B00010100, B00010100,
- B00000101, B00000101, B00000011, B00011000, B00000110, B00011000,
- B00001100, B00001010, B00001100, B00001010, B00001001, B00000011,
- B00001001, B00000110
- };
- const byte portOnOrOff[] = {
- B00010000, B00000001, B00000010, B00010000, B00010000, B00000100,
- B00000001, B00000100, B00000010, B00010000, B00000010, B00001000,
- B00000100, B00000010, B00001000, B00001000, B00001000, B00000001,
- B00000001, B00000100
- };
- const byte patternA[] = { //Chaser pattern, an array is simpler than the alternative.
- 1,3,5,7,9,11,13,15,17,19,18,16,14,12,10,8,6,4,2,0
- };
- void setup() {
- // PUD Bit MCUCR Register - disable all pull-ups
- MCUCR |= 64; // Set the 6th bit HIGH
- }
- void lightLED(byte whichOne) {
- PORTB=0;// Turn off all the outputs first, odd bulbs come on at odd times if you don't.
- DDRB = portInOrOut[whichOne]; // Sets pinModes (input or output)
- // the divide by 2 is because
- PORTB = portOnOrOff[whichOne]; // Set pinStates (HIGH or LOW)
- }
- void patternModeA() { // 2 by 2 going down
- for (byte indexA=0; indexA < 20; indexA+=2) {
- // Two bulbs at a time
- timeTrackerOne=millis();
- while ((millis()-timeTrackerOne)<speed) {
- lightLED(indexA);
- delay(1);
- PORTB=0; // Having two arrays of numbers
- lightLED(indexA+1);
- delay(1);
- PORTB=0; // Made the counting simpler
- }
- //PORTB=0;
- }
- }
- void patternModeB() { // Different body parts
- timeTrackerOne=millis();
- while ((millis()-timeTrackerOne)<(speed*5)) {
- for (byte indexA=0; indexA<6; indexA++) { // LED 0 - 5 is the head
- lightLED(indexA);
- delayMicroseconds(8);
- PORTB=0;
- }
- }
- PORTB=0;
- timeTrackerOne=millis();
- while ((millis()-timeTrackerOne)<(speed*5)) {
- for (byte indexA=6; indexA<12; indexA++) { // LED 6-11 is the middle
- lightLED(indexA);
- delayMicroseconds(8);
- PORTB=0;
- }
- }
- PORTB=0;
- timeTrackerOne=millis();
- while ((millis()-timeTrackerOne)<(speed*5)) {
- for (byte indexA=12; indexA<20; indexA++) { // LED 12-20 is the bottom
- lightLED(indexA);
- delayMicroseconds(50);
- PORTB=0;
- }
- }
- }
- void patternModeC() { // Kind of a snake eating it's tail mode
- byte indexA=0;
- byte indexB=0;
- for (indexA=0; indexA<21; indexA++) { // Start with turning them all on, one by one.
- timeTrackerOne=millis();
- while ((millis()-timeTrackerOne)<speed) {
- for (byte indexC=0; indexC<indexA; indexC++) {
- lightLED(patternA[indexC]);
- delayMicroseconds(25);
- PORTB=0;
- }
- }
- }
- for (indexB=0; indexB<indexA; indexB++) { // Start with turning them all on, one by one.
- timeTrackerOne=millis();
- while ((millis()-timeTrackerOne)<speed) {
- for (byte indexC=indexB; indexC<20; indexC++) {
- lightLED(patternA[indexC]);
- delayMicroseconds(25);
- PORTB=0;
- }
- }
- }
- }
- void patternModeE() { // Left/Right Flip flopper
- for (byte l=0; l<2; l++) { // work through the pattern to turn them all on.
- timeTrackerOne=millis();
- while ((millis()-timeTrackerOne)<speed*2) {
- if (l<1) {
- for (byte indexA=1; indexA<20; indexA+=2) { // We need this loop to keep them refreshed (on)
- lightLED(indexA);
- delay(1);
- PORTB=0;
- }
- }
- else {
- for (byte indexA=0; indexA<20; indexA+=2) { // We need this loop to keep them refreshed (on)
- lightLED(indexA);
- delay(1);
- PORTB=0;
- }
- }
- }
- }
- }
- void patternModeF() { // Startup mode - all on
- for (byte indexA=20; indexA>0; indexA--) { // Timing multiplier
- timeTrackerOne=millis();
- while ((millis()-timeTrackerOne)<((speed/7) * indexA)) {
- for (byte indexB=0; indexB<20; indexB++) { // Run through all of the LEDs
- lightLED(indexB);
- delayMicroseconds(30);
- PORTB=0;
- }
- }
- timeTrackerOne=millis();
- while ((millis()-timeTrackerOne)<((speed/7) * indexA)) {
- }
- }
- }
- void patternModeG() { // Twin chaser mode
- byte indexB=11;
- for (byte indexA=0; indexA<20; indexA++) {
- timeTrackerOne=millis();
- while ((millis()-timeTrackerOne)<(speed*.5)) {
- lightLED(patternA[indexA]);
- delay(1);
- PORTB=0;
- lightLED(patternA[indexB]);
- delay(1);
- PORTB=0;
- }
- if (indexB==0) {
- indexB=19;
- }
- else indexB--;
- // delay(speed*2);
- }
- }
- void loop() {
- patternModeF(); // All lights, blinking on off faster and faster.
- delay(speed*5);
- patternModeB(); // Body parts
- patternModeB(); // Body parts
- patternModeB(); // Body parts
- delay(speed*5);
- patternModeG(); // Twin Chaser
- patternModeG(); // Twin Chaser
- patternModeG(); // Twin Chaser
- delay(speed*5);
- patternModeA(); // 2 by 2 going down.
- patternModeA(); // 2 by 2 going down.
- patternModeA(); // 2 by 2 going down.
- delay(speed*5);
- patternModeC();
- patternModeC();
- delay(speed*5);
- patternModeE(); // Flip Flop - Left/Right
- patternModeE(); // Flip Flop - Left/Right
- patternModeE(); // Flip Flop - Left/Right
- patternModeE(); // Flip Flop - Left/Right
- patternModeE(); // Flip Flop - Left/Right
- delay(speed*5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement