View previous topic :: View next topic |
Author |
Message |
emazzu630
Joined: 14 Aug 2018 Posts: 20 Location: Ouro Verde - SC
|
Use delay_ms () after the button is released |
Posted: Sun Aug 18, 2019 12:27 am |
|
|
Hello, I need to implement a delay that should count after the button is released,
I've never seen anything of this nature, and the courses always deal with simple examples
and routine. Any tips would be welcome.
In the example below, the delay occurs while the button is held down.
Code: |
void incrementa() {
if ((up == 1)&&(j<=255)) {
j++;
delay_ms(100);
|
What I need is that after releasing the button, wait a while and change functions.
Last edited by emazzu630 on Sun Aug 18, 2019 7:27 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9232 Location: Greensville,Ontario
|
|
Posted: Sun Aug 18, 2019 4:24 am |
|
|
hmm, check the manual, in the 'common Q&A section,...
this title...
Quote: | How do I wait only a specified time for a button press? |
It may do what you want, or be a starting point for you.
It's dark here, rainy, and coffee's not ready...sigh...
Jay |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Aug 18, 2019 5:40 am |
|
|
What does "up == 1" mean?
Is it "button up", "button down", or what?
You're making us guess.
Mike |
|
|
emazzu630
Joined: 14 Aug 2018 Posts: 20 Location: Ouro Verde - SC
|
|
Posted: Sun Aug 18, 2019 7:11 am |
|
|
What I want comes down to this:
- I have a function shown on the display.
- After pressing a button, the display will show the function of the pressed button.
- When the button is released after a certain time has elapsed, the display shows the function that was before pressing the button. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sun Aug 18, 2019 7:46 am |
|
|
You've not answered my question.
Please show us SHORT, complete, compilable code which demonstrates the push button part of your code.
We're only need the button part of the code, remove everything else.
The various functions only need to send "Function 1", "Function 2", etc to a UART. The actual functions are irrelevant.
You're the one asking for help, you need to make it easy for use to copy & paste, to test it.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19529
|
|
Posted: Sun Aug 18, 2019 7:48 am |
|
|
OK.
Now this implies you have the button wired as a 'pull up'. Slightly unusual.
There is a very common 'practice' in electronics, to wire switches as 'pull down'
This comes from two 'reasons'. The first was that TTL inputs tended to 'float
up', and the second is that the noise margin on a logic 'high' is much greater
than on a logic 'low', so it is generally more reliable to use a pull up resistor
to 'high', and the switch to 'low'. You might want to consider this.
Then the point being made was that it is much clearer to use a define for
such tests. So:
Code: |
#define SWITCH_ON (1)
//then testing as
void incrementa() {
if ((up == SWITCH_ON)&&(j<=255)) {
j++;
delay_ms(100);
|
Makes the operation 'self document'.
Now in your case, the way to approach this is to have a 'old_key'
variable, store the last used key value in this and a test like:
Code: |
#define SWITCH_ON (1)
#define SWITCH_OFF (0)
if ((old_key==SWITCH_ON) && (up=SWITCH_OFF))
{ //This will now execute when the key is released
|
|
|
|
emazzu630
Joined: 14 Aug 2018 Posts: 20 Location: Ouro Verde - SC
|
|
Posted: Tue Aug 20, 2019 11:02 am |
|
|
A situação foi resolvida.
Last edited by emazzu630 on Sat Oct 26, 2019 10:14 pm; edited 1 time in total |
|
|
MassaM
Joined: 08 Jun 2019 Posts: 31
|
|
Posted: Fri Sep 20, 2019 5:43 pm |
|
|
Few comments:
emazzu630 wrote: |
Code: |
#byte porta = 0x05
#byte portc = 0x07
#bit up_2 = 0x05.0
#bit down_2 = 0x05.1
#bit LED = 0x07.0
|
|
In above, if you have set the #byte porta = 0x05 and #byte portc = 0x07,
then why do you do this:
#bit up_2 = 0x05.0
#bit down_2 = 0x05.1
You can simply use the porta.0 or porta.1 and so on since you have the portx why not be consistent and use as a clearer option?
Also here:
Code: |
address++;
address = address+1;
|
Why are you doing the same thing differently twice? Isn't address++ same as address = address+1
Use this code:
Code: |
#include <stdio.h>
int main ()
{
int address;
address = 0;
address++;
printf ("Value of address using \"address++\" is: %i\n",address);
address = 0;
address = address + 1;
printf ("Value of address using \"address = address + 1\" is: %i\n",address);
return 0;
}
|
on this https://onlinegdb.com/HJUKZy7Dr and then press the run to test the above and understand.
Also that the increment and decrement should be separate functions to be called based on the relevant button(s) pressed.
A decrement or increment should do the polling as per the buttons, also the display.
Check my demo code here:
http://www.ccsinfo.com/forum/viewtopic.php?t=58136
Also as Ttelmah mentioned, you can set/use an old_function variable and set that before you call the new/next function and load back the value once the delay is over, a kind of a revert back to old function if something interrupted it.
Have a good one and keep learning. _________________ while(!dead)
{
keepLearning();
} |
|
|
|