Search code examples
c++arduinocharbyteeeprom

Writing a code from EEPROM to an array of char


On the EEPROM the pincode 1234 is written as bytes. Now I want to read out the pincode and write it to an array of type char and print it on the serial monitor, but I only get this rectangles like in the picture. But if I print it directly to the serial monitor with "Serial.print(EEPROM.read(i));" I get "1234". serial monitor

const byte PINLENGTH = 4;
char pinCode[PINLENGTH+1];

void setup() {
Serial.begin(9600);
Serial.print(pinCode[0]);

for ( int i = 0; i < PINLENGTH; ++i ){
  pinCode[i] = (char) EEPROM.read(i);
  Serial.print(pinCode[i]);
}}


void loop() {

}

Solution

  • Try this:

    const byte PINLENGTH = 4;
    char pinCode[PINLENGTH+1];
    
    void setup() {
    Serial.begin(9600);
    
    for ( int i = 0; i < PINLENGTH; ++i ){
      pinCode[i] = (char) EEPROM.read(i) + '0';    // <- Note +'0'
      Serial.print(pinCode[i]);
    }}
    

    The point here is that the values read from the EEPROM are probably binary, and adding '0' converts them to ASCII.