cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 5, 3 & 1 MCU

Anonymous
Not applicable

 Hi,

   

I am using CY8CKIT-001 - CY8CKIT-009. I have a problem.I want to use it as a single on-off capsense button. So when I press the button once will light up led,later I press the same capsense button again will turn off led. I use the following code:

if( statusButton0 == ON && onoff==0)

LCD_PrintString("On ");
Led1_Write(0x01);
onoff=1;
}


LCD_Position(1,3);
if( statusButton0 == ON && onoff==1)


CyDelay(200u);
LCD_PrintString("On");
onoff=0;
Led1_Write(0x00);
}

I do not know what I'm doing wrong but Led is flashing. Do you have a software error or hardware error? Thanks your answers.

0 Likes
Reply
1 Solution
Honored Contributor II

I suppose that your CapSense button is always on. You should add debug code to gets its state. Then you can determine whether the CapSense handling is at fault, or your on-off logic.

   

Maybe you can upload your project here so we can have a complete look? (Use the 'File / Create Workspace Bundle').

View solution in original post

0 Likes
Reply
37 Replies
Honored Contributor II

I suppose that your CapSense button is always on. You should add debug code to gets its state. Then you can determine whether the CapSense handling is at fault, or your on-off logic.

   

Maybe you can upload your project here so we can have a complete look? (Use the 'File / Create Workspace Bundle').

View solution in original post

0 Likes
Reply
Anonymous
Not applicable

 Thanks.I upload my design program.

0 Likes
Reply
Anonymous
Not applicable
0 Likes
Reply
Esteemed Contributor

If I look at this code, if its in a loop, you start by turning led on,

   

then immeadiately turn the led off after a delay by setting onoff = 1,

   

so would be flashing. You need to test for status button turning back

   

to "0".

   

 

   

 

   

 

   

if( statusButton0 == ON && onoff==0) {

   

 
     LCD_PrintString("On ");
     Led1_Write(0x01);
     onoff=1;
}


   

LCD_Position(1,3);

   

if( statusButton0 == ON && onoff==1)

   

      CyDelay(200u);
      LCD_PrintString("On");
      onoff=0;
      Led1_Write(0x00);
}

   

 

   

Regards, Dana.

0 Likes
Reply
Anonymous
Not applicable

 ı try to upload program but ı I can not upload.

0 Likes
Reply
Esteemed Contributor

Without seeing whole project code should look like ......

   

 

   

if( statusButton0 == ON && onoff==0) {

   

 
     LCD_PrintString("On ");
     Led1_Write(0x01);
     onoff=1;
}

   

 

   

LCD_Position(1,3);

   

 

   

if( statusButton0 == OFF && onoff==1)

   

      CyDelay(200u);
      LCD_PrintString("Off");
      onoff=0;
      Led1_Write(0x00);
}

   

 

   

Regards, Dana.

0 Likes
Reply
Honored Contributor II

