Capsense

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 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    
    
          
    
          
    
          
   
   
        
0 Likes
10 Replies
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

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

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
Anonymous
Not applicable

 thank you boss

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

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...

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

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

0 Likes
Anonymous
Not applicable

 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     

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

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

0 Likes