How to toggle led on/off using Capsense button

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

cross mob
AjSi_4796051
Level 1
Level 1
First like received First like given Welcome!

I am unable to get smoth operation of toggling led using self capacitance  CapSense button

0 Likes
1 Solution
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hi AjSi_4796051​,

That logic is fairly simple again. You need to toggle the LED only once on every successful touch detection. So the firmware would be

uint8 wdgt_active = 0;

for(;;)

{

     if(CapSense_NOT_BUSY == CapSense_IsBusy())

     { 

          CapSense_ProcessAllWidgets(); /* Process all widgets */

          CapSense_RunTuner(); /* To sync with Tuner application */

          if (fingerpresent_0 == 0 && CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))

          {

               if(wdgt_active == 0)

               {

                    PWM_1_WriteCompare1(255);

                    wdgt_active = 1;

               }

               else

               {

                    PWM_1_WriteCompare1(10);

                    wdgt_active = 0;

               }

               fingerpresent_0 = 1;

          }

          else if (fingerpresent_0 == 1 && !CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))

          {

               fingerpresent_0 = 0;

          }

          CapSense_ScanAllWidgets();

     }

}

Best regards,   

Hari

View solution in original post

8 Replies
lock attach
Attachments are accessible only for community members.
VenkataD_41
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hi,

Please find the attached projects where the operation is as follows:

Project 1:

Configured two buttons such that an LED in the kit glows whenever the button is pressed. Send the status of the button through UART only when the status changes. That is, transmit “Button Pressed” or “Button Released” through UART whenever the button is pressed/released respectively and this must only be sent once per change.

Project2:

Configured a linear slider such that the brightness of an LED is adjusted by the finger position and the position must be sent over through UART. Again, it must be sent only once per value. Same value must not be repeated multiple times consecutively.

Hope these helps !

Thanks

Ganesh

Hi ganesh ,

Tnx for your reply ,

I used project 1 to toggle led ( capsense button pressed led become on and then again button pressed led become off )

But flickerring issue is comming !!

Any solution for that !!

Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hi AjSi_4796051

In your case, the for loop can be much simpler.

for(;;)

{

    /* Do this only when a scan is done */

    if(CapSense_NOT_BUSY == CapSense_IsBusy())

    {

        CapSense_ProcessAllWidgets(); /* Process all widgets */

        CapSense_RunTuner(); /* To sync with Tuner application */

        if (CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID)) /* Scan result verification */

        {

            GREEN_LED_Write(0);

        }

        else

        {

            GREEN_LED_Write(1);

        }

        CapSense_ScanAllWidgets(); /* Start next scan */

    }

}

Best regards,
Hari

if(CapSense_NOT_BUSY == CapSense_IsBusy())

     {  

      

         CapSense_ProcessAllWidgets(); /* Process all widgets */

            CapSense_RunTuner(); /* To sync with Tuner application */

      

          

       

      

        

       if (fingerpresent_0 == 0 && CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))

        {

          

             PWM_1_WriteCompare1(255);

             fingerpresent_0 = 1;

            

        }

         else if (fingerpresent_0 == 1 && CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))

        {

          

             PWM_1_WriteCompare1(10);

             fingerpresent_0 = 0;

            

        }

my program is like this but when i pressed the button some time it works fine but some time it fluctuate           

0 Likes
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hi AjSi_4796051

You need to modify the else if condition to

else if (fingerpresent_0 == 1 && !(CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID)))

Notice the negation symbol for the CapSense_IsWidgetActive. This is to turn off the LED only if the widget is not active.

Best regards,

Hari

0 Likes

Hi Hari,

thanks for  your reply !!!

actually I used the led as a feedback, one tap on button to switch on the LED ,then another tap on button to switch off the LED

and as you mentioned , i think it is used for displaying button is active !!!

regards

ajay

0 Likes
AjSi_4796051
Level 1
Level 1
First like received First like given Welcome!

Can you guide me , about the code !!

0 Likes
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hi AjSi_4796051​,

That logic is fairly simple again. You need to toggle the LED only once on every successful touch detection. So the firmware would be

uint8 wdgt_active = 0;

for(;;)

{

     if(CapSense_NOT_BUSY == CapSense_IsBusy())

     { 

          CapSense_ProcessAllWidgets(); /* Process all widgets */

          CapSense_RunTuner(); /* To sync with Tuner application */

          if (fingerpresent_0 == 0 && CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))

          {

               if(wdgt_active == 0)

               {

                    PWM_1_WriteCompare1(255);

                    wdgt_active = 1;

               }

               else

               {

                    PWM_1_WriteCompare1(10);

                    wdgt_active = 0;

               }

               fingerpresent_0 = 1;

          }

          else if (fingerpresent_0 == 1 && !CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))

          {

               fingerpresent_0 = 0;

          }

          CapSense_ScanAllWidgets();

     }

}

Best regards,   

Hari