But the LED should not follow the button. I would write it as:

   
if (ON==statusButton0) {   if(0==onoff)   {     // switch it of     onoff=1;   }   else   {     // switch it off     onoff=0;   } } 
0 Likes
Reply
Anonymous
Not applicable

 Hi Dana,

   

Thank you for answers, I tried to say the two code but  program give different results. I want the same push-button LED on - off .Finally I tried the following program; the problem here After pressing the button LED is on, but I need to push another button for  turning off  same push button

   

 

   

 if( slot_1 == 1 && onoff==0)

   

           

   

  {

   

  

   

LCD_PrintString("   On ");

   

LED1_Write(0x01);

   

Button_ON1 = 1;

   

                    }

   

else if (slot_1 == 0 && Button_ON1 == 1) 

   

{ onoff=1;

   

Button_ON1 = 0;

   

}

   

 

   

        if (slot_1 ==1 && onoff==1 )

   

        {

   

                

   

Button_ON2 = 1;

   

LED1_Write(LED_OFF);

   

        }

   

else if(slot_1 == 0  && Button_ON2 == 1)

   

{

   

onoff=0;

   

Button_ON2 = 0;

   

}

0 Likes
Reply
Esteemed Contributor

Not sure I am following your logic goal -

   

 

   

If you push one button LED = on, and then when another button

   

pushed LED is turned off ? If so code would look like -

   

 

   

if ( button1 && !ledonflg) {

   

      // turn on led, set ledonflag, and print "On" on LCD

   

if ( button2 && ledonflag ) {

   

      // turn off led, clear ledonflag, and print "Off" on LCD

   

 

   

Regards, Dana.

0 Likes
Reply
Anonymous
Not applicable

 Hi Hli,

   

I try to your program logic to be true but no stability. program not work in the same way

0 Likes
Reply
Anonymous
Not applicable

 I want to do same button led on - off  Dana if it is placed on the different buttons,becomes very simple.

0 Likes
Reply
Esteemed Contributor

So one press of button, LED turned on, 2'ond press of same button, led turned off ?

0 Likes
Reply
Anonymous
Not applicable

 Yes Dana exactly.

0 Likes
Reply
Esteemed Contributor II

Dana, Hli,

   

did you forget the function "not"? Who (within the tiny PSoC) cares for the current state of the LED?

   

so just write

   

if(ButtonPressed()) LED_State = !LEDState;

   

LED1_Write(LEDState;

   

 

   

Bob

0 Likes
Reply
Esteemed Contributor

Then -

   

 

   

if( statusButton0 == ON && onoff==0) {

   

 
     LCD_PrintString("On ");
     Led1_Write(0x01);
     onoff = 1;
}

   


   

if( statusButton0 == OFF && onoff == 1) onoff = 2;        // First button press released, enable next button press

   


LCD_Position(1,3);

   

 

   

if( statusButton0 == ON && onoff == 2)

   

      CyDelay(200u);
      LCD_PrintString("Off");
      onoff=0;
      Led1_Write(0x00);
}

0 Likes
Reply
Honored Contributor II

@bob - but there is this small CyDelay(200) call which needs to go somewhere...

   

Otherwise you are right, using a bool and negating it is much simpler.

0 Likes
Reply
Anonymous
Not applicable

 Hi Dana,

   

First, thanks for your helping. I try to send your program. I don't know Did you try this program? Push the button led on, then 2. press of same button led doesn't off.If I press very short time led off.I need to press too fast for led off. That isn't stable but thanks so much.

0 Likes
Reply
Esteemed Contributor II

I do not want to teach YOU programming, but here we have a case where correct modularization will get us further and I cannot keep my mouth shut.

   

Let's build a function named ButtonPressed() that returns a boolean value (int8) that tells us, when a button was pressed the very first time. When this function returns true (the first time) the status gets cleared and further calls will return false, until the button is released. Then, since the button is not pressed, it will return false until the button is pressed again.

   

With a functionality like this the switching of the lead becomes a VERY easy  one-liner:

   

if(ButtonPressed)LED1_Write(LED_State = !LED_State); // Here some warnings could appear.

   

So most of the needed code goes where it belongs, into the ButtonPressed() routine.

   

This example shows that the complexity of a "simple" button can be more as suspected at first sight and has the advantage that when the complexity changes (de-bouncing) it can be kept where it belongs to.

   

Sounds a little bit as if I miss the ++ in PSoC C

   

 

   

Bob

0 Likes
Reply
Esteemed Contributor

Can you post project ?

   

 

   

hli, yes Bobs approach elegant, if button pressed inside a loop, it

   

will flash LED at a high rate as long as button is held down. However

   

if added into simple state machine needed above would work great.

   

 

   

But then orginator says my code still stinks, so I cannot throw stones

   

 

   

Regards, Dana.

0 Likes
Reply
Esteemed Contributor II

Try this one...

   

 

   

Bob

0 Likes
Reply
Anonymous
Not applicable

I did not want to say anything wrong. I think I've been misunderstood. You spent time for me thanks.I think I've been misunderstood for my english is not good. Thanks bob I'm looking now.

0 Likes
Reply
Esteemed Contributor II

We are used to posters who are not native english-speaking. So I think we did understand you right.

   

When you would like to upload a project here, use the "Create Workspace Bundle" function of Creator and finally upload the archive using ms internet explorer, Chrome doesn't work and theree might be issues using other browsers.

   

 

   

Bob

0 Likes
Reply
Honored Contributor II

Basically what you need to to is top not only have the buttons current state, but also it previous one (from when you checked last time). Only when both differ, you need to to something (and in your case, only when the old state is 'not pressed' and the current state is 'pressed').

   

I'm now sad - I programmed this multiple times already, and nonetheless managed to forget about this 😞

0 Likes
Reply
Esteemed Contributor

Seems like my code logic partially worked, but not having a lot of

   

experience in Capsense, is button  "debounced" ?

   

 

   

I do see an error in logic for transition to off state, I did not

   

loop until key released, so it jumps right back to turning on

   

led. In fact both button presses should loop until key released

   

with a simple while loop testing state of key until it is released.

   

 

   

while ( statusButton0 == ON ) {};        // Loop until key released, add this statement after each key on event.

   

 

   

Regards, Dana.

0 Likes
Reply
Esteemed Contributor

Sorry for the delay, got sidetracked.

   

 

   

Here is the Cretor sample code modified to make one CSD button, attached to

   

P5_5 on -030 kit, toggle LED2 on kit.

   

 

   

The core code is lines 68 - 101 in main(). Note lines 68 - 70 cvan be made into a f()

   

and used for 68 - 70, 79 - 81, 97 - 99.

   

 

   

Regards, Dana.

0 Likes
Reply
Esteemed Contributor

For some reason I cannot post archive zip file in any of 3 browsers.

   

Post an address I can send to, and I will forward.

   

 

   

Regards, Dana.

0 Likes
Reply
Anonymous
Not applicable

this is the latest work.Everything is working properly but I don't press the buttpn program is repeated. Your helps that I proceeded

   

 thanks your helps

0 Likes
Reply
Esteemed Contributor II

The problem with your code is: when the routine UpdateButtonState() is finished while the finger still resides on the button it will be called again. Your delays will not cure that behaveour. Have a look at the sample-project I posted, there is a routine that only once returns a TRUE value the first time the button is pressed.

   

Some hints: You could improve your indentation which will make your program more readable.

   

Comments on a group of statements to indicate what you intend to do will make understanding easier.

   

The #defines at beginning of main() are very helpful!

   

 

   

An explanation to my posted project: I am using frequently constructs like

   

if((var1 = var2)) ...

   

or

   

return var1 = var2

   

I did that for testing (saving) code generation. Keep in mind that this is not a comparision but an assignment!

   

 

   

Bob

0 Likes
Reply
Esteemed Contributor

Here is what worked for me in case I cannot post archive this

   

morning -

   

    unsigned char onoff = 0;
 

   

        CapSense_CSD_UpdateEnabledBaselines();        /* Update all baselines */
        CapSense_CSD_ScanEnabledWidgets();            /* Start scanning all enabled sensors */
        while(CapSense_CSD_IsBusy() != 0);            /* Wait for scanning to complete */

        if( CapSense_CSD_CheckIsWidgetActive(CapSense_CSD_BUTTON0__BTN) && onoff==0) {

         
            LCD_PrintString("On ");
            LED1_Write(0x01);
            while ( CapSense_CSD_CheckIsWidgetActive(CapSense_CSD_BUTTON0__BTN) ) {
                           
                CapSense_CSD_UpdateEnabledBaselines();        /* Update all baselines */
                CapSense_CSD_ScanEnabledWidgets();            /* Start scanning all enabled sensors */
                while(CapSense_CSD_IsBusy() != 0);            /* Wait for scanning to complete */
            }
            onoff = 1;
             
        }

        //LCD_Position(1,3);

        if( CapSense_CSD_CheckIsWidgetActive(CapSense_CSD_BUTTON0__BTN) && onoff == 1) {

              CyDelay(200u);
              LCD_PrintString("Off");
              onoff=0;
              LED1_Write(0x00);
              while ( CapSense_CSD_CheckIsWidgetActive(CapSense_CSD_BUTTON0__BTN) ) {
                           
                CapSense_CSD_UpdateEnabledBaselines();        /* Update all baselines */
                CapSense_CSD_ScanEnabledWidgets();            /* Start scanning all enabled sensors */
                while(CapSense_CSD_IsBusy() != 0);            /* Wait for scanning to complete */
              }
        }
 

   

Again, you can see the 3 lines of code used repeatedly that can be turned into a f()

   

 

   

Regards, Dana.

0 Likes
Reply
Esteemed Contributor

One last comment, I adpated this from a Creator example, but did not

   

take out the code related to other parts of the example, like display

   

writes, etc.. So just extract the code I showed and adapt for your needs.

   

 

   

Regards, Dana.

0 Likes
Reply
Anonymous
Not applicable

hey Yilmaz, i also got same problem .did you get solution??Please Help if you got solution for that. thank you

   

Please Mail me the solution.

   

manojec9396@gmail.com

0 Likes
Reply
Anonymous
Not applicable

Hello Bob,

   

Please Help me for Capsense Slider Programe for Motor Speed Control.Please Tell me code for that. thank you

0 Likes
Reply
Esteemed Contributor II

There are already examples for capsense sliders. Go and install the pioneer kit files. This will give you access. to those examples. Alternatively use at Creator start page the item "Find code example" and enter "capsense".

   

 

   

Bob

0 Likes
Reply
Anonymous
Not applicable

Hello Bob, i used that example for slider .but i don't know why the slider acts automatically without touch..is there any sensitivity problem??.please tell me important parameter settings for slider and button in capsense. Thank you..

0 Likes
Reply
Esteemed Contributor II

"why the slider acts automatically without touch" This seems to be a button layout / cable problem. Already seen this abstract?

   

 

   

Bob

0 Likes
Reply
Anonymous
Not applicable

ok .Thanks bob.

0 Likes
Reply
Anonymous
Not applicable

hello all dear guys, i need help to build ir reciver tsop(remote controlled device) using Psoc Creator 4.

   

but i don't know how to do in psoc.please please help me 

   

 

   

thanks

0 Likes
Reply