Converting input pin settings to decimal/binary numbers then to outputs

WhiteSpace

Well-known member
I'm still working on the infrared remote control vehicle for which some of you have helped on various aspects. I'm making good progress and the motor controls seem fairly stable now after all sorts of teething troubles. It's all a bit quicker now after moving the current sense reading, voltage reading and display (in fact everything except the IRIN and the motor control) off to a second Picaxe. Now I would like to implement remote control for accessories such as headlights or a winch or whatever. I have some space on top of the motor controls in the IROUT/IRIN controls. The motor controls take up 0-103 of IROUT (it currently transmits left motor PWM and direction controls in the range 0-51 and right motor 52-103), so I still have 25 IROUT commands to play with up to the total 128. I'm thinking of adding a number of on/off switches to the transmitter. Let's say there are 4. If switches 1 and 2 are on, and 0 and 3 off, that gives binary 0110, I think, which I think is decimal 6. If I add that to 104 to transmit a 3rd unique IROUT value, then I can decode it at the receiver chip, convert to binary and that will instruct output pins 1 and 2 on, turning on the headlights and whatever else. I'm a bit stuck as to how to do all of that, though. On the transmitter side, I think I can do let b1 = pinsX , although according to the manual, that's based on 8 pins. Then let b1 = b1 + 104, and then IROUT b1. Then how do I unscramble that at the receiver end? Is it just a case of let b1 = b1 - 104 and let pinsX = b1? Can I mix up binary and decimal numbers like that? All thoughts gratefully received. Thanks very much.
 

Technical

Technical Support
Staff member
You need to remember that any pinsB statement always controls/reads all 8 pins.

So to read you need to mask off the 4 bits of importance, so something like

let b1 = pinsC & %00001111 + 104

On the receiver end it is a bit more complex, as using outpinsB = xxx will control all 8 bits, not just the lower 4.
If you use b0 you can use something like this instead (assuming the pins are already set as outputs via let dirsB = xxx)

let b0 = b0 -104
let outpinB.0 = bit0
let outpinB.1 = bit1
let outpinB.2 = bit2
let outpinB.3 = bit3
 

lbenson

Senior Member
If you are reading 8 pins with "b1=pinsX", then what you will need to do is isolate the pins you are interested in so the values of the other pins don't interfere. In the easiest case, say for pinsC, suppose the pins of interest are C.0, C.1, C.2, C.3, then you can mask out the high pins by saying "b1=pinC & %00001111". Then if as you suggest, you just add that value to 104, you're not going to get just 4 possible values, but 16--0-15, $0-$F, %00000000-%00001111.

Is that what you want? If so, then yes, you can just subtract 104, with the result in, say, b1, and look at bits, e.g., if bit8=1 then ..., if bit9=1 then ....

If the bits you read don't start with, say, C.0, but are contiguous, you can just right shift the value; if C.2-C.5, then b1=pinsC/4 achieves that shift.

If they're not contiguous, you might map them by brute force, e.g., for C.1, C.4, C.5 and C.7 you'd say:
b1=pinsC
bit8=bit9
bit9=bit12
bit10=bit13
bit11=bit15
b1=b1 & %00001111 ' to mask off the top bits
 

Aries

New Member
Can I mix up binary and decimal numbers like that? All thoughts gratefully received. Thanks very much.
You can "mix up" numbers in any format as much as you like - they are all handled internally in exactly the same way. So, something like
b0 = 97 + $0F - %0110
is perfectly acceptable (if somewhat harder to read). There are certainly cases (as in the other examples above) where using binary or hex representations is easier to follow, particularly when looking at individual bit settings (e.g. %00001111 in Technical's example)
 

WhiteSpace

Well-known member
Thanks very much Aries. That’s good to know. It looks as though mixing up the number formats wasn’t the difficult part after all, but I need to be careful about properly addressing the pins and masking/unmasking them. I’m grateful to all of you.
 
Top