Data over a serial connection

Hi, I'm trying to set up an led array that lights up depending on the data passed to it over the serial port. I'm trying to pass info in the format (X x Y x I) where x and y are coordinate and i is off(0) or on(1). The readSerialString puts the info in an array, my problem is that i have to pass the data to the board twice before it accepts the new parameters. Any one have an idea why?

/* Processing Button To LED Array

Ricardo de Lemos
rdelemos@ufl.edu
Version: 2.0
*/

#include <binary.h>
#include <Sprite.h>
#include <Matrix.h>

char xy[4]; //COORDINATE ARRAY
int verbose = 0; //DEBUGGING
Matrix myMatrix = Matrix(9, 8, 7); //LED ARRAY MATRIX

void setup() {
Serial.begin(9600); //SERIAL CONNECTION
}

void loop () {
if(Serial.available() > 0){
readSerialString(xy); //READ SERIAL COORDINATES
}
if(verbose == 1) {
Serial.println("");
Serial.print("0: ");
Serial.println(xy[0]);
Serial.print("1: ");
Serial.println(xy[1]);
Serial.print("2: ");
Serial.println(xy[2]);
Serial.print("3: ");
Serial.println(xy[3]);
Serial.print("4: ");
Serial.println(xy[4]);
Serial.println("
");
}
char a = xy[0]; //STORE TO TEMP VARIABLE
char b = xy[2];
char c = xy[4];

int x = cTI(a); //CONVERT FROM CHAR TO INT AND STORE
int y = cTI(b);
int n = cTI(c);

if(n == 0){
myMatrix.write(x, y, LOW); //TURNS OFF SELECTED LED
}
else if (n == 3){
myMatrix.clear(); //CLEARS ARRAY
}
else {
myMatrix.write(x, y, HIGH); //TURNS ON SELECTED LED
}

if(verbose == 1) {
Serial.print("(");
Serial.print(x);
Serial.print(", ");
Serial.print(y);
Serial.print(", ");
Serial.print(n);
Serial.println(")");
Serial.println("~~~~~~~~~~~~~~~");
}
}

int cTI (char i){ //CHANGES FROM CHAR TO INT
if(i == '0'){
return 0;
}
else if(i == '1'){
return 1;
}
else if(i == '2'){
return 2;
}
else if(i == '3'){
return 3;
}
else if(i == '4'){
return 4;
}
else if(i == '5'){
return 5;
}
else if(i == '6'){
return 6;
}
else if(i == '7'){
return 7;
}
else if(i == '8'){
return 8;
}
else if(i == '8'){
return 9;
}
}

void readSerialString (char *strArray) { //READS SERIAL AND SAVES TO ARRAY
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available() > 0) {
strArray = Serial.read();

  • i++;*
  • }*
    _ strArray = 0;_
    }

Ricky, try waiting until you have received all three values before processing the data.

if(Serial.available() >= 3){
readSerialString(xy); //READ SERIAL COORDINATES
}

You may also want to send an extra byte to indicate the start or end of your packets so you can make sure you don't get out of synch and that you don't write past the end of your array.

int cTI (char i){ //CHANGES FROM CHAR TO INT
if(i == '0'){
return 0;
}
else if(i == '1'){
return 1;
}
else if(i == '2'){
return 2;
}
else if(i == '3'){
return 3;
}
else if(i == '4'){
return 4;
}
else if(i == '5'){
return 5;
}
else if(i == '6'){
return 6;
}
else if(i == '7'){
return 7;
}
else if(i == '8'){
return 8;
}
else if(i == '8'){
return 9;
}
}

As an aside, you could replace that function body with something like:

return i - '0';

Oh, and I just noticed you've got this:
if(i == '8'){ twice, so you never return 9--another good reason to use the simplified form. :slight_smile:

My code takes advantage of the fact that single characters are stored as a byte containing the ASCII value of the letter (or something like that :slight_smile: ).

--Phil.