LED ON-OFF USING SINGAL CAPSENSE SWITCH.

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

cross mob
Anonymous
Not applicable

Greetings to all,

   

 one switch control the led On at time one press
next press the LED will OFF
please help how can i create it.

   

switch is push type of switch.

   

following is the module to be modify.

   

// input to display is switch press.

   

void display(BYTE switsh_state)
{

   

LED( (switch_state & 0x01) );

   

}

   

Pramod.

0 Likes
12 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You have several different states, so best would be to use a state-machine approach:

   

State 1  - Idle, Switch is released, LED is Off

   

State 2 - Active Switch is pressed, LED is On

   

State 3 - Waitng for second press, switch is released, LED stays On

   

State 4 - Active, Switch is pressed again, LED Is Off

   

The next state, when the switch is released is State 1.

   

... and do not forget to debounce your switch.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Some help on debounce, attached.

   

 

   

Regards, Dana.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I have attached code for refrence, code successfully making LED On after first press, but then it continusly remain on their is no effect of second press. I try out many changes but unable to make LED OFF at second press. 

   

Switches are capsense switches.   Can any one suggest suitable changes.

   

Pramod.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Just looking at part of code, this -

   

 

   

    

   

          

   

  if (sensor_state & 0x01==1)

   

 

   

be typed as

   

 

   

    

   

   

          

   

  if ( ( sensor_state & 0x01 ) == 1 )

   

 

   

per precedence order in C

   

 

   

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Missed in prior post, attached.

   

 

   

Regards, Dana.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I usually I try to avoid logical comparisions in C, so I would have written

   

 if (  sensor_state & 0x01 ) ....:

   

since I am not interested whether the result is equal to one, but if it is not equal to zero which is per definition TRUE

   

As a side-effect when ANDing with a bit-mask and I am interested in any of the masked bits set the syntax would be the same:

   

 if (  sensor_state & BitMask ) ....:

   

 

   

Happy coding

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Fear not Bob -

   

 

   

if ( ( sensor_state & 0x01 ) == 1 )

   

 

   

& is a C bitwise operator

   

 

   

== is a C Relational and Equality Operator

   

 

   

So no logical test occured !

   

 

   

But then we both used an "if", surely that is a logical ? But C seems to think that

   

is a statement.

   

 

   

😞 I am so worried now, all that code I have out in the field...........

   

 

   

Regards, Dana.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I would call "==" ,"!=",>" and so on to be logical operators in opposite to a bitwise operators as "^","~" and so on.

   

 

   

Bob

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Forgot to mention that logical operator's results are always "FALSE" or "TRUE" or rather 0x00 and 0x01

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

I used these references -

   

 

   

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

   

http://msdn.microsoft.com/en-us/library/z68fx2f1.aspx

   

 

   

and my C book.      http://zanasi.chem.unisa.it/download/C.pdf

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Thanks to all.

   

Manage to solve above problem.

   

Pran.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You are always welcome, Pran.

   

@Dana

   

We are probably discussing about language differences and not C-syntax, My (British) C-book shows: publications.gbdirect.co.uk/c_book/chapter3/logical_expressions.html

   

Bob

0 Likes