CapSense up-down button sample (PSoC 4, CY8CKIT-042)

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

cross mob
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

This is a simple sample of using CapSense [v7.0] for 2 buttons operation using CY8CKIT-042.

Although this is a quite simple sample, may be somewhat useful for those who is not familiar with writing C program(s),

such as the discussion below.

https://community.cypress.com/t5/PSoC-4-MCU/Toggle-LED-on-every-successful-capsense-touch-button/td-...


I assigned the node P1.5 as "Up" button and P1.1 as "Down" button.

schematic

002-schematic.JPG

CapSense Config

003-CapSenseConfigure.JPG

Pins

004-Pins.JPG

Since I don't have many LEDs on my CY8CKIT-042, 

I let UART write "*" instead of LEDs.

001-TeraTerm-log.JPG

main.c

==========================

#include "project.h"
#include "stdio.h"

#define MAX_LED_COUNT 4
#define MIN_LED_COUNT 0

#define STR_BUF_LEN 32
char str[STR_BUF_LEN+1] ;

void print(char *str)
{
    UART_PutString(str) ;
}

void cls(void)
{
    print("\x1b[2J\x1b[;H") ;    
}

void init_hardware(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */    
    
    UART_Start() ;
    cls() ;
    
    CapSense_Start() ;
    
    CapSense_ScanAllWidgets() ;
}

void do_led(int led_count)
{
    switch(led_count) {
    case 0: /* MIN_LED_COUNT? */
        /* turn off all LEDs */
        cls() ;
        break ;
    case 1:
        /* turn on only 1 LED */
        cls() ;
        print("*\n\r") ;
        break ;
    case 2:
        /* turn on 2 LEDs */
        cls() ;
        print("* *\n\r") ;
        break ;
    case 3:
        /* turn on 3 LEDs */
        cls() ;
        print("* * *\n\r") ;
        break ;
    case 4: /* MAX_LED_COUNT? */
        /* turn on 4 LEDs */
        cls() ;
        print("* * * *\n\r") ;
        break ;
    default:
        /* do nothing */
        break ;
    }
}

int main(void)
{
    int led_count = 0 ;
    
    init_hardware() ;

    for(;;)
    {
        if (CapSense_NOT_BUSY == CapSense_IsBusy()) {
            CapSense_ProcessAllWidgets() ;
            if (CapSense_IsAnyWidgetActive()) {
                if (CapSense_IsSensorActive(CapSense_BUTTON0_WDGT_ID, CapSense_BUTTON0_SNS0_ID)) {
                    led_count++ ;
                    if (led_count > MAX_LED_COUNT) {
                        led_count = MAX_LED_COUNT ;
                    }
                } else if (CapSense_IsSensorActive(CapSense_BUTTON0_WDGT_ID, CapSense_BUTTON0_SNS1_ID)) {
                    led_count-- ;
                    if (led_count < MIN_LED_COUNT) {
                        led_count = MIN_LED_COUNT ;
                    }
                }
                do_led(led_count) ;
            }
            
            CyDelay(100) ;  
            
            CapSense_ScanAllWidgets() ;
        }
    }
}

==========================

moto

 

 

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

Hello Moto-san

 

Thank you for sharing the project with the community and for your interest in Infineon products.

 

Best regards, 
Hari

Esteem_123
Level 2
Level 2
50 sign-ins 25 sign-ins 10 replies posted

Hi @MotooTanaka ,

Thank you for your wonderful effort. I will try this out in my cy8c4245axi-483 MCU. I have also written code for this.. but it is not properly working. Can you give me suggestions where I'm making mistakes?

y1,y2,y3,y4 are leds.

#include "project.h"

unsigned char status=0,flag[10],flag1[10];

uint8 i=0,n=0;

int main(void)

