Help with code
I am needing some help with the output of a 74HC165 that I have connected to my solar tracker. The code I am using is from an old project and I pretty much copied and pasted the code but don't quite understand how to pull 1 specific "switch" value from the switches variable.
Apparently xInputs or switches cannot be "accessed" like an array? There are 7 switch inputs and I want to check when any are pressed. I know I can use the CASE and then use "%0000_0010", but there are too many variations with switch press combinations. What am I doing wrong?
*** Edit:
I figured it out. I changed it to this :
I am going to bed.... it is WAY past my bed time.....2am....yeah....
VAR
BYTE xInputs
BYTE switches
BYTE WestSwitch ' Max distance switch
PUB ReadSwitches
xInputs := 0 ' clear variable to hold input states
outa[Lever_LOAD] := 0
outa[Lever_LOAD] := 1
repeat 8
xInputs <<= 1
xInputs += ina[Lever_DATA]
outa[Lever_CLK] := 1
outa[Lever_CLK] := 0
switches := xInputs ' Move updated values to new variable to prevent other code from picking up wrong data while processing inputs
if switches[5] == 0 ' ********* This is where I am confused. **********
WestSwitch := 1
else
WestSwitch := 0
Apparently xInputs or switches cannot be "accessed" like an array? There are 7 switch inputs and I want to check when any are pressed. I know I can use the CASE and then use "%0000_0010", but there are too many variations with switch press combinations. What am I doing wrong?
*** Edit:
I figured it out. I changed it to this :
VAR
BYTE switches[8]
PUB ReadSwitches | i
outa[Lever_LOAD] := 0
outa[Lever_LOAD] := 1
repeat i from 0 to 7
switches[i] := ina[Lever_DATA]
outa[Lever_CLK] := 1
outa[Lever_CLK] := 0
if switches[5] == 0
WestSwitch := 1
else
WestSwitch := 0
I am going to bed.... it is WAY past my bed time.....2am....yeah....

Comments
pub rd_bit(value, pos) '' returns bit (0..1) of value.pos if ((pos => 0) and (pos =< 31)) return (value >> pos) & 1 else return 0If you want to assign the value of switches.bit5 to westswitch, you would do this:
If you don't want to be bothered with the method you can manually code it like this:
I've attached my '165 object which will let you keep isolate the 165 from the application data (i.e., setting westswitch).