1

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() {

}

2 Answers2

0

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.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
0
byte b1=1;
char c1 = 49;
char c2 = '2';
Serial.print(b1); 
Serial.print(c1); 
Serial. println(c2); 

produces the output "112", because print behaves different on different data types.

datafiddler
  • 1,755
  • 3
  • 17
  • 30