(我使用的是Arduino IDE) 我试图实现的是即使微控制器关闭也能保存“进度”。我已经尝试了一段时间调整代码,但我还没有让它实际保存/读取EEPROM中的值,所以这是我的代码的一部分(忽略其余的运行显示+我删除了所有不相关的代码):
#include <EEPROM.h>
int hunger = 10000;
int happiness = 10000;
int y = 0;
int z = 0;
int sensorPin = A0;
int sensorPin2 = A1;
void setup() {
Serial.begin(9600);
EEPROM.write(5, 10000);
EEPROM.write(5, 10000);
hunger = EEPROM.read(8)- '0';
happiness = EEPROM.read(5)- '0';
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
display.clearDisplay();
display.display();
Serial.begin(9600);
pinMode(sensorPin,INPUT);
}
void loop() {
int value = happiness;
int value2 = hunger;
EEPROM.write(5, happiness);
EEPROM.write(8, hunger);
hunger -= 1;
happiness -= 1;
if(analogRead(sensorPin)>1020){
testhappy();
happiness+=2500;
}
else{
testsad();
}
while(analogRead(sensorPin2)>1020){
testeat();
hunger+=5000;
}
}
void testhappy(void){
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(30, 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
delay(1);
display.display();
delay(1);
for(y=0;y<2;y++){
for(z=0;z<5;z++){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
for(z=5;z!=0;z-=1){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
for(z=0;z> -5;z-=1){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
for(z=0-5;z!=0;z+=1){
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap((30+z), 32, Happy32_glcd_bmp, 32, 32, 1);
display.display();
}
}
display.stopscroll();
}
void testsad(void){
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(30, 32, Mild32_glcd_bmp, 32, 32, 1);
display.display();
delay(1);
}
void testeat(void){
delay(200);
for(y=0;y<6;y++){
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.clearDisplay();
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(30, 32, Eat32_glcd_bmp, 32, 32, 1);
display.drawBitmap(60, 38, Cookie18_glcd_bmp, 8, 8, 1);
display.display();
delay(200);
display.clearDisplay();
display.setCursor(0,0);
display.print(hunger/100);display.print("/");display.print(happiness/100);
display.drawBitmap(60, 38, Cookie28_glcd_bmp, 8, 8, 1);
display.drawBitmap(30, 32, Mild32_glcd_bmp, 32, 32, 1);
display.display();
delay(200);
}
}
void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h) {
uint8_t icons[NUMFLAKES][3];
// initialize
for (uint8_t f=0; f< NUMFLAKES; f++) {
icons[f][XPOS] = random(display.width());
icons[f][YPOS] = 0;
icons[f][DELTAY] = random(5) + 1;
Serial.print("x: ");
Serial.print(icons[f][XPOS], DEC);
Serial.print(" y: ");
Serial.print(icons[f][YPOS], DEC);
Serial.print(" dy: ");
Serial.print(icons[f][DELTAY], DEC);
}
while (1) {
// draw each icon
for (uint8_t f=0; f< NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, WHITE);
}
display.display();
delay(200);
// then erase it + move it
for (uint8_t f=0; f< NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, BLACK);
// move it
icons[f][YPOS] += icons[f][DELTAY];
// if its gone, reinit
if (icons[f][YPOS] > display.height()) {
icons[f][XPOS] = random(display.width());
icons[f][YPOS] = 0;
icons[f][DELTAY] = random(5) + 1;
}
}
}
}
(编译代码时没有错误)
答案 0 :(得分:2)
在这里查看EEPROM.write的页面。 https://www.arduino.cc/en/Reference/EEPROMWrite
在那里它说参数被定义为:
EEPROM.write(地址,值)
参数address:要写入的位置,从0(int)
开始value:要写入的值,从0到255(字节)
现在看看你尝试写入EEPROM的号码:
EEPROM.write(5, 10000);
希望你现在可以看到问题所在。你的第二个论点太大了。你应该把它分成两个字节或者使用不同的方法,比如EEPROM.put(看起来那样),它没有受到这种限制。