That won't work, you need at least one more bit of state or data. You need to be able to distinguish between when the button was just pressed, and when the button is pressed then let go.
I would do something like this:
Now you do need to make sure with the above that if the person switches between LONG_OR_SHORT_BUTTON to another button, something appropriate happens.Code:old_data= data; data = get_button(); if(old_data == LONG_OR_SHORT_BUTTON){ if(old_data == data) send_long_option() else send_short_option() } switch(data){ other buttons }
Also the above code assumes that get data is a blocking call on the ADC, and that the ADC has a slow enough polling time. If it doesn't you can do digital averaging to make it work as if it was.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Last edited by King Mir; 12-20-2012 at 11:19 PM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
The acquisition time is to compensate for impedance that slows down charging the "charge hold capacitor", as described in section 7.9 of the data sheet - Not for filtering. The inaccuracies will come from noises on the line - Read here for more info on the noises you need to watch out for http://ww1.microchip.com/downloads/e...tes/00823a.pdf
Fact - Beethoven wrote his first symphony in C
Where are you getting the automatic averaging from?
Fact - Beethoven wrote his first symphony in C
I've modified the program a little. I have one additional function (key_states() that calls for the states for each keys pressed). Both of construct() and key_states() functions are called in main().
Code:main(void) { system_init(); while(1) { if ((key > 205) && (key < 235)) { pressed = 1; } else { pressed = 0; } key_states (); construct(); } } void construct(void) { { if ((key > 205) && (key < 235)) { if (key_state == 0) { if (!long_press) { /* short - Mute function */ } else { /* Long - On/Off function */ long_press = 0; } } } else if ((key > 155) && (key < 173)) { /* button 2 */ } else if ((key > 80) && (key < 141)) { /* button 3 */ } else if ((key > 47) && (key < 78)) { /* button 4*/ } else if ((key >= 0) && (key < 15)) { /* button5 */ } else { /* null*/ } } #endif } void key_states () { switch (key_state) { case 0: { /* initial state */ if (pressed) { count=1; key_state = 1; } break; } case 1: { if (pressed) { count++; if (count > 10) { /* long */ long_press = 1; count = 0; key_state = 0; } } else { if (count < 5) { /* short */ long_press = 0; } else { } count = 0; key_state = 0; } break; } default:; } }
In this recent code, I still get the same error! It just doesnt get into the long press for button 1 (which is marked as ON/OFF function in construct() function)
Could you please help to look into it? I have run simulation on this, where I hard code key = 220 (that replicates the ADC result) to see if the functions are functioning well or not. Through the simulation, it seems to be alright. But once tested, it doesnt seem to be so.