Thread: Waiting for a button release in C

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Waiting for a button release in C

    So I'm writing this program to write into hyperterminal using button presses and here is a snippet of my code
    Code:
      if (button1==0)//button is pressed
    {
      		uart_transmit('L');
      }
      if (button2==0)
      	
      {
      		uart_transmit('U');
      }
      if (button3==0)
      {
      		uart_transmit('K');
      }
      if (button4==0)
      {
      		uart_transmit('E');
    	}
    I want it to wait until [button]=1 (ie the button is released) before doing it's next action
    How would I do this?
    Last edited by cloudsword; 05-11-2011 at 11:35 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have to track when button1 was pressed, and then once it's pressed (has a 0 value), wait for it to be released (go back to 1/non-zero).

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    and how would I do that code wise

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Give a little thought to ....

    Code:
    if button1 == 0
     {send to uart 
     while(button1 == 0); }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Maybe a second variable, button1_old. Then, everytime you check button states, you move the value in button1 to button1_old, and update button1 with the new value. If button1_old is 0 and button1 is 1, you know button 1 was released.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    What operating system are you using (or is this an embedded device)?
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by CommonTater View Post
    Give a little thought to ....
    Yeah, after I hit the "post" button, I thought I should have just told the OP to actually try and think of a solution for more than 30 seconds. So...

    @cloudsword: Actually spend time thinking about solutions before giving up and asking for the answer. "Great question asking skills" is a sure way to get your resume thrown out. "Great problem solving skills" is a much better trait to possess. The only way you'll get there is by trying to think things through on your own, and try them out. The world wont end if you try something and it doesn't work.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    cloudsword: Actually spend time thinking about solutions before giving up and asking for the answer. "Great question asking skills" is a sure way to get your resume thrown out. "Great problem solving skills" is a much better trait to possess. The only way you'll get there is by trying to think things through on your own, and try them out. The world wont end if you try something and it doesn't work.
    LOL... it's far more likely to end because we stupidly tried something that DID work....

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Thanks CommonTater! I've been working with HDL's so long that I did not consider using a while loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-28-2008, 08:30 PM
  2. recv() without waiting...
    By eam in forum Networking/Device Communication
    Replies: 12
    Last Post: 02-28-2005, 07:29 PM
  3. Waiting for input
    By Jareds411 in forum Windows Programming
    Replies: 5
    Last Post: 09-16-2004, 06:18 PM
  4. waiting
    By phantom in forum C++ Programming
    Replies: 7
    Last Post: 01-28-2002, 06:38 AM
  5. waiting for help re; pre post
    By OPENCCT in forum C Programming
    Replies: 1
    Last Post: 12-02-2001, 06:20 AM

Tags for this Thread