Pro trinket EEPROM读写

Pro trinket EEPROM read and write

(我正在使用 Arduino IDE) 我想要实现的目标是即使在微控制器关闭时也能保存 "progress"。我已经尝试了一段时间调整代码,但我还没有从 EEPROM 中正确地得到它实际的 save/read 值,所以这是我的代码的一部分(忽略 运行 显示的其余部分+我有删除了所有不相关的代码):

#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;
      }
    }
   }
}

(我在编译代码时没有遇到任何错误)

在此处查看 EEPROM.write 的页面。 https://www.arduino.cc/en/Reference/EEPROMWrite

那里说参数定义为:

EEPROM.write(address, value)

Parameters address: the location to write to, starting from 0 (int)

value: the value to write, from 0 to 255 (byte)

现在查看您要写入 EEPROM 的数字:

EEPROM.write(5, 10000);

希望您现在可以看出问题所在。你的第二个论点太大了。您应该将它分成两个字节或使用不受此限制的不同方法,例如 EEPROM.put(查找)。