Hello,
So while working on an array that can store multiple numbers, I came across a strange error I have not seen before.
In the image attached you will see the problem.
I would like to know how the serial read changes my 1,100 into a B, since I cannot find anything relate able on the interwebs that I can use as reference.
Can someone please give me a few pointers or explain to me where I screwed up with the code?
Thank you in advance!
char coupeNumberChar[4];
char numberOfPeopleChar[4];
int ByteReceived;
bool checkingCoupeNumber = true;
int counter = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0)
{
checkingCoupeNumber = false;
while (Serial.available() > 0) {
Serial.println("reading");
ByteReceived = Serial.read();
delay(50);
if (ByteReceived == '#') {
checkingCoupeNumber = true;
}
else if (ByteReceived == '%') {
return;
}
else if (checkingCoupeNumber && ByteReceived != ',') {
coupeNumberChar[counter] += ByteReceived;
Serial.print("recieved char: ");
Serial.println(coupeNumberChar[counter]);
counter++;
}
else if (ByteReceived == ',') {
checkingCoupeNumber = false;
coupeNumberChar[counter] = '\0';
counter = 0;
}
else if (!checkingCoupeNumber) {
numberOfPeopleChar[counter] += ByteReceived;
Serial.print("recieved char: ");
Serial.println(numberOfPeopleChar[counter]);
counter++;
}
}
numberOfPeopleChar[counter] = '\0';
counter = 0;
for (int i = 0; i < 3; i++) {
Serial.print(coupeNumberChar[i]);
Serial.print(", ");
}
Serial.println("");
for (int i = 0; i < 3; i++) {
Serial.print(numberOfPeopleChar[i]);
Serial.print(", ");
}
Serial.println("");
int coupeNumber = atoi(coupeNumberChar);
Serial.println(coupeNumber);
int numberOfPeople = atoi(numberOfPeopleChar);
Serial.println(numberOfPeople);
memset(coupeNumberChar, 0, sizeof(coupeNumberChar));
memset(numberOfPeopleChar, 0, sizeof(numberOfPeopleChar));
}
}
Edit: I think personally that the error happens with the memset(); function, so I'll be checking out if changing that will fix the problem (or at least clarify the number to letter bug)
Edit 2: After making some changes in the code I discovered that the memset (the function that sets the values to 0) is no the culprit, but rather the atoi();