cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 4 MCU

PREM_KB
New Contributor II

Dear all,

I'm working on CY8C4245AXI-483 MCU. I want to write a code to toggle LED through capsense touch button.  it means, initially LED is OFF. now when i touch button it should be ON. and remains ON untill i touched it 2nd time. after touching 2nd time it should be in OFF condition and remains in OFF untill i touched it 3rd time and so on.

i found code for same, somewhere on internet. is this correct or i need to change something? Please give me your valuable suggestions.

Thanks & Regards,

Prem KB

/*code starts from here*/

uint fingerpresent_0 = 0;

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)
               {
                    LED_Write(1);
                    wdgt_active = 1;
               }
               else
               {
                    LED_Write(0);
                    wdgt_active = 0;
               }
               fingerpresent_0 = 1;
          }
          else if (fingerpresent_0 == 1 && !CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))
          {
               fingerpresent_0 = 0;
          }
          CapSense_ScanAllWidgets();
     }
}
0 Likes
1 Solution
Hari
Moderator
Moderator

Hi @PREM_KB 

 

Yes, this code will work as you require. You can try this out in any kit that has CapSense and LED. 

 

Best regards, 
Hari

View solution in original post

0 Likes
6 Replies
Hari
Moderator
Moderator

Hi @PREM_KB 

 

Yes, this code will work as you require. You can try this out in any kit that has CapSense and LED. 

 

Best regards, 
Hari

View solution in original post

0 Likes
PREM_KB
New Contributor II

hi @Hari ,

thanks for the reply. it works properly in my MCU. I have one more question.

Thanks & Regards,

Prem KB

0 Likes
PREM_KB
New Contributor II

hi @Hari , Please find below Problem.

I have completed all hardware cydwr properly an TopDesign also. i'm facing difficulties in writting main.c.  Please tell me what would be the code for doing this problem. 

0 Likes
Hari
Moderator
Moderator

Hi @PREM_KB 

 

Please let us know where you are facing issues. This looks like a simple implementation of CapSense. Note that we can help you out with any debugging or corner case issues that you might be facing but not with the implementation of the whole code. 

 

To get you started, you can take a look at the CapSense_IsWidgetActive API for the UP and DOWN buttons. This API returns true whenever a particular button is pressed. This can be used to process the LEDs depending on which button is pressed. 


Best regards, 
Hari

PREM_KB
New Contributor II

Thanks, @Hari. actually I'm trying to implement the above code. i will look at the documentation as you suggested .

Thanks & Regards,

Prem KB

0 Likes
MotooTanaka
Esteemed Contributor

Hi,

Although I totally agree with Hari-san, I wrote a sample project.

https://community.cypress.com/t5/Code-Examples/CapSense-up-down-button-sample-PSoC-4-CY8CKIT-042/m-p...

Since I don't have 4 LEDs on my CY8CKIT-042, I just let UART write "*", "* *", "* * *", "* * * *".

I hope that it won't be too difficult to write a program to turn on "real" LEDs in the lines of print("*\n\r") ;

This could be a spoiler, but having a sample to start with is not a bad thing when you (or we) are bewildered.

moto