So here's the final Arduino code - works perfectly. I'll post a movie of the sequences functioning with the .txt file and output from processing when I get a chance.
I guess this could be used for turning relays on and off, or driving servo's. The data stored in the .txt file doesn't necessarily need to be in sequence.
I'll add a delay variable to the .txt file as well - but for what it does already, suits my needs
// Adapted Code for using a 74HC595 Shift Register
// to Read values for LED's from .txt file sent from processing
//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;
int firstByte; // value for Red Leds
int secondByte; // value for Green LEDs - all mine are Red but I refer to the second group of 8 Red LEDs, Green LEDs
int val;
int serialInArray[3]; // array for storing 3 bytes as they arrive from processing
int serialCount = 0; // for counting the number of bytes received
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
serialInArray[serialCount] = Serial.read(); // read a byte sent by processing
serialCount++; // increment number of bytes received
if (serialCount > 2 ) { // when 3 bytes received
firstByte = serialInArray[1]; // get value for Red LEDs
secondByte = serialInArray[2]; // get value for Green LEDs
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, firstByte);
shiftOut(dataPin, clockPin, secondByte);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, 1);
serialCount = 0; // reset byte counter to zero
}
}
}
// the following code from the original tutorial (http://www.arduino.cc/en/Tutorial/ShftOut23 )
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOutÉ
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}
Final revision for processing
/**
* Load LED Settings
*
* Load .txt file that contains values for RED and GREEN LEDs separated by a tab ('\t').
* If the .txt file contains a sequence of values, the pairs are loaded until eof.
*
*/
import processing.serial.*;
Serial port;
String[] colorrows;
String[] myhexstr;
int myindex = 0;
PFont body;
void setup() {
size(200, 200);
background(0);
stroke(255);
frameRate(10);
body = loadFont("TheSans-Plain-12.vlw");
textFont(body);
colorrows = loadStrings("settings.txt");
// Open the port that the board is connected to and use the same speed (9600 bps)
println(Serial.list());
port = new Serial(this, Serial.list()[1], 9600); // port #1 COM4
}
void draw() {
if (myindex < colorrows.length) {
String[] thecolor = split(colorrows[myindex], '\t');
if (thecolor.length == 2) {
text("Red " + thecolor[0] + " Green " + thecolor[1], 20, 20 + myindex*20); // Print LED settings to console
int myredintval = unhex(thecolor[0]);
int mygreenintval = unhex(thecolor[1]);
char myredcharval = char(myredintval);
char mygreencharval = char(mygreenintval);
port.write('A');// send A as first byte
delay(10); // small delay so we don't choke
port.write(myredcharval); // send red
delay(10); // small delay so we don't choke
port.write(mygreencharval); // send green
}
myindex = myindex + 1; // Grab the next pair
if (myindex == colorrows.length) {
colorrows = loadStrings("settings.txt");
myindex=0;
delay(500); // Delay of 500 milliseconds before redrawing
background(0);
redraw();
}
}
}
My settings.txt file ended up looking like this, it was fun to play with.
FF FF // turn them all on
0 0 // turn them all off
6 12 // turn Red LEDs 2&3 on - turn Green LEDs 3&4 on
10 18 // etc
12 22 // etc
14 25 // etc