NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » Help with some code?
May 13, 2011 by SpaceGhost ![]() |
I've been working on what should be a simple piece of code for several days now, and it's really starting to drive me nuts! This function of the code works - When the pushbutton is pressed LEDs 1 & 2 turn on and stay on until the button is released. LED 3 is off for the duration of the button press also. When the pushbutton is released, LED 1 turns off and LED 3 turns on. LED 2 stays on for a duration of 900 ms., then turns off too... What isn't working, is that I need LED 3 to turn off when LED 2 turns off.
I've tried several ways to get this to happen - but that darned LED 3 always stays on! (though at least I get it to turn off when the button is pressed, which is what I want it to do too). Could someone please explain to me why oh why do these lines -
work to turn off LED 2, but does not turn off LED 3 at the same time??? Dave |
---|---|
May 13, 2011 by bretm ![]() |
LED 3 does go off. And then your loop starts over, and because the button is not pushed it turns LED on again and starts the 900ms delay again, etc. You need to do something like this:
|
May 14, 2011 by SpaceGhost ![]() |
On my way to work, I will try this out when I get home this afternoon... I should have searched the forums for "do nothing", and I might have saved myself a few a hours! Thanks bretm again, for the point in the right direction. Dave |
May 14, 2011 by SpaceGhost ![]() |
Hey, works great - problem solved. This bit of knowledge will most definitely help me out in future projects. Once I put the while statements where they belonged, it actually made sense for me. Then I got rid of that silly "if (a == 0)" statement and tidied up the code a bit. Had to move the LCD instructions into the button state loops too, of course -
This was a good lesson for me, hope that it can help someone else out too. Thanks bretm! Dave |
Please log in to post a reply.
Did you know that the Timer/Counter modules on the microcontroller can be configured to output a PWM (Pulse Width Modulation) signal? Learn more...
|