Thread: One button to have two values.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So what value is stored in key when the key is up?

    How is construct() called? Is it in a loop in your main(), or is it called directly as a result of a completed ADC conversion event?

    Finite-state machine - Wikipedia, the free encyclopedia
    Basically, you have two events, key-press and key-release
    You also have two states key-up and key-down
    So state=key-up and event=key-press takes you to state key-down.

    In state key-down, you increment your counter (at some rate).

    On the key-release event, you look at the key and the counter, and decide what it all means.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    37
    Basically, you have two events, key-press and key-release
    You also have two states key-up and key-down
    So state=key-up and event=key-press takes you to state key-down.
    How exactly do u do a key state events?

    Thanks

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by kim15 View Post
    How exactly do u do a key state events?

    Thanks
    The easiest way of doing this is with a switch statement

    Code:
    switch (state)
    {
      case 0:
        if (button_pressed)
        {
          count += (count < 100);
          if (count == 100)
          {
            count = 0;
            state = 1;
          }
          else if (count > 100)
          {
            count = 0;
          }
        }
        else
        {
          count = 0;
        }
    
        break;
    
      case 1:
        if (!button_pressed)
        {
          count += (count < 100);
          if (count == 100)
          {
            count = 0;
            state = 0;
          }
        }
        else
        {
          count = 0;
        }
    
        break;
        
    }
    Also noticed that I do not take the reading straight away -> I wait for 100 positive readings in a row before I change state.
    Last edited by Click_here; 12-19-2012 at 09:58 PM.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write() and read() int values from socket returning wrong values.
    By Premjith P S in forum Linux Programming
    Replies: 8
    Last Post: 11-29-2012, 02:59 PM
  2. passing int values and *double values through message queues
    By Hyp3rTension in forum C Programming
    Replies: 11
    Last Post: 05-11-2012, 05:04 AM
  3. Replies: 100
    Last Post: 06-21-2010, 02:22 AM
  4. Replies: 2
    Last Post: 06-28-2008, 08:30 PM
  5. Scroll button values!
    By pran1 in forum Windows Programming
    Replies: 2
    Last Post: 03-30-2004, 09:35 AM