4051 and 595 problems

hi, ive asked this at the "interfacing with software" forum but got no replay, and this is really really important, so i try again.. i hope i dont nag :slight_smile:

ive successfully connected 5 4051´s to input 34 switches and pots and 3 595´s to control a total of 20 leds. the code is assembled of two different codes ive found on littlescale´s blog. i read and write the data through max. everything works fine, but if i send arrays to the led´s in fast intervals, they freeze or go crazy... i guesse it has to do with the arduino code (i admit, im not a programmer, but i really wanna learn!) so if sombody could please please help me,or atleast shed some light, i would be more than greatful!

here is the code:

//Pin connected to ST_CP of 74HC595

int latchPin = 8;

//Pin connected to SH_CP of 74HC595

int clockPin = 12;

////Pin connected to DS of 74HC595

int dataPin = 11;

byte data;

byte data1;

byte data2;

byte data3;

void setup() {

Serial.begin(57600);
DDRD = B00011110;
pinMode(latchPin, OUTPUT);

}

void loop() {
for(int i = 0; i < 40; i++) {

PORTD = (i % 8) << 2;

data = (analogRead(i / 8)) / 4;

Serial.print(data);
}
delay(5);

if(Serial.available() > 2) {

data1 = Serial.read();

data2 = Serial.read();

data3 = Serial.read();

digitalWrite(latchPin, 0);

shiftOut(dataPin, clockPin, data1);

shiftOut(dataPin, clockPin, data2);

shiftOut(dataPin, clockPin, data3);

digitalWrite(latchPin, 1);

}

}

void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {

int i=0;

int pinState;

pinMode(myClockPin, OUTPUT);

pinMode(myDataPin, OUTPUT);

digitalWrite(myDataPin, 0);

digitalWrite(myClockPin, 0);

for (i=7; i>=0; i--) {

digitalWrite(myClockPin, 0);

if ( myDataOut & (1<<i) ) {

pinState= 1;

}

else {

pinState= 0;

}

digitalWrite(myDataPin, pinState);

digitalWrite(myClockPin, 1);

digitalWrite(myDataPin, 0);

}

digitalWrite(myClockPin, 0);

}

im starting to think that it has to do with the delay(5);
and that im sending data faster than it could handle... could it be that im right? anyways... i would really like to learn... thanks for your time..

I think your problem is:-

i read and write the data through max

The Arduino stuff looks fine as far as one can tell but it might be screwing up at the max end. There are not so many max users here that are experienced which maybe accounts for you lack of replies.

If you think you are sending data too fast try adding a small delay before and after (play with the value to see if it works and perhaps you can suppres one of them)

digitalWrite(myDataPin, pinState);
delay(1);
digitalWrite(myClockPin, 1);
Delay(1)
digitalWrite(myDataPin, 0);

thanks! i will try it right away...allthough the funny thing is, that if i use only the 595 part of the code, it works fine... the problems appear when i integrate the 4051 part..

ok, it didnt help... i now realize that what exactly happens when i send arrays rapidly to the 595´s is that the order of the leds is messed up, though they continue to function...