i have this code that makes ai respond "yes" to every message you would type into the board. the program initially tells you that the bot is ready to chat and after that you would type whatever you want and the bot would response with a yes.
my problem with this program is that every time i would type something into the board the program prints the message back to the monitor (this is what it is supposed to do) but at the end of every message there is a reversed question mark "⸮" (this is not what it is supposed to do).
what can i do to fix this problem, is it the terminating null character thats creating this problem or what?
int zero = 0;
bool set = 1; //initially set "set" to 1 so that the program doesnt immediately print "Robobot: Yes
void setup() {
Serial.begin(9600);
Serial.println(" Robobot is ready to chat"); // make sure that the program says this once
}
void loop() {
bool mail = Serial.available(); //check if theres an available message
if (mail == LOW){ //if there is no message available then do nothing
}else {
Serial.print("You: "); //otherwise if there is a message present in the input then print this
while (mail == HIGH){ //continue reading the input until theres nothing sent
mail = Serial.available(); //write true to mail if theres still something in the mail and write LOW if there is nothing left
delay(10); //put a delay for stability
char message = Serial.read(); // keep wiriting the input characters into the variable "message"
Serial.print(message); // now print all those characters back to the serial moniter
}
set = 0; // after the while loop set "set" to 0 so that the robot can finally say "Yes"
} //exit the else clause
if (set); //if set is still 1 then do nothing
else { //otherwise let the program print "Robobot: Yes"
delay(1500); //put a delay becausr in reality you cant immediately answer a message
Serial.println(" "); //this is to create a new line for the robobot response
Serial.println("Robobot: Yes"); //now the bot can finally respond
set = 1; //set "set" back to 1 so that this wont be executed again until mail goes high
}
}
You have declared mail as a boolean. It can only have one of two possible values, either true or false. As it happens true is 1 and false is zero, each take 1 byte
Later in the code you use mail to hold a byte read by Serial.read(). The byte could have any value from 0 to 255. As it happens, because mail is a boolean then any value other than 0 will be regarded as true
Later still you test whether mail equal LOW, ie whether it is 0 or not
As it happens, as long as you understand what you are doing the code may work as you want, but you are depending on the values defined for false and LOW both being 0. Admittedly it is unlikely that the definitions will change but why not make the code more logical and easier to maintain and debug and use consistent values and a better variable name ?
That's strange because you have previously updated it by posting to it
That's an error. bool variables should only be assigned true or false. You should not assign a number. C/C++ will not give an error message for this, but the approach C/C++ compilers take to this kind of thing is "You want to hang yourself? Fine, here's enough rope to do it". Just because the compiler does not give an error does not mean it is not an error!
HIGH and LOW are values returned by digitalRead(). They are not the same as true and false. (Under the "hood" they might have the same values, but only a foolish programmer makes those kinds of assumptions when they don't have to.)
Enable and read compiler warnings. Those are not errors, they are inconsistencies or possible problems. I'm not sure if it would catch all the semantic errors mentioned above, but it might catch some.