One button to have two values.

This is a discussion on One button to have two values. within the C Programming forums, part of the General Programming Boards category; Originally Posted by King Mir You can trust analog input as long as the ADC polling rate is slow enough. ...

  1. #16
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    Quote Originally Posted by King Mir View Post
    You can trust analog input as long as the ADC polling rate is slow enough. The ADC will poll the analog signal, and average it for a short time before setting the output register. So there isn't necessarily a need for digital averaging.
    Can you tell me where you have got this information about the PIC10F222?

    I've never heard or read about any automatic averaging done with this device.
    Fact - Beethoven wrote his first symphony in C

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by kim15 View Post
    Hey, thanks for the quick response.

    Im not so sure where I am to start, so I'll just start from beginning. So... The remote has 5 different buttons that have different values for the resistors. after converting it, i get the ranges for the key (that is obtained from ADC). I gave about 10 percent tolerance for each key pressed. After reading and determining which key was pressed, it will then start to send out the data 8bit by 8bit.

    Tried it out, and the remote works fine. It does what it basically supposed to do with the values for each d1 and d2. Then a situation comes where I need to make one of the buttons (button one) to have 2 types of presses; short press and long press, which has 2 different set of values for d1 and d2.

    Now, I have tried using if else. Sometimes it works, sometimes it doesnt. But that isnt the point; the point is that in order for me to access the long press, i first must access to the short press. Which is inconvenient since I dont want the button to be sending the data d1 and d2 for the short press while intending for the long press.

    Then when Salem suggested to use key state, and by using the method that you gave (switch case) I wrote the code just a little different. That is by using the states for the key states to determine the short and long press.

    key_state == 0 means it is short press (not pressing after several count) and key_state == 1 means long press (still pressing after several count)

    But the output seems to only detect the key_state == 0 and never the key_state == 1. Even when I had first initialized the value to be 1 instead of the norm 0.

    I've included the key state events that you suggested. Did I miss something else?

    Please advise.

    Thanks
    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:
    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
    
    }
    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.

    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.

  3. #18
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by Click_here View Post
    Can you tell me where you have got this information about the PIC10F222?

    I've never heard or read about any automatic averaging done with this device.
    EDIT: You're right, this chip has a fixed clock. There is necessarily automatic averaging in the ADC, but you can't vary the polling period.
    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.

  4. #19
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    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

  5. #20
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,501
    Where are you getting the automatic averaging from?
    Fact - Beethoven wrote his first symphony in C

  6. #21
    Registered User
    Join Date
    Dec 2012
    Posts
    37
    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.


Page 2 of 2 FirstFirst 12
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, 06:04 AM
  3. Replies: 100
    Last Post: 06-21-2010, 03:22 AM
  4. Replies: 2
    Last Post: 06-28-2008, 09:30 PM
  5. Scroll button values!
    By pran1 in forum Windows Programming
    Replies: 2
    Last Post: 03-30-2004, 09:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21