Advertisement
Antyos

Arc Reactor Prop Code

Jun 28th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.92 KB | None | 0 0
  1. /* == ARC REACTOR PROP USING MSP430 CODE ==
  2.  * By Antyos
  3.  *
  4.  * This is the code for the Arc Reactor prop by Antyos
  5.  * Instructions can be found here: http://www.instructables.com/id/How-to-Make-an-Arc-Reactor-Prop-Using-TI-Msp430
  6.  * This code uses bit shifting to choose which leds have an altered duty cycle to give the illusion that the leds turning in a circle
  7.  *
  8.  */
  9.  
  10. #include <msp430g2553.h>
  11.  
  12. #define BUTTON BIT0 // Button is PX_0 (P2 will be used in this program)
  13.  
  14. int patType = 0; // Light pattern, will be variable for first element of the "lights" array (Base 0)
  15. const int time[2] = {5, 20}; // Delay for duty cycle: on time, off time
  16.  
  17. const int buttonCount = 10; // Delay for how long the button press should be to trigger the special pattern
  18. int buttonTimer = 0; // Value for storing how long the button has been pressed
  19.  
  20. int lightStat; // Value that stores each light's state
  21.  
  22. int lightDat[3][3] = {  // Array for storing all the data for the patterns
  23.     // Stay on lights | Shift | End pattern (last one is 1, all others are 0)
  24.       {0xff,            1,      0x00}// Pattern is solid
  25.       {0x01,            8,      0x00}// Pattern has 1 bright led that goes in a circle
  26.       {0x11,            4,      0x01}   // Pattern has 2 bright leds that go in a circle
  27.  
  28.     /* Variable descriptions:
  29.      * Stay on lights: which lights are the ones that stay on at the beginning of the pattern and will then turn with the rest
  30.      *      0x11 (hex) -> 0001 0001 (binary)   lights 0 and 4 stay on at the beginning and are then shifted one over
  31.      * Shift: How many times the pattern shifts to the right before it needs to be reset
  32.      *      0001 0001 >>SHIFT>> 0010 0010 >>SHIFT>> 0100 0100 >SHIFT>> 1000 1000 >>RESET>> 0001 0001 (repeat)  went through 4 times before needing a reset, so shift = 4
  33.      * End pattern: This value needs to be set to 0x01 for the final pattern, it is used to know when to cycle back to the first pattern
  34.      */
  35.  };
  36.  
  37.  
  38. void delay_ms(int del) { // Function for delay
  39.   while(del--) { // While there is more than one delay left
  40.       __delay_cycles(80); // Equivalent clock cycles for a time
  41.   }
  42. }
  43.  
  44. void delay_flare(unsigned int del) { // More accurate delay for the flare pattern
  45.     while(del--) __delay_cycles(8); //Wait for delay
  46. }
  47.  
  48. void patChange(void) { // Pattern change function, runs when the pattern should be changed
  49.   // If the button is held for a half second or less, move to the next pattern
  50.   if(lightDat[patType][2] & 0x01) patType = 0; // If the selected pattern has reached the last one, then reset the selected pattern to the first one
  51.   else patType ++; // Go to the next pattern
  52.   lightStat = lightDat[patType][0]; // Set the lights to have the next pattern
  53.   //buttonTimer = 0; // Reset buttonTimer
  54. }
  55.  
  56. void patFlare(void) { // Pattern for changing the pattern to a pulsating light -- the unibeam
  57.   // This pattern has a pulsating light where all the lights glow brighter and then switch to dimmer and repeat
  58.   // This pattern does not involve shifting
  59.   float i;
  60.   int j;
  61.   const unsigned int flareDel[2] = {100, 60}; // Values for duty cycle
  62.   // Spin faster and pick up speed and glow brighter
  63.   // Will go 30x, 29x, 28x and so on
  64.   for(i = 30; i > 0; i -= 0.25) {
  65.     for(j = i; j > 0 ; j --) {
  66.       // Duty cycle for lights dimming
  67.       P1OUT = 0xff;  // Turn all lights on
  68.       delay_flare(time[0]*10); // Delay for duty cycle on time
  69.       P1OUT = lightStat; // Keep on only the lights in the light byte dedicated by the pattern
  70.       delay_flare((time[1]*(i/2))/4); // Delay for duty cycle off time
  71.     }
  72.  
  73.     // Shift lights - Check main loop for more detail
  74.     lightStat = lightStat << 1;
  75.     if (lightStat > 255)  lightStat = lightStat >> lightDat[patType][1];
  76.  
  77.   }
  78.  
  79.   i = 60; // Set count to 50 cycles
  80.   P1OUT = 0x00; // Turn off all the lights
  81.   do {
  82.        for(i; i > 7; i --) { // Flash 50 times, stops at 7 so that it doesn't blink faster than the strobe
  83.            for(j=0; j < 2; j ++) { // Flash one time
  84.                P1OUT ^= 0xff; // Toggle lights
  85.                delay_flare(flareDel[j]*50); // Delay
  86.            }
  87.        }
  88.  
  89.        i = 8; // Reset it back to 1 cycle
  90.        if(P2IN & BUTTON) buttonTimer = 0; // If the button is no longer pressed, reset the button timer and return to the main program
  91.   }  while(buttonTimer != 0); // Will continue with flashing the lights, will go forever if the button is held,
  92.  
  93. }
  94.  
  95.  
  96.  
  97. int main(void) {
  98.   WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
  99.  
  100.   P1DIR = 0xff; // Set all Port 1 pins to output
  101.   P2DIR = 0x00; // Set all Port 2 pins to input
  102.  
  103.   P2OUT |= BUTTON; // Set button, reset everything else
  104.   P2REN |= BUTTON; // Set button to pull up
  105.  
  106.   lightStat = lightDat[patType][0]; // Set the lights to be the first pattern
  107.  
  108.   while(1) { // Infinite loop
  109.     // Keep all secondary lights at a lower brightness with a duty cycle
  110.     int i = 0;
  111.     for(i = 33; i > 0 ; i --) {
  112.       // Duty cycle for lights dimming
  113.       P1OUT = 0xff;  // Turn all lights on
  114.       delay_ms(time[0]); // Delay for duty cycle on time
  115.       P1OUT = lightStat; // Keep on only the lights in the light byte dedicated by the pattern
  116.       delay_ms(time[1]); // Delay for duty cycle off time
  117.     }
  118.  
  119.     // Shift lights
  120.     lightStat = lightStat << 1; // Shift lights over one
  121.     if (lightStat > 255)  lightStat = lightStat >> lightDat[patType][1]; // If the mapped registers exceeds the mapped LEDs, then set them back to the default position if the last light goes off, reset the pattern
  122.     // Change the pattern
  123.     if ((P2IN & BUTTON) == 0) { // While the button is pressed
  124.       buttonTimer++; // If the value of P2_0 is high (pressed), add one to the button timer
  125.       while(buttonTimer >= buttonCount) patFlare(); // If the button is held for more than half a second then go to the special pattern
  126.     }
  127.  
  128.     else if(buttonTimer < buttonCount && buttonTimer > 0) {
  129.       patChange(); // If the button isn't pressed and the timer has been triggered but not surpassed, switch patterns
  130.       buttonTimer = 0;
  131.     }
  132.  
  133.   }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement