set variable value depending on switch position

noycesd

New Member
I need to set the value of a variable depending on the position of switches on a number of input pins. I have tried if pin0=1 then let b7=1: if pin0=0 then let b7=0 but this does not work :(

I will have four inputs in total and need to ascribe a numerical value to a variable (b7) depending on the combination of outputs.

Any help appreciated.

Si
 

hippy

Ex-Staff (retired)
You're probably battling against syntax errors and such like; this is one way to do it ...

Code:
b7 = 0
If pin0 = 1 Then : Let b7 = b7 + 1 : End If
If pin1 = 1 Then : Let b7 = b7 + 2 : End If
If pin2 = 1 Then : Let b7 = b7 + 4 : End If
If pin3 = 1 Then : Let b7 = b7 + 8 : End If
Depending upon the settings of four switches to Input Pins 0 to 3, b7 will hold a value between 0 and 15.
 

hippy

Ex-Staff (retired)
You can put a newline where the : are or you can just use the : on the same line. It serves the same purpose but in cases like this it makes the lines more readable and udnrstandable. In most cases it usually is better to use newlines. Ultimately; whichever you prefer.
 

moxhamj

New Member
If you are able to configure the switches on the right pins, you can read it in with two lines:

let b0=pins' read in binary number
b7=b0 and %00001111' mask off 4 leftmost bits and move number into b7
 
Last edited:
Top