I have a very simple program that appears to be able to do magic!
The full program is as follows;
I want to send commands to the audrino to control a stepper motor, essential the command structure will be three character followed by a maximum of 5 numbers, all terminated with a semicolon.
The program below works except for the fact that when I print the command array magically I get the full command CCCNNNNN even though its only a 3 character array with terminator!!!
Try it for yourself, all explanations are welcomed?
char cbuffer[9] = {’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ ‘};
char command[3] = {’ ‘,’ ‘,’ ‘};
char cnumber[6] = {’ ‘,’ ‘,’ ‘,’ ‘,’ ‘,’ '};
void setup()
{
Serial.begin(9600);
}
void loop()
{
int i = 0;
while ( Serial.available( ) > 0 )
{
byte serialByte = Serial.read();
//Serial.println(serialByte);
if(i <=8)
{
cbuffer = char(serialByte);
-
}*
-
i++;*
-
}*
-
delay(500);*
-
//Copy the first three characters into the command array*
-
for(int j = 0; j <3; j++) command[j] = cbuffer[j];*
-
for(int j = 3; j <9; j++)*
-
{*
-
if(cbuffer[j] != ‘;’)*
-
{*
-
cnumber[(j-3)] = cbuffer[j];*
-
}*
-
else*
-
{*
-
break;*
-
}*
-
}*
-
Serial.println(command);*
-
Serial.println(cnumber);*
-
// Clear the cbuffer after reading*
-
for(int j = 0; j <=8; j++) cbuffer[j] =’ ';*
}