Hey,
As the title says, really. I've received my Arduino and successfully got a simple project up and running to allow me to send it data over the USB-Serial port to control the colour of some RGB LED strips.
It works perfectly at 9600 baud, but at anything higher than that it fails to read or write. Are there any known issues with this or anything I should bear in mind? For reference, I'm running on Mac OS X with Arduino 0022. If it helps, here's my sketch:
// It is assumed that the channels are connected RGBRGBRGBRGB from kChannel1RedPin.
const int kChannel1RedPin = 2;
// Protocol details (two header bytes, 12 value bytes, checksum)
const int kProtocolHeaderFirstByte = 0xBA;
const int kProtocolHeaderSecondByte = 0xBE;
const int kProtocolHeaderLength = 2;
const int kProtocolBodyLength = 12;
const int kProtocolChecksumLength = 1;
// Buffers and state
bool appearToHaveValidMessage;
byte receivedMessage[12];
void setup() {
// set pins 2 through 13 as outputs:
for (int thisPin = kChannel1RedPin; thisPin < (kChannel1RedPin + sizeof(receivedMessage)); thisPin++) {
pinMode(thisPin, OUTPUT);
analogWrite(thisPin, 255);
}
appearToHaveValidMessage = false;
// initialize the serial communication:
Serial.begin(9600);
}
void loop () {
int availableBytes = Serial.available();
if (!appearToHaveValidMessage) {
// If we haven't found a header yet, look for one.
if (availableBytes >= kProtocolHeaderLength) {
// Read then peek in case we're only one byte away from the header.
byte firstByte = Serial.read();
byte secondByte = Serial.peek();
if (firstByte == kProtocolHeaderFirstByte &&
secondByte == kProtocolHeaderSecondByte) {
// We have a valid header. We might have a valid message!
appearToHaveValidMessage = true;
// Read the second header byte out of the buffer and refresh the buffer count.
Serial.read();
availableBytes = Serial.available();
}
}
}
if (availableBytes >= (kProtocolBodyLength + kProtocolChecksumLength) && appearToHaveValidMessage) {
// Read in the body, calculating the checksum as we go.
byte calculatedChecksum = 0;
for (int i = 0; i < kProtocolBodyLength; i++) {
receivedMessage[i] = Serial.read();
calculatedChecksum ^= receivedMessage[i];
}
byte receivedChecksum = Serial.read();
if (receivedChecksum == calculatedChecksum) {
// Hooray! Push the values to the output pins.
for (int i = 0; i < kProtocolBodyLength; i++) {
analogWrite(i + kChannel1RedPin, receivedMessage[i]);
}
Serial.print("OK");
Serial.write(byte(10));
} else {
Serial.print("FAIL");
Serial.write(byte(10));
}
appearToHaveValidMessage = false;
}
}
Thanks, --Daniel