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