Hello,
the code at the bottom of this post is just a part of another program i'm writing. I have built this mini version for testing and understanding the problems i'm having with it. (I'm pretty much a newbie)
So, the idea is to be able to write position data x/y (max 3 chars at once) into the serial monitor as well as single letters for further control over the program. I tried parsint before, it worked just fine. But because it's blocking the Serial.read as long as it hasn't recieved a valid int, it didn't make sense to continue with it. So I came up with the idea below...
The problem I'm having is, that it doesn't seem to be able to inject the arrays content into the cti int. It just returns 0 and it seems like my knowledge is not enough to solve that mystery.
I hope my comments are clear and that you have a solution for this. Thank you very much for your time!
Greetings, Jaddow
const byte neuer_Char = 32;
char cInputArray[neuer_Char]; // an array to store the received data
static byte cntCha = 0;
char EndMark_1 = '\n'; //Endmarker for ending the Input
char cInput; //Char input
int cti; //char to int
bool no_input = false;
int z_3; //counter
float itf; //Int to float
void setup() {
Serial.begin(9600);
Serial.println("<ready>");
}
void loop() {
if(z_3 < 1){ //Counter for executing a buffer clearing once
Serial.read();
z_3++;
}
while(no_input == false) {
cInput = Serial.read();
if (cInput != EndMark_1) { //As long as the chars aren't the endmarker, add them to the array
cInputArray[cntCha] = cInput; //Serial.read into the array
cntCha++; //count up
if (cntCha >= neuer_Char) {
cntCha = neuer_Char - 1;
}
}
else{
z_3 = 0;
char buffer[4]; //create a buffer
buffer[0] = cInputArray[0]; //b[0] is the first char in the array
buffer[1] = cInputArray[1]; //...
buffer[2] = cInputArray[2];
buffer[3] = '\0'; //max size 4
cti = atoi(buffer); //buffer chars conversion to a single int
Serial.println(cti);
itf = (float)cti; //conv. to float for further calculations
cInputArray[cntCha] = '\0'; //terminate the string
cntCha = 0; //char counter is back to 0
no_input = true; //ends and blocks the while-loop
}
}
no_input = false; //reopen the loop - only for testing
}