Jump to content

First push on, second push off


sparx
 Share

Recommended Posts

Hi all

Am trying to write some basic code to handle buttons, there are 41 in all, I'm starting with button 7. What I want it to do is to light an LED on a DOut when pushed first and if pushed again, put the LED out.

Am using a long case statement, here is the code for button 7:

case 7:

if ( st40 == 0){

st40=1;

MIOS32_DOUT_PinSet(40, 1);

MIOS32_LCD_CursorSet(0, 2); // X, Y

MIOS32_LCD_PrintFormattedString("Status %d",st40);

MIOS32_LCD_CursorSet(0, 3); // X, Y

MIOS32_LCD_PrintFormattedString("Button 7 pressed on ");

}

else if (st40 == 1){

st40=0;

MIOS32_DOUT_PinSet(40,0);

MIOS32_LCD_CursorSet(0, 2); // X, Y

MIOS32_LCD_PrintFormattedString("Status %d",st40);

MIOS32_LCD_CursorSet(0, 3); // X, Y

MIOS32_LCD_PrintFormattedString("Button 7 pressed off");

}

break;

What happens is that the LED is lit only whilst the button is pressed, when released it goes off. Although sometimes it stays on forever....

Can't see whats wrong, so anyone got any ideas?

Thanks

S

Link to comment
Share on other sites

There is some code missing here - that is the part in which you detect if a button is pushed. From your description I would suspect that you jump to your code above both when the button is pushed and when it is released (both events are detected by MIOS). Try to change that!

Link to comment
Share on other sites

Thanks for the replies.

Just after posting, I realised that MIOS detects on and off, tried lots of things, but found that adding:

if (pin_value==0){

break;

as the first entry in the case statement did the trick.

There are probably more elegant solutions to this, if any C gurus could suggest some it would be good.

At the moment I am only trying to get a feel for what can be done and also remembering my C, which is coming back, slowly.....

Thanks again.

S

Link to comment
Share on other sites

In this code I saved some RAM by storing the toggle state (which you are storing in "st40") in individual bits of a byte.

In order to store 128 flags, I'm using 16 bytes in an array...

However, as long as you don't need more than 32 flags, the implementation could also be done with the "u32" variable type:

Declare it globally as:


u32 toggle_flags;
[/code] Assumed, that the flag number which should be changed is stored in the variable "flag" (or "pin"), you can now set the flag with:
[code]
toggle_flags |= (1 << flag);
and clear it with:

toggle_flags &= ~(1 << flag);
[/code] and you can use following expression to check if the flag is set:
[code]
if( toggle_flags & (1 << flag) ) {
// flag set
} else {
// flag cleared
}
or if you want to store the result, write:

u8 flag_set = (toggle_flags & (1 << flag)) ? 1 : 0;
[/code]

Best Regards, Thorsten.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...