2

This is weirding me out. So I wrote to the EEPROM and I would like to read from the address and format it in Base 10, err... Decimal. The normal number thingy that's not ASCII. But it keeps returning ASCII values when I write:

Serial.println(EEPROM.read(address), DEC).

Wait for the weird part, when I format in other bases like, say BIN, or OCT, it outputs in the correct base. Please help. Here's the full snippet of the tiny program.

#include <EEPROM.h>
int addr = 0;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() > 0){
    int rc = Serial.read();
    EEPROM.write(addr, rc);
    Serial.println(EEPROM.read(addr), DEC);
  }

}
6
  • 2
    you write into the EEPROM the received character, not a number. you print a number. – Juraj May 22 at 15:49
  • 1
    I do not understand what you are expecting your program to do. Computers (usually) communicate using ASCII. You read 1 value from another computer. That's likely ASCII. You store this into the EEPROM. Then you print that from the EEPROM back to another computer. And you see what you initially typed in. Sounds normal. Wait, was your assignment to read ASCII, convert that into a number to store into the EEPROM then read that number out and print it? If so, you are missing steps. Sounds like you need to go back and 1st understand what the problem is trying to teach you. – st2000 May 22 at 16:28
  • The problem is that I write an integer into the EEPROM but it prints out ASCII even with the "DEC" argument. However, when I switch the "DEC" to BIN or OCT, it prints out Binary and Octal values respectively. But when I use "DEC" it only gives me the ASCII rather than the exact DEC value of the integer that I put in. Am I missing something? – Joshua Olalekan May 22 at 16:56
  • What do you mean 'the integer I put in'? How are you putting in an integer over serial, which works on ASCII characters? – Majenko May 22 at 17:29
  • rewrite your code to print integers from 0 to 20 ... what do you see? – jsotola May 22 at 17:46
1

I finally figured it out. Apparently, when I call Serial.read() and assign it to "int rc", It sort of converts the actual ASCII values into integer. So reading the number 3 from the Serial monitor and assigning it to rc stores 51 in rc. So the trick I found to that was to use

int rc = Serial.read()-'0'

That way, I am storing 51-48 which is, 3 (Since 48 is the ASCII value for 0).

Thanks.

2
  • 1
    Serial.read() returns a character, which is always represented as ASCII code number. the return type of read is int to be able to return -1 if nothing is read. – Juraj May 24 at 5:22
  • @Joshua: this answer makes no sense; read and try to understand the comments. – PimV May 26 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.