What is offset? When to remove the offset voltage?

I don't know why the offset needs to do in this code? Please explain about that!
Here's the code:

int num;
void setup() {

Serial.begin(9600);
}
void loop() {
num = 0;
Serial.flush();
while (Serial.available() == 0){

}
while (Serial.available() > 0){

num = Serial.read() -'0'; //remove offset in ASCII code corresponds to the value

}
Serial.print("You entered:");
Serial.println(num);
Serial.print(num);
Serial.print(" mutiplied by 2 is: ");
num = num*2;
Serial.println(num);
}

That is converting the ASCII character that was entered to a binary number that can be multiplied by another number. Strange way to comment what is being done.
Paul

1 Like

but do you know what offset is?(not this code)

look at an ASCII table and you'll see that the ASCII value for a '0' is 0x30, '1' is 0x31, '2' is 0x32. so subtracting 0x30 or '0' from an ASCII char in the range '0'-'9' will result in the value of the digit (e.g. '2' is 2)

1 Like