- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello everyone, i'll trying create a program for turn on a LED with a button (BOTAO1) and turn off a LED with another button (BOTAO 2). The exercise i'm trying to do is that:
Connect to a conventional button (button 1), the capacitive button (button 2) and the
interface for LED without shield. Developing software to run a machine
Figure 1. Uses enumeration REQUIRED
(ENUM command) which is a user-defined data type together
with switch / case. Encapsulate as Status Shift Actions in Functions.
For example:
enum estados {ESTADO_1 = 1, ESTADO_2};
estados led_est;
switch(led_est)
{
case ESTADO_1:
go_to_state2();
break;
case ESTADO_1:
go_to_state1();
break;
}
Solved! Go to Solution.
- Labels:
-
PSoC 4 Architecture
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I saw your project, and there are a few things I'd like to comment.
(1) The Schematic
Your schematic suggests the button is LOW when pushed, there for it must be HIGH when not pushed.
But your button configuration was
This means that the button is LOW when not pushed, and will be low when pushed,
which mean you will never get HIGH from the button.
If the button needs to be HIGH when pushed, you need write schematic like below
Second similarly your LED does not seem to be light, if you want to turn on LED when output is HIGH
the schematic will look like
Anyway, I tried your program with CY8CKIT-044 which has CY8C4247AZI-M485,
on the board SW2 is LOW when pushed (active low) and LED turn on when the pin to LED is LOW (active low).
Meantime, transition of a state is usually only one line, assignment to the state variable,
otherwise the function will keep staying at the same state.
As I don't want to name the state function go_to_xxx(), as usually it will no go,
I renamed the state functions to state_lig() and state_desl().
Then the main loop is now look like
==============
for(;;) {
switch(led_est) {
case lig:
state_lig();
break;
case desl:
state_desl();
break;
}
}
==============
And in each state function only when the condition for the state transition meets,
the state variable will be changed.
==============
void state_desl()
{
if(BOTAO_1_Read() == BUTTON_ON) {
led_est = lig;
LED_Write(LED_ON);
}
}
==============
==============
void state_lig()
{
if (!CapSense_IsBusy()) {
if (CapSense_CheckIsWidgetActive(CapSense_BUTTON0__BTN)) { /* note there are two '_' btween BUTTON0 and BTN */
led_est = desl ;
LED_Write(LED_OFF) ;
}
CapSense_UpdateEnabledBaselines() ;
CapSense_ScanEnabledWidgets() ;
}
}
==============
As you can see, I encapsulate the value of LED and BUTTON in
LED_ON, LED_OFF and BUTTON_ON, BUTTON_OFF
so that when board and/or hardware config gets changed
only the definition of these need to be changed.
==============
#if 1 /* Button is active low */
#define BUTTON_ON 0
#define BUTTON_OFF 1
#else /* Button is active high */
#define BUTTON_ON 1
#define BUTTON_OFF 0
#endif
#if 1 /* LED is active low */
#define LED_ON 0
#define LED_OFF 1
#else /* LED is active high */
#define LED_ON 1
#define LED_OFF 0
#endif
==============
Well, last but not least, as your teacher required to use capacitive switch for the second button I tried so.
So here is the whole enchilada.
==============
#include "project.h"
void state_lig();
void state_desl();
enum estados {desl = 0, lig};
enum estados led_est = desl;
#if 1 /* Button is active low */
#define BUTTON_ON 0
#define BUTTON_OFF 1
#else /* Button is active high */
#define BUTTON_ON 1
#define BUTTON_OFF 0
#endif
#if 1 /* LED is active low */
#define LED_ON 0
#define LED_OFF 1
#else /* LED is active high */
#define LED_ON 1
#define LED_OFF 0
#endif
int main(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */
/* Place your initialization/startup code here (e.g. MyInst_Start()) */
CapSense_Start() ;
CapSense_InitializeAllBaselines() ;
CapSense_ScanEnabledWidgets() ;
LED_Write(LED_OFF) ;
for(;;) {
switch(led_est) {
case lig:
state_lig();
break;
case desl:
state_desl();
break;
}
}
}
void state_desl()
{
if(BOTAO_1_Read() == BUTTON_ON) {
led_est = lig;
LED_Write(LED_ON);
}
}
void state_lig()
{
if (!CapSense_IsBusy()) {
if (CapSense_CheckIsWidgetActive(CapSense_BUTTON0__BTN)) {
led_est = desl ;
LED_Write(LED_OFF) ;
}
CapSense_UpdateEnabledBaselines() ;
CapSense_ScanEnabledWidgets() ;
}
}
==============
Attached is my sample project,
at least it is working on my CY8CKIT-044,
but to use with your hardware, you need
(1) Change the device using the menu Project > Device Selector...
(2) Change the logic of BUTTON if it's different from mine.
(3) Change the logic of LED if it's different from mine.
moto
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Seems you have for both paths of the switch the same case variable ESTADO_1.
Is there no compiler warning/error raised?
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I saw your project, and there are a few things I'd like to comment.
(1) The Schematic
Your schematic suggests the button is LOW when pushed, there for it must be HIGH when not pushed.
But your button configuration was
This means that the button is LOW when not pushed, and will be low when pushed,
which mean you will never get HIGH from the button.
If the button needs to be HIGH when pushed, you need write schematic like below
Second similarly your LED does not seem to be light, if you want to turn on LED when output is HIGH
the schematic will look like
Anyway, I tried your program with CY8CKIT-044 which has CY8C4247AZI-M485,
on the board SW2 is LOW when pushed (active low) and LED turn on when the pin to LED is LOW (active low).
Meantime, transition of a state is usually only one line, assignment to the state variable,
otherwise the function will keep staying at the same state.
As I don't want to name the state function go_to_xxx(), as usually it will no go,
I renamed the state functions to state_lig() and state_desl().
Then the main loop is now look like
==============
for(;;) {
switch(led_est) {
case lig:
state_lig();
break;
case desl:
state_desl();
break;
}
}
==============
And in each state function only when the condition for the state transition meets,
the state variable will be changed.
==============
void state_desl()
{
if(BOTAO_1_Read() == BUTTON_ON) {
led_est = lig;
LED_Write(LED_ON);
}
}
==============
==============
void state_lig()
{
if (!CapSense_IsBusy()) {
if (CapSense_CheckIsWidgetActive(CapSense_BUTTON0__BTN)) { /* note there are two '_' btween BUTTON0 and BTN */
led_est = desl ;
LED_Write(LED_OFF) ;
}
CapSense_UpdateEnabledBaselines() ;
CapSense_ScanEnabledWidgets() ;
}
}
==============
As you can see, I encapsulate the value of LED and BUTTON in
LED_ON, LED_OFF and BUTTON_ON, BUTTON_OFF
so that when board and/or hardware config gets changed
only the definition of these need to be changed.
==============
#if 1 /* Button is active low */
#define BUTTON_ON 0
#define BUTTON_OFF 1
#else /* Button is active high */
#define BUTTON_ON 1
#define BUTTON_OFF 0
#endif
#if 1 /* LED is active low */
#define LED_ON 0
#define LED_OFF 1
#else /* LED is active high */
#define LED_ON 1
#define LED_OFF 0
#endif
==============
Well, last but not least, as your teacher required to use capacitive switch for the second button I tried so.
So here is the whole enchilada.
==============
#include "project.h"
void state_lig();
void state_desl();
enum estados {desl = 0, lig};
enum estados led_est = desl;
#if 1 /* Button is active low */
#define BUTTON_ON 0
#define BUTTON_OFF 1
#else /* Button is active high */
#define BUTTON_ON 1
#define BUTTON_OFF 0
#endif
#if 1 /* LED is active low */
#define LED_ON 0
#define LED_OFF 1
#else /* LED is active high */
#define LED_ON 1
#define LED_OFF 0
#endif
int main(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */
/* Place your initialization/startup code here (e.g. MyInst_Start()) */
CapSense_Start() ;
CapSense_InitializeAllBaselines() ;
CapSense_ScanEnabledWidgets() ;
LED_Write(LED_OFF) ;
for(;;) {
switch(led_est) {
case lig:
state_lig();
break;
case desl:
state_desl();
break;
}
}
}
void state_desl()
{
if(BOTAO_1_Read() == BUTTON_ON) {
led_est = lig;
LED_Write(LED_ON);
}
}
void state_lig()
{
if (!CapSense_IsBusy()) {
if (CapSense_CheckIsWidgetActive(CapSense_BUTTON0__BTN)) {
led_est = desl ;
LED_Write(LED_OFF) ;
}
CapSense_UpdateEnabledBaselines() ;
CapSense_ScanEnabledWidgets() ;
}
}
==============
Attached is my sample project,
at least it is working on my CY8CKIT-044,
but to use with your hardware, you need
(1) Change the device using the menu Project > Device Selector...
(2) Change the logic of BUTTON if it's different from mine.
(3) Change the logic of LED if it's different from mine.
moto