Hola, esta es mi primera publicación y espero hacerlo correctamente. Les comento, he estado haciendo un proyecto donde controlo una matriz de LED's de 7x6 sin driver ni integrado enrte medio, directamente desde Arduino. El control es simple, mi problema es con la comunicación por el puerto serie y la lectura del mismo. Lo que pasa es que despues de enviar varias veces el texto que hará prender los LED's, empieza a recibir cualquier cosa o se tilda y no avanza.
Nose si estaré haciendo algo mal o el arduino no se bancara tanto texto, les dejo mi código para quien quiera tirarme una idea o alguna posible solución, desde ya gracias.
Si necesitan que explique un poco mas como funciona el sktech no tengo ningun problema en hacerlo.
/*********** CONFIGURACION ************/
#define COLSNUM 6
#define ROWSNUM 7
#define SPEED 500
#define MAXSTEPS 200
int RowsPins[ROWSNUM] = { 48, 34, 36, 38, 40, 42, 44 };
int ColsPins[COLSNUM] = { 32, 30, 28, 26, 24, 22 };
/*********** PROGRAMA ************/
int MatrixLEDs[MAXSTEPS];
int TextShow = 0;
int TextSpeed = SPEED;
long TextSize = 0;
long LetterCount = 0;
unsigned long HoldTime = 0;
String SerialBuffer = "COD:NULL";
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
for(int i=0; i<COLSNUM; i++)
{
pinMode(ColsPins[i], OUTPUT);
digitalWrite(ColsPins[i], LOW);
}
for(int i=0; i<ROWSNUM; i++)
{
pinMode(RowsPins[i], OUTPUT);
digitalWrite(RowsPins[i], HIGH);
}
for(int i=0; i<MAXSTEPS; i++)
MatrixLEDs[i] = 0;
}
void loop()
{
digitalWrite(13, LOW);
if( ReadSerialPort(&SerialBuffer) )
{
digitalWrite(13, HIGH);
if( SerialBuffer == " COD:NULL " )
TextShow = false;
else if( SerialBuffer.substring(0, 5) == " VEL:" )
{
String Aux = SerialBuffer.substring(5, SerialBuffer.length());
TextSpeed = Aux.toInt();
}
else
{
LetterCount = 0;
TextSize = 0;
TextShow = true;
HoldTime = millis();
ParseText(SerialBuffer);
}
}
if( TextShow == true)
{
if( LetterCount < TextSize )
{
for( int i=0; i<COLSNUM; i++ )
ShowLEDs(i, MatrixLEDs[i+LetterCount]);
if( millis()-HoldTime > TextSpeed )
{
LetterCount += COLSNUM;
HoldTime = millis();
}
}
else
LetterCount = 0;
}
}
int ReadSerialPort(String *Buffer)
{
int DataRecibe = 0;
char c = ' ';
Buffer->replace(*Buffer, String(""));
Buffer->concat(' ');
if( Serial.available() > 0 )
{
while( DataRecibe == 0 )
{
if( Serial.available() > 0 )
{
c = Serial.read();
if(c == ';')
DataRecibe = 1;
else
Buffer->concat(c);
}
}
}
Buffer->concat(' ');
Serial.flush();
return DataRecibe;
}
void ParseText(String text)
{
String StringAux;
int Count = 0;
TextSize = GetLength(text);
for(int i=0; i<text.length(); i++)
{
if( text.charAt(i) == '{' )
{
for(int j=i; j<text.length(); j++ )
{
if( text.charAt(j) == '}')
{
StringAux = text.substring(i+1, j+1);
char TextAux[StringAux.length()];
StringAux.toCharArray(TextAux, StringAux.length());
char *TextTok = strtok(TextAux, ",}");
while(TextTok != NULL)
{
MatrixLEDs[Count] = atoi(TextTok);
TextTok = strtok(NULL, ",}");
Count++;
}
break;
}
}
}
}
}
long GetLength(String text)
{
long Count = 0;
for(long i=0; i<SerialBuffer.length(); i++)
{
if( SerialBuffer.charAt(i) == '{' )
Count += 6;
}
return Count;
}
void ShowLEDs(int ColNum, int ColBin)
{
digitalWrite(ColsPins[ColNum], HIGH);
for(int i=0; i<ROWSNUM-1; i++)
{
if( ColBin%2 == 1 )
{
digitalWrite(RowsPins[i], LOW);
delayMicroseconds(300);
digitalWrite(RowsPins[i], HIGH);
}
ColBin = ColBin/2;
if( ColBin == 1 )
{
digitalWrite(RowsPins[i+1], LOW);
delayMicroseconds(300);
digitalWrite(RowsPins[i+1], HIGH);
break;
}
else if( ColBin == 0)
break;
}
digitalWrite(ColsPins[ColNum], LOW);
}