Advertisement
dlwestab

Prop Circuit for Attiny45 *Arduino

Nov 29th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. Cortana Arduino Code for the ATTiny45 by Dustin Westaby
  4.  
  5. Version History:
  6.  2/13/11 Initial Draft and test circuit
  7.  2/14/11 Updated Comments, re-arranged structure
  8.  9/17/11 Timer Off Code Added
  9.  9/27/12 converted to arduino, expanded animations (Ryuuzaki Julio)
  10. 11/29/12 cleaned up and rewrote logic
  11.  
  12. Ouputs:
  13.  pin    label   connections
  14.   2     PB3 =   LED
  15.   3     PB4 =   LED
  16.   5     PB0 =   LED
  17.   6     PB1 =   LED
  18.   7     PB2 =   LED
  19. */
  20.  
  21. #include <util/delay.h>
  22.  
  23. //arduino specific digital pin numbers
  24. int myPins[6] = {2,3,4,1,0};
  25.  
  26. //performs subtraction then ensures positive result
  27. #define ABS_SUB(a, b) ((a) < (b)? ((b) - (a)): ((a) - (b)))
  28.  
  29. /* -------------------- */
  30. /* Helper Functions     */
  31. /* -------------------- */
  32.  
  33. // the setup routine runs once when you press reset:
  34. void setup()
  35. {
  36.    int i;
  37.  
  38.    // initialize the digital pin as an output.
  39.    for (i=0;i<=4;i++)
  40.    {
  41.       pinMode(myPins[i], OUTPUT);
  42.    }
  43.  
  44. }
  45.  
  46. void delay_ms(uint16_t ms)
  47. {
  48.    while ( ms )
  49.    {
  50.       _delay_ms(1);
  51.       ms--;
  52.    }
  53. }
  54.  
  55. void delay_us(uint16_t us)
  56. {
  57.    while ( us )
  58.    {
  59.       _delay_us(1);
  60.       us--;
  61.    }
  62. }
  63.  
  64. /* -------------------- */
  65. /* Animation Functions  */
  66. /* -------------------- */
  67.  
  68. void blinkBlink(int delayTime, int MaxFlickr)
  69. {
  70.    int i, RandomFlickrAmmount;
  71.  
  72.    //randomize number of blinks and delay to help blinking look more alive
  73.    RandomFlickrAmmount = random(1,MaxFlickr);
  74.  
  75.    for (i = 0; i <= RandomFlickrAmmount; i++)
  76.    {
  77.       //Turn ON all LEDs 01234
  78.       PORTB = 0b00011111;
  79.       delay(delayTime - RandomFlickrAmmount);
  80.  
  81.       //Turn OFF all LEDs 01234
  82.       PORTB = 0b00000000;
  83.       delay(delayTime - RandomFlickrAmmount);
  84.    }
  85.  
  86. } //end blink function
  87.  
  88. void spinSpin(int count_delay)
  89. {
  90.    int i;
  91.    int repeat           = random(1,3)*5;    //random number of spins times 5 LEDs
  92.    int circle_direction = random(0,1);      //random selection of clockwise or counterclockwise animations
  93.    int pin_to_on  = random(0,4);            //random starting LED position for animation (on)
  94.    int pin_to_off = random(0,4);            //random starting LED position for animation (off)
  95.  
  96.    //loop ends after random number of spins
  97.    for (i = 0; i <= repeat; i++)
  98.    {
  99.       if (circle_direction == 1)
  100.       {
  101.          //each animation section turns off one LED and turns on one LED
  102.          digitalWrite(myPins[pin_to_on++], HIGH);
  103.          digitalWrite(myPins[pin_to_off++], LOW);
  104.       }
  105.       else
  106.       {
  107.          //each animation section turns off one LED and turns on one LED
  108.          digitalWrite(myPins[pin_to_on--], HIGH);
  109.          digitalWrite(myPins[pin_to_off--], LOW);
  110.       }
  111.  
  112.       //delay before next spin animation
  113.       delay(count_delay);
  114.  
  115.       //the following overrun check works because circle_direction can only be 0 or 1.
  116.       //When circle_direction is 0, the pin_to_on pin_to_off are decremented, down to 0.  The overrun check compares to 0, then sets to 4.
  117.       //When circle_direction is 1, the pin_to_on pin_to_off are incremented, up to 4.  The overrun check compares to 4, then sets 0.
  118.       if (pin_to_on == circle_direction*4)
  119.       {
  120.          pin_to_on = ABS_SUB(circle_direction*4,4);
  121.       }
  122.       if (pin_to_off == circle_direction*4)
  123.       {
  124.          pin_to_off = ABS_SUB(circle_direction*4,4);
  125.  
  126.          //one rotation complete
  127.          //speed up the spinning, down to 16
  128.          if (count_delay > 16)
  129.          {
  130.             count_delay = count_delay - 2;
  131.          }
  132.       }
  133.  
  134.    } //end loop
  135.  
  136. } //end spin function
  137.  
  138. /* -------------------- */
  139. /* Main Function        */
  140. /* -------------------- */
  141.  
  142. void loop()
  143. {
  144.    int i = 0;
  145.    int count_delay, e, repeat;
  146.    int time_on, time_off, max_value, min_value, rate_of_change;
  147.    boolean fade_direction;
  148.    int Chances_of_Flickr;
  149.    int Chances_of_Spin;
  150.    int X_Loops;
  151.    int randomInt;
  152.  
  153.    /* Set output pins */
  154.    DDRB = 0b00011111;
  155.  
  156.    /* ---------------------------------------------------------------- */
  157.    /* All OFF Short */
  158.    /* ---------------------------------------------------------------- */
  159.  
  160.    count_delay = 20; //Delay starting point
  161.    for (i = 0; i <= 4; i++)
  162.    {
  163.       digitalWrite(myPins[i], LOW);  //turn off each of the 4 LEDs in sequence
  164.       delay(count_delay*2 + i*20);   //delay increases after each iteration
  165.    }
  166.  
  167.    /* ---------------------------------------------------------------- */
  168.    /* Spin Circle (single runner) */
  169.    /* ---------------------------------------------------------------- */
  170.  
  171.    repeat = 8;       //Number of spins
  172.    count_delay = 60; //Delay between circle movement in ms
  173.  
  174.    for(e = 0; e <= repeat; e++)
  175.    {
  176.       for (i = 0; i <= 4; i++)
  177.       {
  178.          digitalWrite(myPins[i], HIGH);
  179.          delay(count_delay);
  180.          digitalWrite(myPins[i], LOW);
  181.       }
  182.  
  183.       if (count_delay >= 6)
  184.       {
  185.          count_delay = count_delay - 6;
  186.       }
  187.    }
  188.  
  189.    /* ---------------------------------------------------------------- */
  190.    /* Spin Circle (chased runner) */
  191.    /* ---------------------------------------------------------------- */
  192.  
  193.    spinSpin(16);  //spin with constant speed of 16ms
  194.  
  195.    /* ---------------------------------------------------------------- */
  196.    /* All ON Long */
  197.    /* ---------------------------------------------------------------- */
  198.  
  199.    count_delay = 20; //Delay starting point
  200.  
  201.    for (i = 0; i <= 4; i++)
  202.    {
  203.       digitalWrite(myPins[i], HIGH);  //turn on each of the 4 LEDs in sequence
  204.       delay(count_delay*2 + i*20);    //delay increases after each iteration
  205.    }
  206.    delay(200);
  207.  
  208.    /* ---------------------------------------------------------------- */
  209.    /* Blink Blink */
  210.    /* ---------------------------------------------------------------- */
  211.  
  212.    blinkBlink(50,5);
  213.    delay_ms(100);
  214.    blinkBlink(50,2);
  215.    delay(200);
  216.  
  217.    /* ---------------------------------------------------------------- */
  218.    /* Fade In and Out Continuous (Software PWM) */
  219.    /* ---------------------------------------------------------------- */
  220.  
  221.    max_value = 400;           //Max for LEDs ON in us
  222.    min_value = 3;             //Max for LEDs OFF in us
  223.    time_on  = min_value;      //Set Starting Time ON to min
  224.    time_off = max_value;      //Set Starting Time OFF to max
  225.    rate_of_change = 1;        //This is the speed that the fade goes between min and max
  226.    fade_direction = false;    //Direction is defined for the direction of the fade (in or out)
  227.    Chances_of_Flickr = 2;     //The breathing animation has 2 in X_Loops chances of flickering.
  228.    Chances_of_Spin   = 1;     //The breathing animation has 1 in X_Loops chances of Spining.
  229.    X_Loops = 3000;
  230.  
  231.    while(true) // Repeat Forever
  232.    {
  233.  
  234.       //Turn ON all LEDs 01234
  235.       PORTB = 0b00011111;
  236.       delay_us(time_on);
  237.  
  238.       //Turn OFF all LEDs 01234
  239.       PORTB = 0b00000000;
  240.       delay_us(time_off);
  241.  
  242.       if (fade_direction==true)
  243.       {
  244.          //In the UP direction the Time ON increases while the Time OFF decreases.
  245.          //The result is that the LEDs get brighter
  246.          time_on  += rate_of_change;
  247.          time_off -= rate_of_change;
  248.  
  249.          //When the end of the fade is reached, switch directions
  250.          if (time_on>max_value)
  251.          {
  252.             fade_direction = false;
  253.          }
  254.       }
  255.       else
  256.       {
  257.          //In the DOWN direction the Time ON decreases while the Time OFF increases.
  258.          //The result is that the LEDs get dimmer
  259.          time_on  -= rate_of_change;
  260.          time_off += rate_of_change;
  261.  
  262.          //When the end of the fade is reached, switch directions
  263.          if (time_on <= min_value)
  264.          {
  265.             fade_direction=true;
  266.          }
  267.       }
  268.  
  269.       //Throughout the sequence, random chance of blinking or spinning instead of normal fading
  270.       randomInt= random(1, X_Loops);
  271.       if (randomInt <= Chances_of_Flickr)
  272.       {
  273.          blinkBlink(15, 4);
  274.       }
  275.  
  276.       if (randomInt >= X_Loops-Chances_of_Spin)
  277.       {
  278.          spinSpin(30);  //spin with increasing speed, from 20 to 16ms
  279.       }
  280.  
  281.    } //end inf loop
  282.  
  283. } // end main loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement