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
First push on, second push off
Started by
sparx
, Oct 29 2011 18:57
5 replies to this topic
#1
Posted 29 October 2011 - 18:57
#2
Posted 29 October 2011 - 20:15
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!
#3
Posted 29 October 2011 - 20:23
use a conditional based on pin state to only trigger when you press or release the button.
#4
Posted 30 October 2011 - 12:24
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
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
#5
Posted 31 October 2011 - 00:17
Hi,
May be I am wrong.
In the Mios32 based Midio128_v3 application is included toggle button option too :
http://svnmios.midibox.org/filedetails.php?repname=svn.mios32&path=%2Ftrunk%2Fapps%2Fcontrollers%2Fmidio128_v3%2Fsrc%2Fmidio_din.c
Regards,
Janis
May be I am wrong.
In the Mios32 based Midio128_v3 application is included toggle button option too :
http://svnmios.midibox.org/filedetails.php?repname=svn.mios32&path=%2Ftrunk%2Fapps%2Fcontrollers%2Fmidio128_v3%2Fsrc%2Fmidio_din.c
Regards,
Janis
#6
Posted 31 October 2011 - 16:34
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:
Assumed, that the flag number which should be changed is stored in the variable "flag" (or "pin"), you can now set the flag with:
and clear it with:
and you can use following expression to check if the flag is set:
or if you want to store the result, write:
Best Regards, Thorsten.
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;
Assumed, that the flag number which should be changed is stored in the variable "flag" (or "pin"), you can now set the flag with:
toggle_flags |= (1 << flag);
and clear it with:
toggle_flags &= ~(1 << flag);
and you can use following expression to check if the flag is set:
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;
Best Regards, Thorsten.
Buy TK a Beer Disclaimer: buying TK a beer gets you absolutely nothing in return likesuchas firmware enhancements, technical advices and MIDIbox troubleshooting assistance.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users




