Can 2 switches be ANDed ?????

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
7omos
Posts: 2
Joined: Sun Sep 24, 2006 2:30 am
Contact:

Can 2 switches be ANDed ?????

Post 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!

User avatar
Steve
Matrix Staff
Posts: 3421
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post 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.

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Post 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

7omos
Posts: 2
Joined: Sun Sep 24, 2006 2:30 am
Contact:

Post 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.

jimhumphries
Posts: 112
Joined: Wed Oct 12, 2005 6:29 pm
Location: USA
Been thanked: 1 time
Contact:

Post by jimhumphries »

7omos:

Glad that helped and thanks for the feedback.

Jim

Post Reply