Serialmonitor input and returning reversed questionmark

hello!

My arduino uno is giving a constant flow of reversed questionmarks (or -1 in DEC) when looking for input. I have given commands trough the serialmonitor and through a HM10 BLE bluetooth device.

In the current state the arduino give a string of -1 until i give an input, and then stops.

I have changed the baudrate multiple times, tried with and with out both the motorshield and BLE.

the code:

#include <Adafruit_MotorShield.h>
#include <stdio.h>
//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(2, 3); //RX,TX

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

//creates two objects to control two terminals on the motor shield 
Adafruit_DCMotor *motor1 = AFMS.getMotor(2); 
Adafruit_DCMotor *motor2 = AFMS.getMotor(1); 

char val; 

void setup() 
{       
  Serial.begin(9600, SERIAL_8N1);  //Set the baud rate to your Bluetooth module.
}


void loop() {
   if(Serial.available() > 0)
      val = Serial.read();
         Serial.print("You typed: " );
          Serial.println(val, DEC);
      
  //if(Serial.read() != -1)
  
{ 
  
}

if(val=='F')  //when the bluetooth module recieves 1 the car moves forward
{
 //Serial.println("2");
 forward();
  
}

if(val=='B')  //when the bluetooth module recieves 2 the car moves backward
{
 backward(); 
}

if(val=='L') //when the bluetooth module recieves 3 the car moves left
{
 left(); 
}

if(val=='R')  //when the bluetooth module recieves 4 the car moves right
{
 right(); 
}

if(val=='S') //when the bluetooth module recieves 5 the car stops
{
 Stop(); 
}

}


void forward()
{

  motor1->setSpeed(100); //Define maximum velocity
  motor1->run(BACKWARD); //rotate the motor clockwise
  motor2->setSpeed(100); //Define maximum velocity
  motor2->run(FORWARD); //rotate the motor clockwise

  delay (500);
 
  motor1->setSpeed(220); //Define maximum velocity
  motor1->run(BACKWARD); //rotate the motor clockwise
  motor2->setSpeed(220); //Define maximum velocity
  motor2->run(FORWARD); //rotate the motor clockwise

  Serial.println("1");
}

void backward()
{
  motor1->setSpeed(220); 
  motor1->run(FORWARD); //rotate the motor counterclockwise
  motor2->setSpeed(220); 
  motor2->run(BACKWARD); //rotate the motor counterclockwise
}

void left()
{
  motor1->setSpeed(220); //Define maximum velocity
  motor1->run(FORWARD); //rotate the motor clockwise
  motor2->setSpeed(100);
  motor2->run(FORWARD); //turn motor2 off
}

void right()
{
  motor1->setSpeed(100);
  motor1->run(FORWARD); //turn motor1 off
  motor2->setSpeed(220); //Define maximum velocity
  motor2->run(FORWARD); //rotate the motor clockwise
}

void Stop()
{
  motor1->setSpeed(0);
  motor2->run(RELEASE); //turn motor1 off
  motor2->setSpeed(0);
  motor2->run(RELEASE); //turn motor2 off
}

this code worked before i tried to give the motor some rev-time, no its restored to the original (That worked)

Any and all help is much appriciated!!

    if(Serial.available() > 0)
        val = Serial.read();
    Serial.print("You typed: " );
    Serial.println(val, DEC);
    //if(Serial.read() != -1)
    {
    }

in the above code, you test if available() and only read() if it is, but then continue to process val regardless. don't know what you intend the pair of braces to do

since the rest of loop() needs a valid value for val, there's nothing for it to do unless there is something available. so if nothing is available, return

   if(Serial.available() ==0)
       return;

    val = Serial.read();

If you read from serial when there is nothing there, -1 is what you get. The code you posted guards against this so I can't see how you're getting -1.

Although I don't have the motor shield, if I run your code I get a stream of zeros, not -1s.

I suspect that the code you posted is a different version from the one you took the screenshot with.

wildbill:
If you read from serial when there is nothing there, -1 is what you get. The code you posted guards against this so I can't see how you're getting -1.

Although I don't have the motor shield, if I run your code I get a stream of zeros, not -1s.

I suspect that the code you posted is a different version from the one you took the screenshot with.

Thanks for your help, Yes the screenshot is with Serial.println(val, DEC); Instead of Serial.println(val); you are correct!

The thing is, the fact that its picking up, and sending the -1 (or reversed questionmark without DEC in the serialprint) makes the code to jump out of the loop. Im confused to say the least. From my Original original code:

#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

//creates two objects to control two terminals on the motor shield 
Adafruit_DCMotor *motor1 = AFMS.getMotor(2); 
Adafruit_DCMotor *motor2 = AFMS.getMotor(1); 

char val; 

void setup() 
{       
  Serial.begin(9600);  //Set the baud rate to your Bluetooth module.
}

void loop() {
 
val = Serial.read();
         Serial.print("You typed: " );
          Serial.println(val);


if(val=='F')  //when the bluetooth module recieves 1 the car moves forward
{
 forward(); 
}

if(val=='B')  //when the bluetooth module recieves 2 the car moves backward
{
 backward(); 
}

if(val=='L') //when the bluetooth module recieves 3 the car moves left
{
 left(); 
}

if(val=='R')  //when the bluetooth module recieves 4 the car moves right
{
 right(); 
}

if(val=='S') //when the bluetooth module recieves 5 the car stops
{
 Stop(); 
}

}


void forward()
{

  motor1->setSpeed(100); //Define maximum velocity
  motor1->run(BACKWARD); //rotate the motor clockwise
  motor2->setSpeed(100); //Define maximum velocity
  motor2->run(FORWARD); //rotate the motor clockwise

  delay (500);
 
  motor1->setSpeed(220); //Define maximum velocity
  motor1->run(BACKWARD); //rotate the motor clockwise
  motor2->setSpeed(220); //Define maximum velocity
  motor2->run(FORWARD); //rotate the motor clockwise
}

void backward()
{
  motor1->setSpeed(220); 
  motor1->run(FORWARD); //rotate the motor counterclockwise
  motor2->setSpeed(220); 
  motor2->run(BACKWARD); //rotate the motor counterclockwise
}

void left()
{
  motor1->setSpeed(220); //Define maximum velocity
  motor1->run(FORWARD); //rotate the motor clockwise
  motor2->setSpeed(100);
  motor2->run(FORWARD); //turn motor2 off
}

void right()
{
  motor1->setSpeed(100);
  motor1->run(FORWARD); //turn motor1 off
  motor2->setSpeed(220); //Define maximum velocity
  motor2->run(FORWARD); //rotate the motor clockwise
}

void Stop()
{
  motor1->setSpeed(0);
  motor2->run(RELEASE); //turn motor1 off
  motor2->setSpeed(0);
  motor2->run(RELEASE); //turn motor2 off
}

everthing worked. Then when i started messing around with the void forward, void backward Etc i started getting the Reversed questionmarks on all code, as soon as i asked for a serial.read.

Thank you for taking your time to help out!

That version will indeed show the funky question mark. I see no reason why it would exit the loop though.

I can't test it due to lack of hardware, if I send F, the earlier version just hangs.

Im confused to say the least.

you only read if available, but print and process regardless

is this falling on deaf ears?

gcjr:
you only read if available, but print and process regardless

is this falling on deaf ears?

No mr gcjr, its not. The code worked moments before, but we took your advice, changed too ignore all the output from the arduino and work around it. It works great again. Thanks for the input!