Hi there!
To briefly explain what I want to do, I want Matlab to be able to send an array which contains a letter and 2 coordinates (Example: 'B1020' for 'B', '10', '20'). That's it, I know it is documented a lot on the Internet, but I couldn't find any solution for this. I also read Robin2 kind of tutorial on how Arduino is reading multiple characters and things like that.
So I will post my code and explain after what is going wrong/right. I need to precise that I'm using an Arduino Uno,
Code :
int a=0;
char t[3];
void setup()
{
Serial.begin(9600);
}
void loop()
{
serialRoutine();
}
void serialRoutine()
{
if(Serial.available()>0)
{
for (int i=0; i<3; i++)
{
t[i] = Serial.read();
}
if (t[0] =='T')
{
for( int i=0; i<3; i++)
{
Serial.println(t[i]);
}
}
else if (t[0] =='R')
{
Serial.println("NO");
}
else if (t[0]== 'M')
{
a++;
Serial.println(a);
}
}
}
So here I'm just sending 3 letters for example. Let's say 'TOP'. I'm using the monitor since it's easier to visualize.
What I expect the code to do is :
-> Read 'TOP'
-> Put each letter in each cell of t - T in t[0], O in t[1],...
-> If t[0] equal 'T', then let me view the content of the other cells
But, I'm nowhere near this result. What I have is :
As you can see, if I'm writing 'TOP', the first T is well saved into the first cell, but O and P kind of disappear. And if I'm sending 'OTP', instead of T being stored in the 2nd cell, it is stored in the first one.
So there's basically something I don't understand going on.
Any insights on this?
Best regards!
EDIT: Ok I found the solution, as the serial communication is a bit slow, I needed to put a delay() in the for loop that is storing the data. So now my first problem is fixed. My second is how can I break the string to go from B1034 to t[0] = 'B', t[1] = '10', t[2] = '34' ?