Serial.read() input

Hey guys,

When I use the line “Serial.read()” I would like the variable that I send, that it is used as, for instance, when I send “4”, that the arduino doesn’t use the 52th key, but the number 4.

Could anyone help me out?

this is my code:

/* By inserting 1 variable in the Serial monitor, I should get the table of that number

  • Created by Foitn
  • Written on 28-09-2014
    */

void setup(){
Serial.begin(9600);
}

void loop(){
if(Serial.available() > 0){
char incomingbyte = Serial.read();
for(int i = 0; i <= 20; i++){
Serial.print(incomingbyte);
Serial.print(" * “);
Serial.print(i);
Serial.print(” = ");
char answer = incomingbyte * i;
Serial.println(answer);
}
}
}

I would like the variable that I send, that it is used as, for instance, when I send "4", that the arduino doesn't use the 52th key, but the number 4.

Look at an ASCII table. http://www.asciitable.com/ Perhaps a clue will present itself.

'4' - '0' = ?

Hey foitn I am a beginner and also strugling with the serial communication :) Try change the line "char incomingbyte = Serial.read();" to " char incomingbyte = Serial.read()-'0'; I think it should work

Hey guys,

I’ve changed my code, this is what I’ve done:
/* By inserting 1 variable in the Serial monitor, I should get the table of that number

  • Created by Foitn
  • Written on 28-09-2014
    */

void setup(){
Serial.begin(9600);
}

void loop(){
while(Serial.available() > 0){
char incomingbyte = Serial.read();
for(int i = 0; i <= 20; i++){
Serial.print(incomingbyte);
Serial.print(" * “);
Serial.print(i);
Serial.print(” = ");
int answer = (incomingbyte-48) * i;
Serial.println(answer);
}
}
}

It helped me a lot, thnx!!!

 int answer = (incomingbyte-48) * i;

Two weeks from now, will you remember why you are subtracting 48? Two weeks from now, would you remember why you are subtracting '0'?

They are the same value, but one implies a lot more than the other.

Is there some reason for doing that 20 times? Or would it make more sense to do it once, as you read the character?

What does your code do if the user enters 'D'?

Welcome to the Forum! Several things: First, please make sure you read the first two posts on this Forum by Nick Gammon. It tells you the proper way to post your code and following those instructions will generate more help for you. Second, PaulS raises a good question about the use of a for loop. If the Serial.read() method only returns a single character, it doesn't help to print it out 20 times. Third, he also points out that "magic numbers" in code (e.g., your 48) tend to be forgotten over time and makes your code harder to read at some later date. There are a number of ways to avoid them, such as:

#define ASCIIZERO  48      // Place this line before setup()
// ... more of your code...

 int answer = (incomingbyte - ASCIIZERO) * i;

Another way is to use a constant:

const int ASCIIZERO = 48;      // Place this line before setup()
// ... more of your code...

 int answer = (incomingbyte - ASCIIZERO) * i;

Finally, before you post your code, click in the source code window of the IDE, press Ctrl-T, and the IDE will format your code into a common C style. (If your code can't be compiled, Ctrl-T won't work.)