Why the reversed question mark "⸮"?

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
  }
}

Are you going to fix the misuse of data types that I pointed out in your other topic ?

Please explain why you started another topic

is

bool mail

not effective enough?
i used bool mail so that i could save memory storage, provided that the only value needed in mail is either a 1 or a 0

i started a new topic because i didnt really know how to update my last one

You print one more character than available...

Get rid of mail. And do like this..

while (Serial.available()) {

}

thank you this helped alot

consider (easier to read)


int cnt = 0;

void setup ()
{
    Serial.begin (9600);
    Serial.println (" Robobot is ready to chat");
}

void loop ()
{
    if (Serial.available ())  {
        Serial.print ("You:     ");
        while (Serial.available ())  {
            char c = Serial.read ();
            Serial.print (c);
            cnt++;
        }
    }

    if (cnt) {
        Serial.println ();
        Serial.println ("Robobot: Yes");
        cnt = 0;
    }
}

it seems this sketch is better,
also i added a 10ms in the while loop for stability because without the delay my messages get printed out distorted

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

1 Like

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!

1 Like

Same error again. Serial.available() returns a number, not true/false.

Perhaps you meant

bool mail = Serial.available() > 0;
1 Like

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.)

1 Like

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.

1 Like

ill try better next time with my code

i thought 1 is true and 0 is false like the cookbook told me?

That was already explained in this thread. #9, #10 and #12.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.