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!
Can 2 switches be ANDed ?????
- Steve
- Matrix Staff
- Posts: 3433
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
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.
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.
-
- Posts: 112
- Joined: Wed Oct 12, 2005 6:29 pm
- Location: USA
- Been thanked: 1 time
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
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
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.
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.
-
- Posts: 112
- Joined: Wed Oct 12, 2005 6:29 pm
- Location: USA
- Been thanked: 1 time