This is some of the code we are using on the Arduino to receive data from the PC and to write it out to the register:
//---------------------------------------------------------
// read (and print back) full strings from the serial port
// for more info www.progetto2501.com/b/tools/Arduino
//----------------------------------------------------------
boolean readSerialString () {
int i = 0;
// int n = 0;
int z = 0;
int myByte = 0;
if(serialAvailable()) {
while (serialAvailable()){
myByte = (serialRead() - '0') + myByte *10;
i++;
if(i%3 == 0){
dataArray[z] = myByte;
z++;
myByte = 0;
}
// myByte = ;
}
}
Serial.flush();
if(z == anzRegister){
return true;
} else {
return false;
}
}
//----------------------------------------------------------
//----------------------------------------------------------
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
[...]
}
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
Serial.begin(9600);
beginSerial(9600); //setup serial conversation at 9600 bauds
printString("Yes, I'm hearing you.");
printByte(0);
}
void loop () {
//try to read the serial port and create a string out of what you read
if(readSerialString()){
//Anzeigen
printString("Arduino heard you.");
printByte(0);
int j = 0;
digitalWrite(latchPin, 0);
for (int b = 0; b < anzRegister; b++) {
//jetzt das entsprechende feld aufleuchten lassen
//ground latchPin and hold low for as long as you are transmitting
//load the light sequence you want from array
data = dataArray[b];
shiftOut(dataPin, clockPin, data);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
}
digitalWrite(latchPin, 1);
Serial.flush();
}
delay(100);
// serInString[0] = 0;
}
What happens when it is working is this:
1. PC sends command to Arduino
2. Arduino RX flashes
3. Arduino shifts out the information to the register
4. Arduino TX flashes
5. Arduino has replied with "heard you" which is received by the PC
What happens when it is not-working is:
1. PC sends command to Arduino
2. Arduino RX flashes
3. --- nothing else ---
Sometimes it works, sometimes it doesn't, even when running at the same port speed. It is working perfectly while using a program designed for testing the whole setup - manually. It is not working properly when information is sent by the main program in faster intervals of 0.5s...
We thought that might be connected to the port speed but that does not seem to change anything, or would it?
Thanks!