PDA

View Full Version : Toggle switch (latching switch) state



craigwb
- 4th November 2011, 07:50
Hi All

Can someone Please help me with a Toggle or Latching switch State that I am battling with and not getting right.
I have 6 Toggle switches (Float Switches in a tank) which i need to read the state of all the time 0 or 1. They are either 0 or 1, one can be triggered or more than one together. I have setup my code and can read a standard momentary switch but, I cannot seem to read a latching switch, What I mean is I can read the switches but, I have a problem when they are latched in a fixed position either 0 or 1 for a long period of time.

How do I only read the switch once and ignore it until it changes state and then only read it once again and ignore it until it changes state later on. What I also need to do is trigger a 433mhz Transmitter only once with the updated info on the switches and then let the Tx sleep until another update is ready to be sent.

If you could Please help and if you maybe have some sample code I am sure that I will be able to figure it out?
I am still fairly new to programming!
Kind Regards

Craig

HenrikOlsson
- 4th November 2011, 09:10
Hi,
If you only want to read it once when it changes you have to know that is has changed and the only way to know that is to read it (or use the Interrupt on change feature) The easiest way is yo keep reading the switches in a loop and compare the current state to the previous state, if they are not the same one or more switches have changed state. Lets say your switches are connected to PortB.0 thru PortB.5, then perhaps something like:
Switches VAR BYTE
oldState VAR BYTE
TRISB=%00111111 'PortB.0-5 are inputs.
oldState = 0
ReadSwitch:
Switches = PortB & %00111111 'Get 6 low bits.
IF Switches <> oldState THEN ' Has it changed?
GOSUB ActivateTx ' Deal with it.
ENDIF
oldState = Switches ' Remeber current state
Goto ReadSwitch ' Do it again.

ActivateTx:
' Do what needs be done with the transmitter
' and anything else here
RETURN

/Henrik.

Jerson
- 4th November 2011, 13:11
Henrik has suggested one possibility. Another is to use a XOR function.

The simple pseudo code for this would be


MyLoop:
Keys = Read_switch_now
if (PrevKeys XOR Keys) then
gosub KeysHaveChanged
endif
PrevKeys = Keys
goto MyLoop


Cheers

craigwb
- 4th November 2011, 13:21
Hi Henrik

Thanks for the code, Set up your example and it works fine, I have to read each Switch separately and then send individual info on each switch level when it changes.
I have done a small sample using your code but, I know that there will be different variables with the state of the switches eg: �1101 or 0100
I have put a sample for an individual bit too look at in my code and then print on LCD when it matches but, I know that there can be many different states of the bits present at any time.

Is there a better and simpler way that I can look at an individual bits condition and react on it?
Please have a look at my piece of code to better understand what I mean

Thanks for the help!
Craig

Switches VAR BYTE

oldState VAR BYTE
Oldstate = 0

ReadSwitch:
Switches = PORTA & 111 ' Get 5 low bits.
Print At 1,1, Bin Switches
If Switches <> Oldstate Then ' Has it changed?
GoSub ActivateTx ' Deal with it.
EndIf
Oldstate = Switches ' Remeber current state
GoTo ReadSwitch ' Do it again.

ActivateTx:
Print At 2,1, "BIT= "

If PORTA.1 = 0 Then ' When Bit 1 Changes State Print "0" on LCD
Print At 2,7, "0"
EndIf
If PORTA.2 = 0 Then ' When Bit 2 Changes State Print "1" on LCD
Print At 2,7, "1"
EndIf
If PORTA.4 = 0 Then ' When Bit 4 Changes State Print "2" on LCD
Print At 2,7, "2"
EndIf

Return


End

craigwb
- 6th November 2011, 14:18
Thanks Jerson

For the piece of code I only now got a chance to try it on my application and it works like a dream.
When I have finished writing and testing my code in a few days I will post a piece of code showing
what I have done. Hopefully this will assist people in the future with a similar project.

Thanks again for all of the help

Regards

Craig