CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Use delay_ms () after the button is released

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
emazzu630



Joined: 14 Aug 2018
Posts: 20
Location: Ouro Verde - SC

View user's profile Send private message

Use delay_ms () after the button is released
PostPosted: Sun Aug 18, 2019 12:27 am     Reply with quote

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: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Aug 18, 2019 4:24 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Aug 18, 2019 5:40 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Aug 18, 2019 7:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Aug 18, 2019 7:46 am     Reply with quote

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: 19541

View user's profile Send private message

PostPosted: Sun Aug 18, 2019 7:48 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 20, 2019 11:02 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Fri Sep 20, 2019 5:43 pm     Reply with quote

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();
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group