Why do the serial monitor doesnt receive alphabet but i receive numbers instead

So i am sending text from bluetooth app that i made from MIT App Inventor to my arduino but the serial monitor shows numbers instead of alphabet.Im a new one and i can't really figured it out.


`

char command;
char servopos;


int LMT1  = 5;//IN1
int LMT2  = 6;//IN2
int RMT1  = 7;//IN3
int RMT2  = 8;//IN4

#include <Servo.h> // servo library 
Servo servo1; // servo name

void posservo() {
  int servopos = Serial.read(); // save the received number to servopos
  Serial.println(servopos); // serial print servopos current number received from bluetooth
  servo1.write(servopos); // roate the servo the angle received from the android app
}
void startlauncher() {
  digitalWrite(LMT1, HIGH);
  digitalWrite(LMT2, LOW);

}


void stoplauncher() {
  digitalWrite(LMT1, LOW);
  digitalWrite(LMT2, LOW);

}

void startreloader() {
  digitalWrite(RMT1, HIGH);
  digitalWrite(RMT2, LOW);

}


void stopreloader() {
  digitalWrite(RMT1, LOW);
  digitalWrite(RMT2, LOW);

}

void setup()
{
  Serial.begin(9600);
  servo1.attach(9);
  pinMode(LMT1, OUTPUT);
  pinMode(LMT2, OUTPUT);
  pinMode(RMT1, OUTPUT);
  pinMode(RMT2, OUTPUT);

}

void loop() {
  if (Serial.available() > 0) {

    posservo();
    command = Serial.read();
    Serial.println(command);





    //startlauncher..............
    if (command == 'A' ) {
      startlauncher();

    }
    //stoplauncher..............
    else if (command == 'B' ) {
      stoplauncher();
    }
    //startreloader..............
    else if (command == 'C' ) {
      startreloader();

    }

    //stopreloader........
    else if (command == 'D') {
      stopreloader();
    }




  }
}
`

The code

@yabedibedoo Installation and Troubleshooting is for Problems with the Arduino IDE itself NOT your project. It says so in the description of the section.

Therefore I have moved your post here. Please be more careful where you post in future.

You may want to read this before you proceed:-
how to get the best out of this forum

Your code is printing out numbers because that is what you are asking it to do. The single quote mark like 'A' is taken by the computer to mean a number, and when you print it that is what you get.
However the double quotes mark is taken to mean a string, which is printed as a letter "A".

1 Like

MIT APP INVENTOR

1 Like

command is a char and hence a number. What you see is the ascii code; see e.g. https://www.asciitable.com/.

To see the character, use Serial.write(command);

// echo received command
Serial.write(command);
// add a <CR><LF>
Serial.println();

EVERYTHING in the processor/computer is a (binary) "number". It could represent an actual numerical value, or it could represent an alpha-numeric character, or a processor instruction, etc. The software has to "know" the context.

For example, an upper case "A" is represented by a decimal value of 65 (decimal). (ASCII Table).

Guys, this is incorrect.

char is meat to represent character and the type is honoured by the print class to do the right thing.

you can see this at work If you run this

char command;

void setup() {
  Serial.begin(115200);Serial.println();
  for (command = 'A'; command <= 'Z'; command++) Serial.println(command);
}

void loop() {}

you will really see the alphabet being printed out to the Serial monitor.

what is likely happening is that the MIT app is actually sending in the character as its ASCII number representation, so what we see on the console is a succession of characters properly printed by multiple succession of the loop reading one char and what was sent was an ASCII textual representation "65" may be and not 'A'

probably reading the documentation for this function would clarify what happens when you send an integer

Send1ByteNumber(number)
Decodes the given number String to an integer and writes it as one byte to the output stream.

If the number could not be decoded to an integer, or the integer would not fit in one byte, then the Form’s ErrorOccurred event is triggered and this method returns without writing any bytes to the output stream.

here OP is providing an int and not a String to the function

1 Like

what if just change the code from "A"
to "65"

Thanks for that correction; probably confused with byte.

That's why --

char y = 0x41;  
Serial.print(y); //shows:  A


byte y = 0x41; 
Serial.print(y);  //shows: 65
1 Like

Thanks for the correction.

You haven't provided details of your hardware setup. You have an android device running your app and it is sending data via bluetooth.

But what device is receiving that data? Do you have an HC-05 type bluetooth connected to the Arduino board? Or is your PC receiving the bluetooth data directly without going thru the Arduino board?

HC-05 connected to arduino that control servo motor and l298n motor driver

Thanks.

Your output is repeating "65" and the backward question mark. So what is happening at the Android device? It sends "A" when the START_LAUNCHER button is pressed. Why is the output repeating?

Also when using .write, to simulate .println you can use another .write function to write the charcter '\n' after using write to send the char

because i click it multiple times😂

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