Wednesday, January 16, 2008

Talking to 3 595's via Max/MSP

This is a follow up to a previous post regarding the connection of three 595 registers to an Arduino board and is a response to a question someone has asked me. What i did not cover in that post is how to actually make the connection between a host computer and the shift registers.

Let us review quickly how the Arduino sketch passes on the data to the 595 registers. Basically, the Arduino waits until its serial buffer has received three bytes. Then, each of these bytes is read and shifted out into the registers. The first byte will go to the 595 IC that is furthest away from the Arduino in the serial input / Q7' output chain (see the previous post for the hardware setup). The baud rate is 57600.

The 8 bits from each byte represents each of the outputs of the 595 register, like so:

bit 7 = 595 output Q7 (pin 7)
bit 6 = 595 output Q6 (pin 6)
bit 5 = 595 output Q5
(pin 5)
bit 4 = 595 output Q4
(pin 4)
bit 3 = 595 output Q3
(pin 3)
bit 2 = 595 output Q2
(pin 2)
bit 1 = 595 output Q1
(pin 1)
bit 0 = 595 output Q0
(pin 15)


So, it is possible to use an 8 bit value (from 0 to 255) to independently represent the 8 digital output pins Q0 to Q7 on the 595. It follows that we can control the three 595 shift registers quite easily from Max/MSP with a patch such as this one:


The main patch.



The sto.8b1b encapsulation that is referenced in the main patch.

How does this patch work? Well, twenty-four toggles each represents a bit in three bytes of information. Whenever the value of a toggle is changed, then the entire updated byte of information is sent out of the bottom of the sto.8b1b encapsulation as an integer (0 to 255).

These three bytes need to be sent for every change that occurs within the three bytes because the Arduino is expecting to see three bytes of serial data in order to set the three shift registers. Furthermore, these three bytes must be sent in a particular order - the third shift register must be sent first, the first shift register must be sent last.

In order to get the proper sequence of bytes, the three values are grouped together in a list via the pack object. The list in then unpacked, and because of Max's right-to-left order, the third item in the list is sent to the serial object first and so on.

The serial object is connected to the Arduino at a baud rate of 57600.

It is evident that this may be an inefficient means of updating the three shift registers, because three bytes must be sent in order to change a single bit of information output. This may suffice in many situations. But it is possible to use a numbering prefix (1 to 3) to control the register. This would require a reworking of the Arduino code.

Download this patch here: http://milkcrate.com.au/_other/downloads/max_patches/Three_595s/.


22 comments:

Anonymous said...

thanks man! hope its not a lame question; but what exactly is happening inside the sto. encucsulation? :)

Sebastian Tomczak said...

Nah, not a lame question at all.

Sto.8b1b stands for "Sebastian Tomczak Object, 8 bits in 1 byte out". It takes each incoming bit, shfts it left by the appropriate amount and adds all of the outcomes together. So, if we send it the following bits (from bit 7 to bit 0) B01000001, this will return 65. Because:

bit 0 << 0 = 1
and
bit 6 << 6 = 64
then
1 + 64 = 65

It combines all of the 8 bits into a single byte. The toggles are sending out a 1 for an on state and a 0 for an off state.

Hope this answers you question! When you download the patch, the 8b1b encapsulation will be visible as a separate patch and you can view it and edit it to see how it works.

Sebastian Tomczak said...

i also just added a screenshot of the 8b1b encap.

Anonymous said...

thanks, its working perfect!

Sebastian Tomczak said...

this is great! i am glad it is working for you.

Anonymous said...

thanks a lot for your work. i just wanted to download the patch but the .zip-file is gone and i'm sad.

Sebastian Tomczak said...

Okay, updated. URL got a bit changed with the recent server migration.

What are you building? Is it something super-cool?

Anonymous said...

thanks for the update. i want to control a pd-patch and a sequencer with LEDs as visual feedback so i don't need eye contact with the computer display. not that cool but helpfull and essential for me. and a good starting point.

Sebastian Tomczak said...

Sounds good.

You might also be interested in:
http://little-scale.blogspot.com/2008/01/32-led-fader-with-arduino.html

Or something. Yep.

Anonymous said...

LEDs are blinking after rebuilding your patch in puredata. thanks a lot.
today a friend and i had a look on the SimpleMessageSystem. we wrote a sketch file and a pd patch and it works too. now we want 32 buttons with the help of four 74HC165 chips. hopefully it will work.

Sebastian Tomczak said...

Word. Well, i am sure it will work. I guess the first thing is to get one working, and then link the serial out of the three others as a daisy chain along to the next serial ins. Let me know how it goes.

emp said...

Thank's a lot for this. Your blog is great and very inspirational. I've extended your example with yet another 595, so now i have 32 leds blinking. :-)

I'm a total novice when it comes to electronics so..
One question about current: Do you run this on USB power? Or do we need external power? It seems to work fine with USB alone, but i didn't dare trying for long..
So, basically, how do you calculate the mA:s used by 32 LEDs and 4 595s?

Emile M said...

Thanks for this, it was exactly what I was looking for. What I was wondering was- if I wanted to add more shift resistors, would I need to change the arduino code and the Max patch? Sorry if this an overly simplistic question, I am new to this stuff.

Sebastian Tomczak said...

yes, you would need to change the code etc, but not really by much.

Emile M said...

I know this is asking alot, but I was wondering if you could look at this code? It would really be appreciated.
To try and control 4 shift registers I have added:

1.byte data4;

2.data4 = Serial.read();

3.shiftOut(dataPin, clockPin, data4);


.pde here --
----------------
http://0000ff.org/image_dump/_4shiftregisters/_4shiftregisters.pde

Anonymous said...

Hi, thanks for this brilliant code. I am using it for my new midibox, but ive got stuck on this: "But it is possible to use a numbering prefix (1 to 3) to control the register.". Any chance you want to develop more on that?

Thanks in advance,
- Rickard

Unknown said...

hey great thank you kind sir! Just bought a shift register today. I was hoping I could just use the Pduino (or specifically Maxuino) code to interact with the arduino and the 595 but I need to upload a different code to the arduino??

Thanks regardless,
Matthew S Breadboard

Unknown said...

Hi,
thanks for sharing this!
this is great!!

but I have a question.

How can I mix your code (controlling three 595'3) and SimpleMessageSystem (like this -http://www.arduino.cc/playground/Interfacing/MaxMSP )

I meant I need Arduino's analog pins to control the 24 leds, but I don't know how to do, can you teach me?

Not matter what, thanks a lot! :)

Unknown said...

hello

i have a simlar question

I'm wondering how to control all the other digital and analog ins and outs on the arduino.
whilst using this shift register sketch code.

can we mix this code with maxuino code? or simple message code?

so we can unlock all the other ins and outs:?

: )

Unknown said...

hello

i have a similar question

I'm wondering how to control all the other digital and analog ins and outs on the arduino.
whilst using this shift register sketch code.

can we mix this code with maxuino code? or simple message code, so we can unlock all the other ins and outs :?

thanks
: )

Unknown said...

hello :) I have a similar question as above.

I wonder: How can we now have access to the analog ins and other digital pins? what needs to be done to the arduino sketch?

thanks for any help:)

Unknown said...

sorry to the blog owner for multiple posts.

i did not see the message
"Your comment has been saved and will be visible after blog owner approval."

it was in tiny text at the top of the screen.

sorry : )