Search code examples
arduinoeeprom

Pro trinket EEPROM read and write


(I am using Arduino IDE) The Thing I am trying to achieve is it saving the "progress" even when the microcontroller turns off. I have tried for a while tweaking the code but I have yet to get it to actually save/read values from EEPROM properly, so here's the part of my code(Ignore the rest for running the display+i have removed all of the irrelevant code):

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

(I am getting no errors when compiling the code)


Solution

  • Look here at the page for EEPROM.write here. https://www.arduino.cc/en/Reference/EEPROMWrite

    There it says the arguments are defined as:

    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)

    Now look at the number you're trying to write into EEPROM:

    EEPROM.write(5, 10000);
    

    Hopefully now you can see what the problem is. Your second argument is far too large. You should break it into two bytes or use a different method like EEPROM.put (look that up) which doesn't suffer this limitation.