#### Equip your Genius

Welcome to the Parallax Discussion Forums, sign-up to participate.

# Writing to the ports

Posts: 6

BS2 question:

It has been a long time since I wrote anything on the BASIC Stamp... so forgive me please for forgetting most of it...

That said, I am trying to figure out how I can write a byte value to ports.

The idea is to generate a random number, then write the three least significant bits to three port in succession.

The ultimate goal is to generate a random RGB color from LEDs (driven by a set of relays) based on the values of least-significant bits in a random number.

The process would be placed in a loop thus creating a sequence of non-repeatable colors on a lighting fixture as a game show effect.

Getting the least significant contents of a random number into three ports elegantly is the task...

## Comments

• Posts: 6
OOPS!

I discovered that you can't get there from here....

That said, I am taking another, less magical tack with my project.

I'll post the resulting BASIC Stamp device on this thread later.
• Posts: 496
Sure you can. This writes the lowest 4 bits of a random number x to P0-P3 every second, of which you only use P0-P2 for your LED.
```x  VAR  Word

DIRS = \$0007     ' set P0-P2 as output

DO
RANDOM x       ' generate a random number x
OUTA = x       ' write lowest 4 bits to P0-P3
PAUSE 1000     ' delay for 1 second
LOOP
```

Writing 4 bits is done in one instruction and all the bits will change at the same time.
Sapphire
• Posts: 1,689
Yes exactly.
The large blue glowing thing is indeed right.

Oh and welcome back to the board and the firm.

And this message is being sponsored by the New Rebel Alliance
• Posts: 6
Sapphire wrote: »
Sure you can. This writes the lowest 4 bits of a random number x to P0-P3 every second, of which you only use P0-P2 for your LED.
```x  VAR  Word

DIRS = \$0007     ' set P0-P2 as output

DO
RANDOM x       ' generate a random number x
OUTA = x       ' write lowest 4 bits to P0-P3
PAUSE 1000     ' delay for 1 second
LOOP
```

Writing 4 bits is done in one instruction and all the bits will change at the same time.

i KNEW there was a solution!

Thanks, Sapphire!

Next question:

I want to drive a set of three DIP-format relay with the above pins. the coil resistance is 960 ohms. can I drive them directly? And should I run them VDD or VSS?
• Posts: 6
Also - OUTA is not listed in the BASIC Stamp Online Command Reference which is displayed by selecting :Help-PBasic Command Reference.

That is probably one reason why I could not find a solution...

Hmmmm.
• Posts: 6
Yikes!

I just found an answer to my "drive relays directly" and the OUTA questions.

According to an article I found on the output loads for a BASIC stamp, the minimum resistance across an output pin should be no less than 1000 ohms

OUTA is one of the several commands for moving variable values to ports OUTA loads the least significant 4 bits of a byte into selected ports OUTB loads the upper 4 bits of a byte - etc.

It is all coming back to me!

Thanks!
• Posts: 1,450
acker_dackerly,

You should always refer to the BASIC Stamp Manual as the online help is not as comprehensive.
https://www.parallax.com/downloads/basic-stamp-manual
• Posts: 6
Genetix wrote: »
acker_dackerly,

You should always refer to the BASIC Stamp Manual as the online help is not as comprehensive.
https://www.parallax.com/downloads/basic-stamp-manual

thank you!
Sign In or Register to comment.