10 Replies Latest reply on Nov 20, 2013 1:46 AM by user_1377889

    Capsense

    gopalakrishnan.varadappan

       Hai,

         

      I am tring to use capsense to increment a variable (s)  (CY8CKIT-001) on each touch. For each touch the s has to incremented one. I just altered and used the example program "Capsense_CSD _Design".  

         
          
               char s;    
          
                   
          
               if (CapSense_CSD_CheckIsWidgetActive(CapSense_CSD_BUTTON0__BTN)) {    
          
             CyDelay(2000);    
          
             s++;    
          
             LCD_Position(0u, 5u);    
          
                     LCD_PrintInt16(s); }    
          
                
          
           I noticed that the if loop is operated twise for for each touch which increments the variable s twise. I want the 'if 'loop to be executed only once for each touch. can you sugguest me how?    
          
                
          
           reagrds    
          
           kavin    
          
                
          
                
          
                
         
         
              
        • 1. Re: Capsense
          user_78878863

          Just remember the previous state of the CapSense button, and only increase your counter when it changes from 'not pressed' to pressed'.

          • 2. Re: Capsense
            user_14586677

            To add to Hli's recommendation, if you basically detect a button down,

               

            then you also need to detect its release, then update display, and S. You

               

            tried to do this by intoducing long delay, which is a waste of CPU MIPs and

               

            constrains the rest of the program for other processing.

               

             

               

            So detect down, then release, then inc S, update display. Or if you want a

               

            button that is held down continuously keep incing S, or do so after button has

               

            been pressed for some minimum time. This approach used to increase update

               

            rate the longer a button down, makes button multi functional.

               

             

               

            Regards, Dana.

            • 3. Re: Capsense
              gopalakrishnan.varadappan

               thank you boss

              • 4. Re: Capsense
                user_1377889

                Who is the "Boss" ?

                   

                To add my 2 ct:

                   

                I usually prefer a (simple) state-machine that keeps track of the required different states

                   

                 

                   

                -no button pressed

                   

                -button pressed

                   

                -button pressed for > 500ms (or any other time)

                   

                -button released

                   

                 

                   

                and the event(s) within each state that will go to another state.

                   

                To make things more complicate, you can setup a timer that interrupts at a medium rate (10 - 50Hz) and check the button when Timer expires only Frees a lot of MIPS but still maintains reactivity of the system and additionally has the opportunity to measure the time a button is pressed (or nothing did happen)

                   

                 

                   

                Bob

                • 5. Re: Capsense
                  user_78878863

                  I guess with the 'button released' state you really mean the 'butten released event', which is between the 'button pressed' and the 'no button pressed' states? At least thats how I would implement it...

                  • 6. Re: Capsense
                    user_1377889

                    Probably a language problem:

                       

                    The button released state is a state between the button pressed state and the no button pressed state and is used to have something like a de-bouncing.

                       

                     

                       

                    Bob

                    • 7. Re: Capsense
                      user_78878863

                      Oh, I didn't think of using it for debouncing (but then you would need something similar for pressing the button, right?). But I think this isn't needed for CapSense, it should not have bouncing...

                         

                      hli

                      • 8. Re: Capsense
                        gopalakrishnan.varadappan

                         Actually I need to increment the variable ‘s’ for each key press. Now I shifted the incrementing business to else braces, ie button released after a key press.

                           

                         char s,s1;     

                         

                           

                         LCD_Position(1u, 0u);     

                         

                           

                         if (CapSense_CSD_CheckIsWidgetActive(CapSense_CSD_BUTTON0__BTN))     

                         

                           

                        {s=1; // button pressed     

                         

                           

                        }     

                         

                           

                         else{     

                         

                           

                        if(s==1){s1++;  // button released     

                         

                           

                                LCD_Position(1u, 1u);     

                         

                           

                                LCD_PrintInt16(s1);}     

                         

                           

                                s=0;}

                           

                         

                           

                         is it OK.

                           

                         Kavin     

                         

                        • 9. Re: Capsense
                          user_1377889

                          Yes Kevin, that looks good. But you ought to initialize s1.

                             

                          By the way: It is ALWAYS advisable to use names for variables that reflect the meaning. So I would call "s1" "PressedCounter" and "s" "ButtonWasPressed" or something similar. This improves readability and can help others (us) to understand what is going on or even detect a bug.

                             

                           

                             

                          Bob

                          • 10. Re: Capsense
                            user_1377889

                            Oh, I just see:

                               

                            You defined s1 as a aigned char, but display it on LCD with a routine that requires a 16-bit integerr. I would suggest to use for s1 an unsigned int.

                               

                             

                               

                            Bob