Page 1 of 1

Can 2 switches be ANDed ?????

Posted: Sun Sep 24, 2006 2:53 am
by 7omos
Hello Everybody!
I have a problem to solve in FlowCode. I will explain what i really need and then i welcome any reply:

I have a PIC16F877. I have 4 switches on port C and i have 3 LEDs on Port D.
I can make one LED go on when i press switch 1 (thats easy) but what i cant make is when switch 1 and switch 2 are pressed TOGETHER the second LED is on and the first one goes off!

Please help me!

Posted: Mon Sep 25, 2006 10:38 am
by Steve
There are a number of different ways to do this.

The quickest (but hardest?) way is to use the masking feature of the input icon (entire port... use masking... and tick the bits which your switches are connected to). You would also need to declare a variable (e.g. "State") to accept the result. Then you would have a decision icon with "if State <> 0" as the decision.

Alternatively, you could use read in the states of each switch individually (using variables "S1" and "S2") and then use "nested" decision icons.

I hope this helps.

Posted: Mon Sep 25, 2006 6:16 pm
by jimhumphries
7omos:

If I needed to do this fast I would:

Read port C and assign that value to a variable, NUMBER (for 4 bits, NUMBER can be any value between 0 and 15).
Test NUMBER:
Is NUMBER = 1? – yes (switch 1 is closed) – output 1 on port D (LED 1 is lit).
No
Is NUMBER = 2? – yes (switch 2 is closed) – output x on port D.
No
Is NUMBER = 3? – yes (switch 1 and 2 are closed) – output 2 on port D (LED 2 is lit).
No
Etc………….

This is not an elegant way of doing it but it has some advantages. It is literal and very easy to understand, it is easy to do and, because it is basically a look-up table, it allows any arbitrary correspondence you want between input and output.

Jim

Posted: Thu Sep 28, 2006 4:37 pm
by 7omos
I would like to thank Jim and Steve for their help!

I have tried Jim's Method and it really helped me!

I could AND two switches using this method.
I have AND switch 1 and switch 2 by declaring the variable SWITCH is equal to 3. I also AND switch 1 and switch 3 by declaring the variable SWITCH is equal to 5. I also AND switch 1 and switch 4 by declaring the variable SWITCH is equal to 9.

Thank you Jim.

Posted: Thu Sep 28, 2006 11:35 pm
by jimhumphries
7omos:

Glad that helped and thanks for the feedback.

Jim