Hi guys,
I’m still expanding my ofxSimpleSerial addon and I’m working on a example where I send multiple variabels in a string to the arduino. I want to control 3 led’s with the 1,2,3 keys on my keyboard. So if I press 1, I want to light up led 1, if I release the led has to go off. To do this for the 3 keys at the same time I need to send messages containing all there statuses at once.
Syntax at the moment:
all on: a?3
all off: xxx
only 1 on: axx
Why a, ? and 3? That’s part of the test, because I want to be able to send almost anything to the Arduino.
This required me to write some simple kind of parser into Arduino. I did and it works, but I don’t like that putting the complete message buffer into a complete message is done trough a for loop.
I tried the following:
message = messageBuffer // this would be perfect
message[] = {}; // doesn't work
message = {}; // doesn't work
message = ""; // doesn't work
So I just override the message with a for loop at the moment:
for(int i = 0;i<numBufferedChars;i++)
{
message[i] = messageBuffer[i]; // copying data from buffer
messageBuffer[i] = 0; // clearing buffer
}
Do you guys have any other Ideas to clear an char array in the Arduino language and how to put the contents of one char array into the other?
And another thing, do I really need to keep a manual count of a char array? Because I wasn’t able to read it out automatically.
My Arduino code:
char message[] = "";
char messageBuffer[] = "";
int numBufferedChars = 0;
int numMessageChars = 0;
int ledPin1 = 10;
int ledPin2 = 11;
int ledPin3 = 12;
void setup()
{
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
Serial.begin(9600);
}
void loop()
{
/*if(Serial.available() > 0)
{
Serial.println(Serial.read(),DEC);
delay(100);
}*/
//Serial.println("Serial.available(): "+Serial.available() );
if(Serial.available() > 0)
{
Serial.println("loop: available bytes");
//int bytesRead = Serial.read();
while(Serial.available() > 0)
{
char readChar = Serial.read();
Serial.print("readChar: ");
Serial.println(readChar);
//int bytesRead = Serial.read();
if(readChar == '|')
{
//message = messageBuffer // this would be perfect
//message[] = {}; // doesn't work
//message = {}; // doesn't work
//message = ""; // doesn't work
/*for(int i = 0;i<numMessageChars;i++)
{
message[i] = 0;
}*/ // somehow also clears my buffer
Serial.println("copy buffer to message");
Serial.print(" messageBuffer: ");
Serial.println(messageBuffer);
for(int i = 0;i<numBufferedChars;i++)
{
Serial.print(" i: ");
Serial.println(i);
Serial.print(" messageBuffer[i]: ");
Serial.println(messageBuffer[i]);
message[i] = messageBuffer[i];
Serial.print(" message[i]: ");
Serial.println(message[i]);
messageBuffer[i] = 0;
}
Serial.print(" message: ");
Serial.println(message);
//messageBuffer = ""; // doesn't work
numMessageChars = numBufferedChars;
numBufferedChars = 0;
useMessage(message);
}
else
{
Serial.println("add to buffer");
messageBuffer[numBufferedChars] = readChar;
Serial.print(" numBufferedChars: ");
Serial.println(numBufferedChars);
Serial.print(" messageBuffer: ");
Serial.println(messageBuffer);
//messageBuffer[numBufferedChars] = bytesRead;
numBufferedChars++;
//Serial.print("messageBuffer: ");
//Serial.println(messageBuffer);
}
delay(100);
}
}
}
void useMessage(char* message)
{
Serial.println("useMessage");
Serial.print("numMessageChars: ");
Serial.println(numMessageChars);
for(int i = 0;i<numMessageChars;i++)
{
int ledPin = i+10;
int value = (message[i] == 'x')? LOW : HIGH;
Serial.print(ledPin);
Serial.print(": ");
Serial.print(value);
Serial.print(", ");
digitalWrite(ledPin,value);
}
Serial.println("");
}
You can also download the complete example (example writing vars simultaneously), it’s inside the ofxSimpleSerial addon:
http://code.google.com/p/ofxsimpleserial/
Thnx for the help