我想跟随Arduino函数从EEPROM地址读取并返回一个可以在串行监视器上打印的字符串。
char EEPROM_read(int addr, int length_str) {
char letter[length_str];
for (int i=0; i<length_str; i++) {
letter[i] = EEPROM.read(addr);
addr ++;
}
//Serial.println(letter); This works inside the function, but I need it to work outside of the function
return letter;
}
serial_number = EEPROM_read(0,11);
Serial.println(serial_number); //This does not work
String SN(serial_number);
Serial.println(SN); //This does not work
答案 0 :(得分:1)
如果要在RAM中返回文本,则需要在RAM中留出空间。在Arduino上(以您的情况为例),动态内存分配是次优的主意,因此最好事先提供该空间:
Array
(
[0] => SAPPLE IS VERY BEAUTIFUL.TX.. .
[1] => WBOOK IS SUCCESSFUL.YJ.. .
[2] => EDO YOU HAVE A PEN? /.EQ.. .
)
在此示例中,变量 char* EEPROM_read(int addrE, char* buf, int length_str)
{
for (int i=0; i<length_str; i++) {
buf[i] = EEPROM.read(addrE++);
if (buf[i] == 0) break;
}
return buf;
}
void setup() {
Serial.begin(9600);
char text[20];
Serial.println(
EEPROM_read(0, text, sizeof(text))
);
}
仅在text
内部可用,您自己应该能够在示例范围之外使用它。