NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Need help with multiple switched inputs for a desired output.
June 15, 2012 by Bamftoy |
Ok, so I'm trying to make it so that a pushbutton will light up 2 LEDs sequentially and turn off the LEDs on the third button press. While, if at ANY point in the program 2 switches are engaged, and the button is pressed, 3 LEDs will light and stay on until the button is released. After the button is released the first 2 LEDs should stay on until the button is pressed to turn them off again. Also while compiling I get these warnings test.c: In function 'main': test.c:56: warning: 'sc' is used uninitialized in this function test.c:31: warning: 'sb' may be used uninitialized in this function Thanks for any help, Josh here's my code:
|
---|---|
June 15, 2012 by pcbolt |
Josh - I've read contrasting information on this subject. Some say all variables will get initiallized to 0 automatically, some say it is best to leave them uninitialized, some say you should explicitly initialize all variables. By initializing, I mean setting them to some value when you declare them. To stop the compiler warning, just change line 30 to:
Same with line 38. The compiler won't warn about the other variables since they get assigned a value before they are used in any equations. "sp" and "sb" are used before they are assigned any value. |
June 15, 2012 by BobaMosfet |
It is a fact, that in any given c compiler, it does not initialize variables to a clean state for you- it simply allocates memory to them, and whatever happened to be in that memory prior to the allocation to the variable, will still be there. You should always initialize your variables to a known state. You should always set pointer variables to NULL when done with them. Same with any deallocations you make- they should be NULLed. This way, if anything attempts to use them, you hit zero-page and it crashes fairly quickly and definitively. Most of the time. Good practice, following these two things will save you loads of problems. BM |
June 15, 2012 by pcbolt |
Boba - I agree. Despite what I have read, I still don't trust the compiler or platform to initialize variables automatically to 0. The Win-AVR manual says otherwise...see This FAQ for a discussion on this subject. |
June 15, 2012 by Bamftoy |
Thanks for the help! Warnings are now gone. Anyone have any suggestions for it to read the 2 switches and 1 pushbutton to goto:
Thanks, josh |
Please log in to post a reply.
Did you know that you can use printf and scanf functions to talk to your computer from your USB NerdKit? Learn more...
|