Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* == ARC REACTOR PROP USING MSP430 CODE ==
- * By Antyos
- *
- * This is the code for the Arc Reactor prop by Antyos
- * Instructions can be found here: http://www.instructables.com/id/How-to-Make-an-Arc-Reactor-Prop-Using-TI-Msp430
- * 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
- *
- */
- #include <msp430g2553.h>
- #define BUTTON BIT0 // Button is PX_0 (P2 will be used in this program)
- int patType = 0; // Light pattern, will be variable for first element of the "lights" array (Base 0)
- const int time[2] = {5, 20}; // Delay for duty cycle: on time, off time
- const int buttonCount = 10; // Delay for how long the button press should be to trigger the special pattern
- int buttonTimer = 0; // Value for storing how long the button has been pressed
- int lightStat; // Value that stores each light's state
- int lightDat[3][3] = { // Array for storing all the data for the patterns
- // Stay on lights | Shift | End pattern (last one is 1, all others are 0)
- {0xff, 1, 0x00}, // Pattern is solid
- {0x01, 8, 0x00}, // Pattern has 1 bright led that goes in a circle
- {0x11, 4, 0x01} // Pattern has 2 bright leds that go in a circle
- /* Variable descriptions:
- * Stay on lights: which lights are the ones that stay on at the beginning of the pattern and will then turn with the rest
- * 0x11 (hex) -> 0001 0001 (binary) lights 0 and 4 stay on at the beginning and are then shifted one over
- * Shift: How many times the pattern shifts to the right before it needs to be reset
- * 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
- * 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
- */
- };
- void delay_ms(int del) { // Function for delay
- while(del--) { // While there is more than one delay left
- __delay_cycles(80); // Equivalent clock cycles for a time
- }
- }
- void delay_flare(unsigned int del) { // More accurate delay for the flare pattern
- while(del--) __delay_cycles(8); //Wait for delay
- }
- void patChange(void) { // Pattern change function, runs when the pattern should be changed
- // If the button is held for a half second or less, move to the next pattern
- 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
- else patType ++; // Go to the next pattern
- lightStat = lightDat[patType][0]; // Set the lights to have the next pattern
- //buttonTimer = 0; // Reset buttonTimer
- }
- void patFlare(void) { // Pattern for changing the pattern to a pulsating light -- the unibeam
- // This pattern has a pulsating light where all the lights glow brighter and then switch to dimmer and repeat
- // This pattern does not involve shifting
- float i;
- int j;
- const unsigned int flareDel[2] = {100, 60}; // Values for duty cycle
- // Spin faster and pick up speed and glow brighter
- // Will go 30x, 29x, 28x and so on
- for(i = 30; i > 0; i -= 0.25) {
- for(j = i; j > 0 ; j --) {
- // Duty cycle for lights dimming
- P1OUT = 0xff; // Turn all lights on
- delay_flare(time[0]*10); // Delay for duty cycle on time
- P1OUT = lightStat; // Keep on only the lights in the light byte dedicated by the pattern
- delay_flare((time[1]*(i/2))/4); // Delay for duty cycle off time
- }
- // Shift lights - Check main loop for more detail
- lightStat = lightStat << 1;
- if (lightStat > 255) lightStat = lightStat >> lightDat[patType][1];
- }
- i = 60; // Set count to 50 cycles
- P1OUT = 0x00; // Turn off all the lights
- do {
- for(i; i > 7; i --) { // Flash 50 times, stops at 7 so that it doesn't blink faster than the strobe
- for(j=0; j < 2; j ++) { // Flash one time
- P1OUT ^= 0xff; // Toggle lights
- delay_flare(flareDel[j]*50); // Delay
- }
- }
- i = 8; // Reset it back to 1 cycle
- if(P2IN & BUTTON) buttonTimer = 0; // If the button is no longer pressed, reset the button timer and return to the main program
- } while(buttonTimer != 0); // Will continue with flashing the lights, will go forever if the button is held,
- }
- int main(void) {
- WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
- P1DIR = 0xff; // Set all Port 1 pins to output
- P2DIR = 0x00; // Set all Port 2 pins to input
- P2OUT |= BUTTON; // Set button, reset everything else
- P2REN |= BUTTON; // Set button to pull up
- lightStat = lightDat[patType][0]; // Set the lights to be the first pattern
- while(1) { // Infinite loop
- // Keep all secondary lights at a lower brightness with a duty cycle
- int i = 0;
- for(i = 33; i > 0 ; i --) {
- // Duty cycle for lights dimming
- P1OUT = 0xff; // Turn all lights on
- delay_ms(time[0]); // Delay for duty cycle on time
- P1OUT = lightStat; // Keep on only the lights in the light byte dedicated by the pattern
- delay_ms(time[1]); // Delay for duty cycle off time
- }
- // Shift lights
- lightStat = lightStat << 1; // Shift lights over one
- 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
- // Change the pattern
- if ((P2IN & BUTTON) == 0) { // While the button is pressed
- buttonTimer++; // If the value of P2_0 is high (pressed), add one to the button timer
- while(buttonTimer >= buttonCount) patFlare(); // If the button is held for more than half a second then go to the special pattern
- }
- else if(buttonTimer < buttonCount && buttonTimer > 0) {
- patChange(); // If the button isn't pressed and the timer has been triggered but not surpassed, switch patterns
- buttonTimer = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement