Table of Contents
- Introduction
- Getting Started
- Adding CapSense
- How to Configure The OLED Display
- Device Assembly
- Programming, Test and Troubleshooting
- Summary
**********************************************************************************************************************
In this chapter I will show you the programming of the PSoC 62S4 board, the tests, and the bugs that I report to Infineon. Let me tell you that I made changes to my final project due this bugs. In the previous chapter we assembled the drink dispenser, in the image below I show you how it would look like:
Programming
I have created the second version of the project in modustoolbox. Below the image of the project created.
I have used as a reference the project that I did in chapter 3 with CapSense sensors. Below I show you the code of the file "beverage_dispenser.c" which contains the main instructions of my project.
// Author: Guillermo Perez /******************************************************************************* * Header files includes *******************************************************************************/ #include "beverage_dispenser.h" #include "cybsp.h" #include "cyhal.h" #include <stdio.h> #include <math.h> /******************************************************************************* * Global constants *******************************************************************************/ #define PWM_LED_FREQ_HZ (1000000lu) /* in Hz */ #define GET_DUTY_CYCLE(x) (100 - x) #define DELAY_LONG_MS (3000) /* ADD 2 milliseconds */ /* PWM Frequency */ #define PWM_FREQUENCY (50u) /******************************************************************************* * Global constants *******************************************************************************/ led_state_t led_state_cur = LED_OFF; cyhal_pwm_t pwm_led; cyhal_pwm_t pwm_dispenser; cyhal_pwm_t pwm_dispenser2; /******************************************************************************* * Function Name: update_led_state ******************************************************************************** * Summary: * This function updates the LED state, based on the touch input. * * Parameter: * ledData: the pointer to the LED data structure * *******************************************************************************/ void update_led_state(led_data_t *ledData) { if ((led_state_cur == LED_OFF) && (ledData->state == LED_ON)) { cyhal_pwm_start(&pwm_led); led_state_cur = LED_ON; cyhal_gpio_write(P0_2, CYBSP_LED_STATE_OFF); // tea dispenser cyhal_gpio_write(P0_3, CYBSP_LED_STATE_OFF); // red LED } else if ((led_state_cur == LED_ON) && (ledData->state == LED_OFF)) { cyhal_pwm_stop(&pwm_led); led_state_cur = LED_OFF; cyhal_gpio_write(P0_2, CYBSP_LED_STATE_ON); // tea dispenser cyhal_gpio_write(P0_3, CYBSP_LED_STATE_ON); // red LED } else { } if ((LED_ON == led_state_cur) || ((LED_OFF == led_state_cur) && (ledData->brightness > 0))) { cyhal_pwm_start(&pwm_led); uint32_t brightness = (ledData->brightness < LED_MIN_BRIGHTNESS) ? LED_MIN_BRIGHTNESS : ledData->brightness; uint32_t PWM_DUTY_CYCLE_DISPENSER = brightness; cyhal_pwm_set_duty_cycle(&pwm_dispenser, PWM_DUTY_CYCLE_DISPENSER, PWM_FREQUENCY); // coffee dispenser cyhal_pwm_start(&pwm_dispenser); // coffee dispenser cyhal_pwm_set_duty_cycle(&pwm_dispenser2, PWM_DUTY_CYCLE_DISPENSER, PWM_FREQUENCY); // green LED cyhal_pwm_start(&pwm_dispenser2); // green LED } } /******************************************************************************* * Function Name: initialize_led ******************************************************************************** * Summary: * Initializes a PWM resource for driving an LED. * *******************************************************************************/ cy_rslt_t initialize_led(void) { cy_rslt_t rslt; rslt = cyhal_pwm_init(&pwm_led, CYBSP_USER_LED, NULL); rslt = cyhal_pwm_init(&pwm_dispenser, P2_4, NULL); // connect to water pump 2 rslt = cyhal_pwm_init(&pwm_dispenser2, P2_6, NULL); // connect to red LED rslt = cyhal_gpio_init(P0_2, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_ON); // connect to water pump 1 rslt = cyhal_gpio_init(P0_3, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_ON); // connect to green LED if (CY_RSLT_SUCCESS == rslt) { rslt = cyhal_pwm_set_duty_cycle(&pwm_led, GET_DUTY_CYCLE(LED_MAX_BRIGHTNESS), PWM_LED_FREQ_HZ); if (CY_RSLT_SUCCESS == rslt) { rslt = cyhal_pwm_start(&pwm_led); } } if (CY_RSLT_SUCCESS == rslt) { led_state_cur = LED_ON; } return rslt; } /* [] END OF FILE */
How does it work?
- In this case, I have added a red LED to visually indicate that pump 2 is working when the CapSense buttons are activated. In my case I have used these buttons to control a tea dispenser.
- Similarly, I've added a green LED to visually indicate that pump 1 is working when I slide my finger across the CapSense slider. Here the slider controls a coffee dispenser.
/******************************************************************************* * Function Name: initialize_led ******************************************************************************** * Summary: * Initializes a PWM resource for driving an LED. * *******************************************************************************/ cy_rslt_t initialize_led(void) { cy_rslt_t rslt; rslt = cyhal_pwm_init(&pwm_led, CYBSP_USER_LED, NULL); rslt = cyhal_pwm_init(&pwm_dispenser, P2_4, NULL); // connect to water pump 2 rslt = cyhal_pwm_init(&pwm_dispenser2, P2_6, NULL); // connect to red LED rslt = cyhal_gpio_init(P0_2, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_ON); // connect to water pump 1 rslt = cyhal_gpio_init(P0_3, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_ON); // connect to green LED if (CY_RSLT_SUCCESS == rslt) { rslt = cyhal_pwm_set_duty_cycle(&pwm_led, GET_DUTY_CYCLE(LED_MAX_BRIGHTNESS), PWM_LED_FREQ_HZ); if (CY_RSLT_SUCCESS == rslt) { rslt = cyhal_pwm_start(&pwm_led); } } if (CY_RSLT_SUCCESS == rslt) { led_state_cur = LED_ON; } return rslt; }
I've also made the necessary changes so that the buttons and the slider don't interfere with each other.
void update_led_state(led_data_t *ledData) { if ((led_state_cur == LED_OFF) && (ledData->state == LED_ON)) { cyhal_pwm_start(&pwm_led); led_state_cur = LED_ON; cyhal_gpio_write(P0_2, CYBSP_LED_STATE_OFF); // tea dispenser cyhal_gpio_write(P0_3, CYBSP_LED_STATE_OFF); // red LED } else if ((led_state_cur == LED_ON) && (ledData->state == LED_OFF)) { cyhal_pwm_stop(&pwm_led); led_state_cur = LED_OFF; cyhal_gpio_write(P0_2, CYBSP_LED_STATE_ON); // tea dispenser cyhal_gpio_write(P0_3, CYBSP_LED_STATE_ON); // red LED } else { } if ((LED_ON == led_state_cur) || ((LED_OFF == led_state_cur) && (ledData->brightness > 0))) { cyhal_pwm_start(&pwm_led); uint32_t brightness = (ledData->brightness < LED_MIN_BRIGHTNESS) ? LED_MIN_BRIGHTNESS : ledData->brightness; uint32_t PWM_DUTY_CYCLE_DISPENSER = brightness; cyhal_pwm_set_duty_cycle(&pwm_dispenser, PWM_DUTY_CYCLE_DISPENSER, PWM_FREQUENCY); // coffee dispenser cyhal_pwm_start(&pwm_dispenser); // coffee dispenser cyhal_pwm_set_duty_cycle(&pwm_dispenser2, PWM_DUTY_CYCLE_DISPENSER, PWM_FREQUENCY); // green LED cyhal_pwm_start(&pwm_dispenser2); // green LED } }
Test
Below I show you the tests that I carried out with this device. One of the things I discovered is that using the capsense buttons the water pump throws the liquid more forcefully. On the other hand, with the slider we can control the speed of the liquid until it becomes smoother.
Troubleshooting
I recommend using plastic and having a kitchen towel ready for possible liquid spills. It's also necessary to unfold the plastic hose. When I developed my project, I found into problems that I couldn't solve, so I request help to Infineon technical support and I started the cases shown below and their status.
Case IFX-230328-890211
My idea was to use the OLED display to visually indicate when a cup of coffee or tea was ready. However I found that there is a bug if I want to use the CapSense buttons and the OLE display simultaneously. I don't know if the problem is hardware or software because the I2C protocol in theory indicates that I can connect several devices and the protocol assigns an address to control the communication of each device. I opened the case number IFX-230328-890211 on March 28 with high priority and today it has not been solved.
The person in charge of this case is Sobhit Panda, and if there is any solution to this case, my idea is to update the version of my project as soon as I can.
Last update: On May 31 Rohan Manish contacted me to let me know that he had fixed this bug. I already have the information to update this project and I will publish the solution as soon as I can.
Case IFX-230315-875860
Another idea that I wanted to carry out was the use of the HC-SR04 ultrasonic sensor to know if there was a glass in the drink dispenser. To do so, it is necessary to use Timer/Counter and PWM functionalities. However, there is no example code with counters, and technical support told me that only theoretical information existed. In the end, they committed to doing sample code with counters in the future. The contest is close to end, so my idea is to update the version of the drink dispenser as soon as I solved this issue.
In the next chapter I will give you a summary of all my experience related to this project.
Top Comments