Reversed Question Mark on Serial Monitor

Hello,

When I open the Serial Monitor it is just printing these reversed question marks and I dont know why. Can anybody help me please?

int laser = A3;
int led1 = 4;
int led2 = 1;
int fotocelda1 = A0;
int fotocelda2 = A1;
int fotocelda3 = A2;
int motorRF = 6;
int motorRB = 9;
int motorLF = 5;
int motorLB = 3;

// Declaracion de constantes
int vidas = 3;
int tFoto1, tFoto2, tLaser1, tLaser2;
boolean impacto = true;
boolean disparo = true;

void setup() {

// Estado de los pines
pinMode(laser,OUTPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(fotocelda1, INPUT);

pinMode(motorRF,OUTPUT);
pinMode(motorRB,OUTPUT);
pinMode(motorLF,OUTPUT);
pinMode(motorLB,OUTPUT);

//#include <SoftwareSerial.h>
//#define txPin 4
//#define rxPin 2
//SoftwareSerial BLE(rxPin, txPin);
//BLE.begin(9600);
Serial.begin(9600);

}

void loop()
{

//int valorFotocelda = analogRead(fotocelda1);
int valorFotocelda = 0;

if (vidas == 2) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
}

char instruccion = Serial.read();
char vida = '2';
Serial.println(instruccion);

if (instruccion == '1' && disparo == true)
{
analogWrite(laser, 255);
tLaser1 = millis();
disparo = false;
Serial.write(vida);
}

if (instruccion == 'B' || instruccion == 'D' || instruccion == 'F' || instruccion == 'H') {
digitalWrite(motorRF, 0);
digitalWrite(motorRB, 0);
digitalWrite(motorLF, 0);
digitalWrite(motorLB, 0);
}

if (instruccion == 'A') {
digitalWrite(motorRF, 255);
digitalWrite(motorRB, 0);
digitalWrite(motorLF, 255);
digitalWrite(motorLB, 0);
}

if (instruccion == 'C') {
digitalWrite(motorRF, 0);
digitalWrite(motorRB, 255);
digitalWrite(motorLF, 0);
digitalWrite(motorLB, 255);
}

if (instruccion == 'E') {
digitalWrite(motorRF, 255);
digitalWrite(motorRB, 0);
digitalWrite(motorLF, 0);
digitalWrite(motorLB, 255);
}

if (instruccion == 'G') {
digitalWrite(motorRF, 0);
digitalWrite(motorRB, 255);
digitalWrite(motorLF, 255);
digitalWrite(motorLB, 0);
}

tLaser2 = millis();

if ( tLaser2 - tLaser1 > 3000 )
{
analogWrite(laser, 0);
}

if ( tLaser2 - tLaser1 > 8000 )
{

disparo = true;

}

if ( impacto == true )
{

if ( valorFotocelda > 800 ) {
vidas = vidas - 1;
tFoto1 = millis();
}
}

tFoto2 = millis();

if ( vidas == 1) {
digitalWrite(led1, LOW);
impacto = false;
}

if (tFoto2 - tFoto1 > 3000) {
impacto = true;
}

if ( vidas == 0 ) {
digitalWrite(led2, LOW);
}

}

  Serial.begin(9600);What speed have you set the serial monitor to?

9600

Try Serial.print vs Serial.write:

Serial.write(vida);

 char instruccion = Serial.read();
         char vida = '2';
         Serial.println(instruccion);

If there is nothing to read, Serial.read() returns -1 which is not recognized as a printable character.

Serial.read() is usually placed with a conditional for Serial.available()

if (Serial.available() > 0) {

kinda late but i took the analog read serial from the examples and i plugged in everything correctly , but it still shows the reverse question marks . someone help.

// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.print(sensorValue);
delay(1); // delay in between reads for stability
}

It still sounds like a baud rate problem

For what its worth the program works for me

2 Likes