setting pin HIGH or LOW with varible

Marcwolf

Senior Member
Hi Folks
Been trying to work out how to do this..

I have a variable b0 which is either 1 or 0, and I want to set the state of pin to that value. i.e. Set B.7 = b0

Because I cannot use a set range of pins like B or C I cannot use the Let Pins command.

Any suggestions, it will save a pile of extra coding with IF's etc

Many thanks
Dave
 

Svejk

Senior Member
use :

b.7 = bit0

it should do the trick.

Edit: it doesn't unfortunately. But this does the trick

if bit0 = 1 then high b.7 else low b.7 endif
 
Last edited:

Marcwolf

Senior Member
Hi Svejk

I know that 'if' part works as I am using that now. But I have 8 pins to set and that is a lot of 'if' to cycle through

The project is a 'musical cloak using an 18M2, a Spectrum Sheild from Sparkfun, and a El Escudo also from Sparkfun.

The idea is that I have a small microphone and amplifier built into the cloak. When music is playing nearby the spectrum shield and 18M2 will break the music down into 7 spectrum ranges, and then depending on an average threshold of the values it will then turn on or off EL wires via the El Escudo.

With the average I can easily work out if a pin needs to be set on or off... But that is a lot of IF statements to track through.. being one for each output.

Take Care
Dave
 

eclectic

Moderator
re post #4
This works for real
Code:
#picaxe 18M2
dirsB = %10000000
;LED on B.7, for test
Do
b0 = 1
PinB.7 = b0
pause 1000
b0 = 0
pinB.7 = b0
pause 1000
loop
e
 

Marcwolf

Senior Member
Hi eclectic
Many many thanks

It worked. It was the DIRS that was eluding me. I now have some nice fast coad that can keep up with disco music.

Take Care
Dave
 

westaust55

Moderator
But I have 8 pins to set
Seems you want to set the 8 bit variables comprising b0 and then use b0 to set the port.

For example:

Code:
dirsb = %11111111
; set the bit variable through your program as needed
bit0 = 1
bit1 = 1
bit2 = 0
bit3 = 1
bit4 = 0
bit5 = 1
bit6 = 1
bit7 = 0
;
; then you can set the pins from some point in the program - called by a subroutine if for example pins are set in many different places.
pinsb = b0
 

Marcwolf

Senior Member
Hi West

That would work.. if all of the pins were from the same bank. But if one has 4 pins from bank C, and 4 pins from bank B then it a little more difficult.

take Care
Dave
 

westaust55

Moderator
knowing that b.0, b.1, b,2 etc are pre defined constants
ie
b.0 = 0
b.1 = 1
b.2 = 2
:
:
b.7 = 7
c.0 = 8
c.1 = 9
:
:
c.7 = 15, etc

then you may be able to calculate the pin to make high or low and if not in a sequence maybe use the LOOKUP command as a cross reference mechanism.

But we are guessing to help further without knowing the full scenario and which pins are in fact being used.
 

hippy

Technical Support
Staff member
if one has 4 pins from bank C, and 4 pins from bank B then it a little more difficult.
The easiest solution is to manipulate variable 'b0' rather than an output port ( you can use 'bit' variables to set individual bits ) and then move them one at a time to the output pins -

b0 = some value
pinC.3 = bit0
pinC.1 = bit1
pinC.2 = bit2
pinC.0 = bit3
pinB.7 = bit4
pinB.6 = bit5
pinB.5 = bit6
pinB.1 = bit7

Wrap that in SETFREQ for more speed and better 'simultaneous' updates, or copy bits into other variables and update all bits for each port simultaneously. Put it in a subroutine then you only need to GOSUB that to set the outputs.

This is a standard trick to use when you find that your hardware pin allocation doesn't quite match the way you'd like your software to work or vice-versa, and also a good way to write code anyway ( only updating variables until put to output ports ). You can write code which works on any hardware and only the output routine needs to change no matter what the hardware is.

This is usually what people mean by a "Hardware Abstraction Layer" (HAL) and allows you to write code even if you haven't got actual hardware, haven't decided what you will be using.
 
Top