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

PSoC™ 4 Forum Discussions

saurabh
Level 3
Level 3
50 sign-ins 25 sign-ins 10 replies posted

Hello @Hari ,

I am using CY8c4245AXI-483 PSoC4 family MCU. I have implemented button and slider capsense functionality. For the touch button, I have implemented a functionality which changes the state of the button when the key is released. It is working fine but currently, I have been facing an issue with the slider. I need certain reference on how to implement a function which will change the state of slider key  when it is released as I am sending a command to underlying MCU based on touch detection.

0 Likes
1 Solution
MotooTanaka
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Hi, 

I also played with my CY8CKIT-042 😉

When I touch the slider the Green LED lights and changes the strength depending on the position.

When I release my finger the Blue LED lights.

Schematic

001-schematic.JPG

Pins

002-Pins.JPG

main.c

#include "project.h"

#define LED_ON 0u
#define LED_OFF 1u

void init_hardware(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    
    LED_B_Write(LED_OFF) ;
    LED_R_Write(LED_OFF) ;
    
    EZI2C_Start() ;
    /* Set up communication data buffer to CapSense data structure to be exposed to 
    I2C master at primary slave address request. */
    EZI2C_EzI2CSetBuffer1(sizeof(CapSense_dsRam), sizeof(CapSense_dsRam), 
                         (uint8 *)&CapSense_dsRam);
 
    /* Start CapSense block - Initializes CapSense Data structure and 
    performs first scan to set up sensor baselines */
    CapSense_Start();
    
    PWM_Start() ;
    
    /* Scan all widgets */
    CapSense_ScanAllWidgets();
}

void set_pwm_duty(uint16_t duty)
{
    uint16_t period ;
    uint32_t work  ;

    period = PWM_ReadPeriod() ;
    work = duty * period / 100 ;
    if (work > period) {
        work = period ;
    }
    
    PWM_WriteCompare(work) ;
    PWM_WriteCounter(0) ;
}

int main(void)
{
    uint16_t duty = 0 ;

    init_hardware() ;

    for(;;)
    {
        if (CapSense_NOT_BUSY == CapSense_IsBusy()) {
            CapSense_ProcessAllWidgets() ;
            
            CapSense_RunTuner() ;
            
            if (CapSense_IsWidgetActive(CapSense_LINEARSLIDER0_WDGT_ID)) {
                LED_B_Write(LED_OFF) ;
                duty = 0 ;
               
                if (CapSense_IsSensorActive(CapSense_LINEARSLIDER0_WDGT_ID, CapSense_LINEARSLIDER0_SNS0_ID)) {
                    duty += 5 ;
                }
                if (CapSense_IsSensorActive(CapSense_LINEARSLIDER0_WDGT_ID, CapSense_LINEARSLIDER0_SNS1_ID)) {
                    duty += 10 ;
                }
                if (CapSense_IsSensorActive(CapSense_LINEARSLIDER0_WDGT_ID, CapSense_LINEARSLIDER0_SNS2_ID)) {
                    duty += 15 ;
                }
                if (CapSense_IsSensorActive(CapSense_LINEARSLIDER0_WDGT_ID, CapSense_LINEARSLIDER0_SNS3_ID)) {
                    duty += 30 ;
                }
                if (CapSense_IsSensorActive(CapSense_LINEARSLIDER0_WDGT_ID, CapSense_LINEARSLIDER0_SNS4_ID)) {
                    duty += 40 ;
                }          
                set_pwm_duty(duty) ;    
            } else { /* Liner Slider is not touched */
                LED_B_Write(LED_ON) ;
                set_pwm_duty(0) ;
            }
            
            CapSense_ScanAllWidgets() ;       
        }
    }
}

moto

View solution in original post

3 Replies
ncbs
Moderator
Moderator
Moderator
50 likes received 250 sign-ins 250 replies posted

Hi @saurabh,

You can refer to the following code example- CE210289 – PSoC®4 CapSense® Linear Slider.

Regards,
Nikhil

0 Likes
saurabh
Level 3
Level 3
50 sign-ins 25 sign-ins 10 replies posted

Thanks @ncbs, I will give it a try.

0 Likes
MotooTanaka
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Hi, 

I also played with my CY8CKIT-042 😉

When I touch the slider the Green LED lights and changes the strength depending on the position.

When I release my finger the Blue LED lights.

Schematic

001-schematic.JPG

Pins

002-Pins.JPG

main.c

#include "project.h"

#define LED_ON 0u
#define LED_OFF 1u

void init_hardware(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    
    LED_B_Write(LED_OFF) ;
    LED_R_Write(LED_OFF) ;
    
    EZI2C_Start() ;
    /* Set up communication data buffer to CapSense data structure to be exposed to 
    I2C master at primary slave address request. */
    EZI2C_EzI2CSetBuffer1(sizeof(CapSense_dsRam), sizeof(CapSense_dsRam), 
                         (uint8 *)&CapSense_dsRam);
 
    /* Start CapSense block - Initializes CapSense Data structure and 
    performs first scan to set up sensor baselines */
    CapSense_Start();
    
    PWM_Start() ;
    
    /* Scan all widgets */
    CapSense_ScanAllWidgets();
}

void set_pwm_duty(uint16_t duty)
{
    uint16_t period ;
    uint32_t work  ;

    period = PWM_ReadPeriod() ;
    work = duty * period / 100 ;
    if (work > period) {
        work = period ;
    }
    
    PWM_WriteCompare(work) ;
    PWM_WriteCounter(0) ;
}

int main(void)
{
    uint16_t duty = 0 ;

    init_hardware() ;

    for(;;)
    {
        if (CapSense_NOT_BUSY == CapSense_IsBusy()) {
            CapSense_ProcessAllWidgets() ;
            
            CapSense_RunTuner() ;
            
            if (CapSense_IsWidgetActive(CapSense_LINEARSLIDER0_WDGT_ID)) {
                LED_B_Write(LED_OFF) ;
                duty = 0 ;
               
                if (CapSense_IsSensorActive(CapSense_LINEARSLIDER0_WDGT_ID, CapSense_LINEARSLIDER0_SNS0_ID)) {
                    duty += 5 ;
                }
                if (CapSense_IsSensorActive(CapSense_LINEARSLIDER0_WDGT_ID, CapSense_LINEARSLIDER0_SNS1_ID)) {
                    duty += 10 ;
                }
                if (CapSense_IsSensorActive(CapSense_LINEARSLIDER0_WDGT_ID, CapSense_LINEARSLIDER0_SNS2_ID)) {
                    duty += 15 ;
                }
                if (CapSense_IsSensorActive(CapSense_LINEARSLIDER0_WDGT_ID, CapSense_LINEARSLIDER0_SNS3_ID)) {
                    duty += 30 ;
                }
                if (CapSense_IsSensorActive(CapSense_LINEARSLIDER0_WDGT_ID, CapSense_LINEARSLIDER0_SNS4_ID)) {
                    duty += 40 ;
                }          
                set_pwm_duty(duty) ;    
            } else { /* Liner Slider is not touched */
                LED_B_Write(LED_ON) ;
                set_pwm_duty(0) ;
            }
            
            CapSense_ScanAllWidgets() ;       
        }
    }
}

moto