{

    CyGlobalIntEnable;

    gup_Write(1);

    CapSense_Start();

    y1_Write(1);

    CapSense_InitializeAllBaselines();

    

    for(;;)

    {

        //CapSense_ClearSensors();

        CapSense_ProcessAllWidgets();

        while(CapSense_IsBusy());

        CapSense_ScanAllWidgets();

        status=CapSense_IsWidgetActive(CapSense_UP_WDGT_ID);

        if(status)

        {

            

           if(flag[1]==0)  

                    {flag[1]=1;y1_Write(1);y2_Write(1);y3_Write(0);y4_Write(0);CyDelay(2);}

            else if(flag[1]==1)

                    {flag[1]=2;y1_Write(1);y2_Write(1);y3_Write(1);y4_Write(0);CyDelay(2);}

            else if(flag[1]==2)

                    {flag[1]=0;CyDelay(2);y1_Write(1);y2_Write(1);y3_Write(1);y4_Write(1);}

            

            while(status)

            {

                CapSense_ProcessAllWidgets();

                while(CapSense_IsBusy()); 

                CapSense_ScanAllWidgets();   

                status = CapSense_IsWidgetActive(CapSense_UP_WDGT_ID);

            }

        }

        status=CapSense_IsWidgetActive(CapSense_DOWN_WDGT_ID);

        if(status)

        {   

                 if(flag1[1]==0 && flag[1]==0)  

            {flag1[1]=1;y1_Write(1);y2_Write(1);y3_Write(1);y4_Write(0);CyDelay(2);}

            

            else if(flag1[1]==0 && flag[1]==1)

            {flag1[1]=1;y1_Write(1);y2_Write(0);y3_Write(0);y4_Write(0);CyDelay(2);}

            

            else if(flag1[1]==0 && flag[1]==2)

            {flag1[1]=1;y1_Write(1);y2_Write(1);y3_Write(0);y4_Write(0);CyDelay(2);}

            

            else if(flag1[1]==1 && flag[1]==0)

            {flag1[1]=2;y1_Write(1);y2_Write(1);y3_Write(0);y4_Write(0);CyDelay(2);}

            

            else if(flag1[1]==1 && flag[1]==1)

            {flag1[1]=2;y1_Write(1);y2_Write(0);y3_Write(0);y4_Write(0);CyDelay(2);}

            

            else if(flag1[1]==1 && flag[1]==2)

            {flag1[1]=2;y1_Write(1);y2_Write(0);y3_Write(0);y4_Write(0);CyDelay(2);}

            

            else if(flag1[1]==2 && flag[1]==0)

            {flag1[1]=0;y1_Write(1);y2_Write(0);y3_Write(0);y4_Write(0);CyDelay(2);}

            

            else if(flag1[1]==2 && flag[1]==1)

            {flag1[1]=0;y1_Write(1);y2_Write(0);y3_Write(0);y4_Write(0);CyDelay(2);}

            

            else if(flag1[1]==2 && flag[1]==2)

            {flag1[1]=0;y1_Write(1);y2_Write(0);y3_Write(0);y4_Write(0);CyDelay(2);}

            

            while(status)

            {

                CapSense_ProcessAllWidgets();

                while(CapSense_IsBusy()); 

                CapSense_ScanAllWidgets();   

                status = CapSense_IsWidgetActive(CapSense_DOWN_WDGT_ID);

            }

        }

    }

}

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I modified your source and it seems to be working.

Note, I'm using CapSense [v7.0]

And to test the program, I connected active-high LEDs,

so if yours are active-low,  change the first #if 1 to #if 0.

=============================

#include "project.h"
#include "stdio.h"
#if 1 /* for low active LED, set this to 0 */
#define LED_ON 1
#define LED_OFF 0
#else
#define LED_ON 0
#define LED_OFF 1
#endif

#define STR_BUF_LEN 32
char str[STR_BUF_LEN+1] ;
void print(char *str)
{
    UART_PutString(str) ;
}

unsigned char status=0,flag[10],flag1[10];

uint8 i=0,n=0;

void clear_leds(void)
{
    y1_Write(LED_OFF);
    y2_Write(LED_OFF) ;
    y3_Write(LED_OFF) ;
    y4_Write(LED_OFF) ;    
}

int main(void)
{
    CyGlobalIntEnable;

    clear_leds() ;
        
    UART_Start() ;
    print("\x1b[2J\x1b[;H") ;
//    gup_Write(1);
    CapSense_Start();

    CapSense_ScanAllWidgets();  
    for(;;)
    {
        //CapSense_ClearSensors();
        // CapSense_ProcessAllWidgets();
        if (CapSense_NOT_BUSY == CapSense_IsBusy()) {
            CapSense_ProcessAllWidgets();
            if (CapSense_IsAnyWidgetActive()) {
                if (CapSense_IsSensorActive(CapSense_BTN_WDGT_ID, CapSense_BTN_SNS0_ID)) {
                    if(flag[1]==0) {
                        flag[1]=1;y1_Write(LED_OFF);y2_Write(LED_OFF);y3_Write(LED_ON);y4_Write(LED_ON);CyDelay(2);
                    } else if(flag[1]==1) {
                        flag[1]=2;y1_Write(LED_OFF);y2_Write(LED_OFF);y3_Write(LED_OFF);y4_Write(LED_ON);CyDelay(2);
                    } else if(flag[1]==2) {
                        flag[1]=0;CyDelay(2);y1_Write(LED_OFF);y2_Write(LED_OFF);y3_Write(LED_OFF);y4_Write(LED_OFF);
                    }
                }

                if (CapSense_IsSensorActive(CapSense_BTN_WDGT_ID, CapSense_BTN_SNS1_ID)) {  
                    if (flag1[1]==0 && flag[1]==0) {
                        flag1[1]=1;y1_Write(LED_OFF);y2_Write(LED_OFF);y3_Write(LED_OFF);y4_Write(LED_ON);CyDelay(2);
                    } else if(flag1[1]==0 && flag[1]==1) {
                        flag1[1]=1;y1_Write(LED_OFF);y2_Write(LED_ON);y3_Write(LED_ON);y4_Write(LED_ON);CyDelay(2);
                    } else if(flag1[1]==0 && flag[1]==2) {
                        flag1[1]=1;y1_Write(LED_OFF);y2_Write(LED_OFF);y3_Write(LED_ON);y4_Write(LED_ON);CyDelay(2);
                    } else if(flag1[1]==1 && flag[1]==0) {
                        flag1[1]=2;y1_Write(LED_OFF);y2_Write(LED_OFF);y3_Write(LED_ON);y4_Write(LED_ON);CyDelay(2);
                    } else if(flag1[1]==1 && flag[1]==1) {
                        flag1[1]=2;y1_Write(LED_OFF);y2_Write(LED_ON);y3_Write(LED_ON);y4_Write(LED_ON);CyDelay(2);
                    } else if(flag1[1]==1 && flag[1]==2) {
                        flag1[1]=2;y1_Write(LED_OFF);y2_Write(LED_ON);y3_Write(LED_ON);y4_Write(LED_ON);CyDelay(2);
                    } else if(flag1[1]==2 && flag[1]==0) {
                        flag1[1]=0;y1_Write(LED_OFF);y2_Write(LED_ON);y3_Write(LED_ON);y4_Write(LED_ON);CyDelay(2);
                    } else if(flag1[1]==2 && flag[1]==1) {
                        flag1[1]=0;y1_Write(LED_OFF);y2_Write(LED_ON);y3_Write(LED_ON);y4_Write(LED_ON);CyDelay(2);
                    } else if(flag1[1]==2 && flag[1]==2) {
                        flag1[1]=0;y1_Write(LED_OFF);y2_Write(LED_ON);y3_Write(LED_ON);y4_Write(LED_ON);CyDelay(2);
                    }
                }
                snprintf(str, STR_BUF_LEN, "%d %d\n\r", flag1[1], flag[1]) ;
                print(str) ;  
            }
            CyDelay(100) ;
            CapSense_ScanAllWidgets();  
        }
    }
}

