greetings,
I want to make a program that convert the numbers I sent via serial monitor into integers, this is my code :
void setup()
{
Serial.begin(9600);
}
void serReadInt()
{
int i, r, x, y, z, serAva;
char inputBytes [5];
char * inputBytesPtr = &inputBytes[0];
char inputBytes2 [5];
char * inputBytesPtr2 = &inputBytes2[0];
char intInit;
char intData;
char intData2;
if (Serial.available()>0)
{
delay(5);
intInit = Serial.read();
if (intInit == '<')
{
serAva = Serial.available();
for (i=0; i<serAva; i++)
{
intData = Serial.read();
if (intData == '>')
{
inputBytes[i] = '\0';
inputBytes2[r] = '\0';
y = atoi(inputBytesPtr);
z = atoi(inputBytesPtr2);
Serial.print("y = ");
Serial.print(y);
Serial.print("\t z = ");
Serial.println(z);
}
else if (intData == '|')
{
x = serAva-i;
for (r=0; r<x; r++)
{
intData2 = Serial.read();
if (intData2 == '>')
{
inputBytes[i] = '\0';
inputBytes2[r] = '\0';
y = atoi(inputBytesPtr);
z = atoi(inputBytesPtr2);
Serial.print("y = ");
Serial.print(y);
Serial.print("\t z = ");
Serial.println(z);
}
else
{
inputBytes2[r] = intData2;
}
}
}
else
{
inputBytes[i] = intData;
}
}
}
}
else
Serial.println("ZZZZZ");
}
void loop()
{
while(Serial.available()>0)
{
serReadInt();
}
}
I want to make it able to extract two integers in one input, for example, if I input <123|456>, then it means y = 123, and z = 456
the problem is, it only works if the total byte I send is less then 4, it can process <1>, <12>, <123> just right, but it won't process <1234>
I thought the problem lies in the serial.available, because after several trial and code modification, I conclude that the number of available bytes in the buffer was never above 4 bytes.....
can anyone help me ???
thanks,
Heggi