thomas_crx 0 Posted October 30, 2012 Share Posted October 30, 2012 Hi All:) i have msp430 launchpad with this chip msp430g2553 and what im gonna do is something like this : http://www.thekanes....d-stairs-howto/ Im in college on electronic engineering first year, we working on msp 430 but i dnt know to much about programming, the tutor gave me simple code but i can get this workin. What i have is just the sequence for leds : #include <msp430g2553.h> void delay(unsigned int i); // Function Prototype void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer P1DIR=0xff; //Configure all Port 1 as output P2DIR=0xff; //Configure all Port 2 as output while(1) { P1OUT = 0x01; delay(60000); P1OUT = 0x03; delay(60000); P1OUT = 0x07; delay(60000); P1OUT = 0x0F; delay(60000); P1OUT = 0x1F; delay(60000); P1OUT = 0x3F; delay(60000); P1OUT = 0x7F; delay(60000); P1OUT = 0xFF; delay(60000); P2OUT = 0x01; delay(60000); P2OUT = 0x03; delay(60000); P2OUT = 0x07; delay(60000); P2OUT = 0x0F; delay(60000); P2OUT = 0x1F; delay(60000); P2OUT = 0x3F; delay(60000); P2OUT = 0x7F; delay(60000); P2OUT = 0xFF; delay(60000); delay(65000); delay(65000); delay(65000); delay(65000); delay(65000); P1OUT = 0xFF; delay(60000); P1OUT = 0xFE; delay(60000); P1OUT = 0xFC; delay(60000); P1OUT = 0xF8; delay(60000); P1OUT = 0xF0; delay(60000); P1OUT = 0xE0; delay(60000); P1OUT = 0xC0; delay(60000); P1OUT = 0x80; delay(60000); P1OUT = 0x00; delay(60000); P2OUT = 0xFF; delay(60000); P2OUT = 0xFE; delay(60000); P2OUT = 0xFC; delay(60000); P2OUT = 0xF8; delay(60000); P2OUT = 0xF0; delay(60000); P2OUT = 0xE0; delay(60000); P2OUT = 0xC0; delay(60000); P2OUT = 0x80; delay(60000); P2OUT = 0x00; delay(60000); delay(65000); delay(65000); delay(65000); delay(65000); delay(65000); } } void delay(unsigned int i) { int j; for(j=0;j<5000;j++) { while (i != 0) { i--; } } } i will like to if u can guys and want ofcourse to direct me how to do fade in and out on each led, and maybe start the sequence by push and relased the button. ps i know my msp can drive right now 12 leds and can have 2 buttons/sensors. i will working on it later to do some extension by maybe TLC5940... thanks for repply, sorry for language i come frome poland... Quote Link to post Share on other sites
SirPatrick 35 Posted October 31, 2012 Share Posted October 31, 2012 Alright so you are on the right track currently but we definitely can improve. Before I go into showing you how to fade an LED let me first give you some tips on your code. First lets take a look at your main function. It looks like you are going through each LED and turning it on for a certain amount and then back off. It also looks like your are doing pretty much the same thing for several lines. To clean that up a bit you should think about using some type of loop. I am going to pseudocode the loop and let you try to attempt it. for every LED: set LED high delay N seconds set LED low delay N seconds continue on to next LED So you need to look into "For Loops" and figure out how you can iterate through each LED. Think about trying to store each LED in some type of structure and then loop through that structure. I would look into "Arrays" as well in C. Another note, TI has explicit declarations for the pins on the launchpad so you don't have to know the hex representation of each. For example pin 0 is BIT0, pin1 is BIT, etc. You can specify which port by doing PxOUT, where x is 1 or 2. Okay so now lets take a look at fading an LED. To do that we are going to need to learn about the launchpad timers and about pulse width modulation (PWM). Check out this tutorial on timers. After that you can find more info by a general google search like this. If you have any specific problems just update this post and we will go from there! thomas_crx, jsolarski and GeekDoc 3 Quote Link to post Share on other sites
abecedarian 330 Posted October 31, 2012 Share Posted October 31, 2012 That is cool. I think it would be much better if the LED's switched off after the person passed by. Feel free to ignore me. I have aspirations of fuel injection and haven't yet written my own code. Quote Link to post Share on other sites
RobG 1,891 Posted October 31, 2012 Share Posted October 31, 2012 You should try shift registers, There are many code and hardware examples. jsolarski and thomas_crx 2 Quote Link to post Share on other sites
thomas_crx 0 Posted October 31, 2012 Author Share Posted October 31, 2012 Thanks for ur reply, i will like to ask U about some things, U said that it is better to clean up the code, that's right it is messy :grin: im still learning, but my question is is it not easier to use this sequence what i have already type in? Because I can write 2 sequences First is : when u past sensor bottom : LEDs goes on from bottom to top , and then when u past sensor top led goes off from bottom to top after 5 sec. Second is : when u past sensor top : LEDs goes on from top to bottom , and then when u past sensor bottom led goes off from top to bottom after 5 sec. so i will have to just add 2 sensors and do fade in and out on each led. i was reading about arrays and my other question is P1OUT &= ~BIT0; delay(65000); P1OUT |= BIT0; delay(65000); Is this let say 1 array ? how to use it? could u explain me a little bit ? thanks Quote Link to post Share on other sites
chibiace 46 Posted November 1, 2012 Share Posted November 1, 2012 http://www.arduino.cc/en/Reference/Array Quote Link to post Share on other sites
chibiace 46 Posted November 1, 2012 Share Posted November 1, 2012 how many stairs you looking at lighting? what sensor are you going to use for detection? recommend a time out to switch off the leds if the second sensor is not triggered timers are the way to go for pwm, but you could use some for loops to switch off and on an output with a very small increasing/decreasing delay between Quote Link to post Share on other sites
chibiace 46 Posted November 1, 2012 Share Posted November 1, 2012 P1OUT ^= BIT0; toggle ftw. Quote Link to post Share on other sites
thomas_crx 0 Posted November 1, 2012 Author Share Posted November 1, 2012 how many stairs you looking at lighting? about 16 steps( but for now 12) what sensor are you going to use for detection? fototransistor and IR recommend a time out to switch off the leds if the second sensor is not triggered about 10sec timers are the way to go for pwm, but you could use some for loops to switch off and on an output with a very small increasing/decreasing delay between about 1s thanks for help Quote Link to post Share on other sites
SirPatrick 35 Posted November 1, 2012 Share Posted November 1, 2012 Thomas, It is definitely easier to make use of functions and loop structures rather than hard coding repetitive statements. Even if it wasn't necessarily "easier" you still we need to be able to utilize it down the road as it is an essential fundamental concept to programming of all types. Okay so an array can hold a "list" of things. These things can be anything from numerical constants to strings. Now in order to make use of this data structure we need to think of what we could put in there to make our code better. So first let's ask some question about your code as it is now. Why are you repeating the same line over and over again? Specifically what action is being repeated and what property of this action are we changing. So to me it looks like the action you are repeating is lighting an led for a arbitrary amount of time, and then turning it off. Now the property of this action that you are changing is the specific LED that you are turning on. Ideally we want our array to hold whatever is changing. In your case that is the specific LED So lets make an array of LEDs. unsigned int LEDArray[3] = {BIT0,BIT1,BIT2}; Okay, so now we have an array that hold the significant constants for pin 0,1 and 2. So now we have a structure that we can loop through, also known as iterating. I am going to leave the looping to you for now so you can solidify your understanding on what is actually going on. I will give you psedo code with our new fancy array. For item in LEDArray: turn on LEDArray[currentLED] delay x seconds turn off LEDArray[currentLED] delay x seconds currentLED = next LED in array Okay, so here comes the bonus. See how the above code is somewhat repetitive as well? It turns out that it is a perfect candidate for becoming a function (just like your delay function that you call over and over again). So this function really doesn't need to return anything, right? All you want it to do is blink an LED for a certain amount of time. So the functions paramter is going to be which LED you are blinking. Take a stab and this and we can fix it afterwords! thomas_crx 1 Quote Link to post Share on other sites
RobG 1,891 Posted November 1, 2012 Share Posted November 1, 2012 It is definitely easier to make use of functions and loop structures rather than hard coding repetitive statements. Even if it wasn't necessarily "easier" you still we need to be able to utilize it down the road as it is an essential fundamental concept to programming of all types. Using functions will also add abstraction layer by hiding the implementation details. For example you can have startAnimation(char direction), stopAnimation(), updateLEDs(), pwmCycle(char direction), etc. In your first project, implementation of updateLEDs() would look at an array and update MSP's pins. Once you move to shift registers, all you need to do is change updateLEDs() to send array to shift registers via SPI. No other changes (other than configuration) would be necessary. Quote Link to post Share on other sites
cde 334 Posted November 4, 2012 Share Posted November 4, 2012 Actually, the code, and the video, shows that they are turning a led on, waiting on the delay, then turning on the next, then waiting a few delays before turning them back off, in reverse. At one point, all the Leds are on. The code would need two separate for loops, or a while loop with a conditional variable and inverse. Pseudo code int count = 0; Boolean done = 0; boolean reverse = 0; while (done = 0) if reverse = 0 tempvariable = tempvariable << 1; // (bit shift, then add one to enable the next led) tempvariable = tempvariable + 1; led port = tempvariable; count++; if count = 8 then reverse = 1; else if reverse = 1 tempvariable = tempvariable << 1; //(Bit shift without adding one, taking advantage of the bitshift operation to disable the last bit) led port = tempvariable; count--; if count = 0 then done = 1; loop; Quote Link to post Share on other sites
thomas_crx 0 Posted November 5, 2012 Author Share Posted November 5, 2012 sorry for not repply, and no interest for a few days, i had car accident and i was in hospital few times to do tests, xray and things like that... I'll back at weekend, thanks for interest and examples Quote Link to post Share on other sites
SirPatrick 35 Posted November 5, 2012 Share Posted November 5, 2012 sorry for not repply, and no interest for a few days, i had car accident and i was in hospital few times to do tests, xray and things like that... I'll back at weekend, thanks for interest and examples Oh wow! Best of luck on your recovery. Look forward to helping you more on this down the line. thomas_crx 1 Quote Link to post Share on other sites
kylej1050 27 Posted November 9, 2012 Share Posted November 9, 2012 Couple of ideas for you. The TLC5940 will control 16 leds with more current without needing discrete transistors for the individual LEDs. Makes the PWM code a bit easier as well. I've got some .5W 8mm wide angle leds that take 100mA, perfect for dim lighting and matches well with the 5940. I think it would be neat to play with IR leds and CdS cells to try and 'track' people on the stairs for lighting with a person. If you were to use a 5940 for led driving it would free up a ton of pins for analog reading. Have the IR led shine outward and the CdS cell would read reflection off the wall. A users foot/clothes will either absorb or reflect the IR light better giving you a peak or dip that could be used to see where they are on the stairs. If you got really into it you could have it detect if a person falls down the stairs(multiple steps triggered at the same time in short succession) and trigger an alarm. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.