=============================

moto

lock attach
Attachments are accessible only for community members.
Esteem_123
Level 2
Level 2
50 sign-ins 25 sign-ins 10 replies posted

hi @MotooTanaka ,

Thank you so much for your help.

i need some more doubt .

1. the code which you have create is works fine for me. but it should hold its states. i.e if i tap up button 1 time y1,y2 two LEDs should glow till i tap up button for 2nd time, after 2nd tap y1,y2,y3 should glow till next tap. like wise i need to do. so what should be need to change?

2. The code which I have you has a error, when i tap more than 3 times on up button. it again turns on y1,y2 LED instead of this it should be hold it max LED count i.e y1,y2,y3,y4 should continue to glow. similarly if i tap down button initially before tap on up button ony1 should glow, instead of this y1,y2 are glowing.  so what will be the modification required for both the cases?

The modified which you have sent me also having same problem as described in point.1.

Here i am attaching video of Point.1 .it should hold states on each tap instead of changing its states on continue TAP.

Thanks & Regards,

Prem KB

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Prem KB-san,

So, I think that you want to trigger action only when button is tapped and keep tapping will not increase/decrease the LED Count.

I modified (1) to detect tapped edge using previous status of each button.

function "main()" was modified as below.

==============================

int main(void)
{
    int led_count   = 0 ;
    int button_up   = 0 ;
    int prev_up     = 0 ;
    int button_down = 0 ;
    int prev_down   = 0 ;
    
    init_hardware() ;

    for(;;)
    {
        if (CapSense_NOT_BUSY == CapSense_IsBusy()) {
            CapSense_ProcessAllWidgets() ;
            if (CapSense_IsAnyWidgetActive()) {
                button_up = CapSense_IsSensorActive(CapSense_BUTTON0_WDGT_ID, CapSense_BUTTON0_SNS0_ID) ;
                if ((prev_up == 0) && (button_up != 0)) { /* button up tapped */
                    led_count++ ;
                    if (led_count > MAX_LED_COUNT) {
                        led_count = MAX_LED_COUNT ;
                    }
                }
                button_down = CapSense_IsSensorActive(CapSense_BUTTON0_WDGT_ID, CapSense_BUTTON0_SNS1_ID) ;
                if ((prev_down == 0) && (button_down != 0)) { /* button down tapped */
                    led_count-- ;
                    if (led_count < MIN_LED_COUNT) {
                        led_count = MIN_LED_COUNT ;
                    }
                }
                do_led(led_count) ;
                prev_up = button_up ;
                prev_down = button_down ;                   
            } else {
                prev_up = 0 ;
                prev_down = 0 ;
            }
                
            CyDelay(100) ;  
            
            CapSense_ScanAllWidgets() ;
        }
    }
}

==============================

And for (2), I hope that you should debug the code.

(I hope that I have provided you enough information/sample)

Best Regards,

10-Mar-2021

Motoo Tanaka

Hey @MotooTanaka 

Thank you so much. It is working fine. I wanted to control just one on off button with this also. I will try to do it by myself. Thanks again.

 

Thanks && Regards,

Prem KB