Page 1 of 3

Saving and writing to eeprom

Posted: Fri Feb 09, 2018 10:32 pm
by Duhjoker
hi guys

I'm trying to save a player structure for a game I'm building to eeprom so I can retrieve it later. the esp32/Arduino library is different from the Arduino library so I'm having some problems.

ive got the address part down I think but its giving me a problem with the value which should be the player structure. but when I put player as the value it gives me errors. Can someone please help me?

Code: Select all

void setup() {
  while (!Serial && (millis() < 4000)) ;
  Serial.begin(115200);
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(BLACK);
  //tft.setFrameRate(60);
  tft.persistence = false;
  ////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////
  if (!ss1.begin(0x49)) {
    Serial.println("ERROR!");
    while (1);
  }
  if (!ss2.begin(0x4a)) {
    Serial.println("ERROR!");
    while (1);
  }
  else {
    Serial.println("seesaw started");
    Serial.print("version: ");
    Serial.println(ss1.getVersion(), HEX);
  }
  /////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////
  ss1.pinModeBulk(button_mask, INPUT_PULLUP);
  ss1.setGPIOInterrupts(button_mask, 1);
  pinMode(IRQ_PIN1, INPUT);
  /////////////////////////////////////////////////////////
  ss2.pinModeBulk(button_mask2, INPUT_PULLUP);
  ss2.setGPIOInterrupts(button_mask2, 1);
  pinMode(IRQ_PIN2, INPUT);
  /////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////
  if (!EEPROM.begin(EEPROM_SIZE))
  {
    Serial.println("failed to initialise EEPROM"); delay(1000000);
  }
  Serial.println(" bytes read from Flash . Values are:");
  for (int i = 0; i < EEPROM_SIZE; i++)
  {
    Serial.print(byte(EEPROM.read(i))); Serial.print(" ");
  }
  Serial.println();
  Serial.println("writing random n. in memory");

  tft.useFrameBuffer(use_fb);
}

struct Player
{
  int player_x;
  int player_y;
  int w;
  int h;
  int room;
  int player_direction;
  int player_directionRepeat;
};

Player player = { 160, 170, 16, 16, 3, 2, 0};

byte saveKey = 121;
int val = byte(random(10020));

void save()
{
  EEPROM.write(0, saveKey);
  Serial.print(val); Serial.print(" ");
  EEPROM.write(28, player);
   Serial.print(val); Serial.print(" ");
   Serial.print("saved");
}



Re: Saving and writing to eeprom

Posted: Fri Feb 09, 2018 10:41 pm
by WiFive
EEPROM.write does 1 byte at a time only. Try EEPROM.put. Keep it in the Arduino forum please.

Re: Saving and writing to eeprom

Posted: Fri Feb 09, 2018 11:08 pm
by Duhjoker
Sorry but I asked days ago and no one answered.

So like this.....

Code: Select all

byte saveKey = 121;

void save()
{
  EEPROM.put(0, saveKey);
  EEPROM.put(1, player);
  Serial.print("saved");
}

bool checkLoad()
{
  byte nr;
  EEPROM.get(0, nr);
  Serial.print("checkload");
  return (nr == saveKey);
}

void load()
{
    EEPROM.get(1, player);
    Serial.print("loaded");
}


Re: Saving and writing to eeprom

Posted: Sat Feb 10, 2018 12:10 am
by Duhjoker
ok so I reset my code back to using put get and checkload. Then I told the save and load to print the random number in the serial monitor and when loading. I saved the game then turned off.

when I turn the game back on it puts out a black screen that the player can move around the boarders too. But I have to actually move the player before he shows up. it should also put the player in the x and y save spot but it puts out in the same spot every time.

Code: Select all

void setup() {
  while (!Serial && (millis() < 4000)) ;
  Serial.begin(115200);
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(BLACK);
  //tft.setFrameRate(60);
  tft.persistence = false;
  ////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////
  if (!ss1.begin(0x49)) {
    Serial.println("ERROR!");
    while (1);
  }
  if (!ss2.begin(0x4a)) {
    Serial.println("ERROR!");
    while (1);
  }
  else {
    Serial.println("seesaw started");
    Serial.print("version: ");
    Serial.println(ss1.getVersion(), HEX);
  }
  /////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////
  ss1.pinModeBulk(button_mask, INPUT_PULLUP);
  ss1.setGPIOInterrupts(button_mask, 1);
  pinMode(IRQ_PIN1, INPUT);
  /////////////////////////////////////////////////////////
  ss2.pinModeBulk(button_mask2, INPUT_PULLUP);
  ss2.setGPIOInterrupts(button_mask2, 1);
  pinMode(IRQ_PIN2, INPUT);
  /////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////
  if (!EEPROM.begin(EEPROM_SIZE))
  {
    Serial.println("failed to initialise EEPROM"); delay(1000000);
  }
  Serial.println(" bytes read from Flash . Values are:");
  for (int i = 0; i < EEPROM_SIZE; i++)
  {
    Serial.print(byte(EEPROM.read(i))); Serial.print(" ");
  }
  Serial.println();
  Serial.println("writing random n. in memory");

  tft.useFrameBuffer(use_fb);
}


struct Player
{
  int player_x;
  int player_y;
  int w;
  int h;
  int room;
  int player_direction;
  int player_directionRepeat;
};

byte saveKey = 121;
int val = byte(random(10020));
 
void save()
{
  EEPROM.put(0, saveKey);
  Serial.print(val); Serial.print(" ");
  EEPROM.put(1, player);
  Serial.print(val); Serial.print(" ");
  Serial.print("saved");
}

bool checkLoad()
{
  byte nr;
  EEPROM.get(0, nr);
  Serial.print("checkload");
  return (nr == saveKey);
}

void load()
{
    EEPROM.get(1, player);
    Serial.print(val); Serial.print(" ");
    Serial.print("loaded");
}

void loadgame() {
  /////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////
  palette[0] = 0;            palette[8] = BEIGE;
  palette[1] = BLACK;        palette[9] = GREEN;
  palette[2] = BLUE;         palette[a] = DARKGREY;
  palette[3] = BROWN;        palette[b] = LIGHTGREY;
  palette[4] = DARKGREEN;    palette[c] = YELLOW;
  palette[5] = GREY;         palette[d] = PURPLE;
  palette[6] = PINK;         palette[e] = WHITE;
  palette[7] = RED;          palette[f] = ORANGE;
  //////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////

  Rect rectA {80, 94, 80, 20};
  Rect rectB {80, 128, 80, 20};
  Rect rectC {cursorc.cursorC_x, cursorc.cursorC_y, 32, 32};

  tft.fillScreen(BLACK);
  tft.fillRoundRect(80, 80, 162, 82, 4, WHITE);
  tft.fillRoundRect(85, 84, 153, 74, 4, BLUE);
  tft.setCursor(91, 94);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("New Game");
  tft.setCursor(91, 128);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Continue");
  ////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////
  int y = ss1.analogRead(2);
  int x = ss1.analogRead(3);

  /// if(tft.Bpressed(BTN_UP)){
  if (x > 600 && last_x < 600) {
    tft.writeRectNBPP(cursorc.cursorC_x, cursorc.cursorC_y, 32, 32, 4, cursor3, palette);
    cursorc.cursor_direction = 1;
    cursorc.cursorC_y -= 40;
  }
  if (cursorc.cursorC_y <= 94) {
    cursorc.cursorC_y = 94;
  }

  //////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////Down///////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  /// if(tft.Bpressed(BTN_DOWN)){
  if (x < 400 && last_x > 400) {
    tft.writeRectNBPP(cursorc.cursorC_x, cursorc.cursorC_y, 32, 32, 4, cursor3, palette);
    cursorc.cursor_direction = 1;
    cursorc.cursorC_y += 40;
  }
  if (cursorc.cursorC_y >= 130) {
    cursorc.cursorC_y = 130;
  }

  last_x = x;
  //////////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  if (cursorc.cursor_direction == 1) {
    tft.writeRectNBPP(cursorc.cursorC_x, cursorc.cursorC_y, 32, 32, 4, cursor3, palette);
  }
  //////////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  if (!digitalRead(IRQ_PIN2)) {
    uint32_t buttons = ss2.digitalReadBulk(button_mask2);
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////

    if ((! (buttons & (1 << BUTTON_X)) && tft.collideRectRect( rectA.x, rectA.y, rectA.width, rectA.height, rectC.x, rectC.y, rectC.width, rectC.height)))
    {
      state = STATE_Player;
    }
    else if ((! (buttons & (1 << BUTTON_X)) && tft.collideRectRect( rectB.x, rectB.y, rectB.width, rectB.height, rectC.x, rectC.y, rectC.width, rectC.height)))
    {
       checkLoad(); {
        load();
        state = STATE_Player;
      }
    }
  }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void savegame() {
  /////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////
  palette[0] = 0;            palette[8] = BEIGE;
  palette[1] = BLACK;        palette[9] = GREEN;
  palette[2] = BLUE;         palette[a] = DARKGREY;
  palette[3] = BROWN;        palette[b] = LIGHTGREY;
  palette[4] = DARKGREEN;    palette[c] = YELLOW;
  palette[5] = GREY;         palette[d] = PURPLE;
  palette[6] = PINK;         palette[e] = WHITE;
  palette[7] = RED;          palette[f] = ORANGE;
  //////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////

  Rect rectA {80, 94, 80, 20};
  Rect rectB {80, 128, 80, 20};
  Rect rectC {cursord.cursorD_x, cursord.cursorD_y, 32, 32};

  tft.fillRoundRect(80, 80, 162, 82, 4, WHITE);
  tft.fillRoundRect(85, 84, 153, 74, 4, BLUE);
  tft.setCursor(91, 94);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Save");
  tft.setCursor(91, 128);
  tft.setTextColor(WHITE);
  tft.setTextSize(3);
  tft.println("Load");
  ////////////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////////////
  int y = ss1.analogRead(2);
  int x = ss1.analogRead(3);

  /// if(tft.Bpressed(BTN_UP)){
  if (x > 600 && last_x < 600) {
    tft.writeRectNBPP(cursorc.cursorC_x, cursorc.cursorC_y, 32, 32, 4, cursor3, palette);
    cursord.cursor_direction = 1;
    cursord.cursorD_y -= 40;
  }
  if (cursord.cursorD_y <= 94) {
    cursord.cursorD_y = 94;
  }

  //////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////Down///////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  /// if(tft.Bpressed(BTN_DOWN)){
  if (x < 400 && last_x > 400) {
    tft.writeRectNBPP(cursorc.cursorC_x, cursorc.cursorC_y, 32, 32, 4, cursor3, palette);
    cursord.cursor_direction = 1;
    cursord.cursorD_y += 40;
  }
  if (cursord.cursorD_y >= 130) {
    cursord.cursorD_y = 130;
  }

  last_x = x;
  //////////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  if (cursord.cursor_direction == 1) {
    tft.writeRectNBPP(cursord.cursorD_x, cursord.cursorD_y, 32, 32, 4, cursor3, palette);
  }
  //////////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  //////////////////////////////////////////////////////////////////////////////
  if (!digitalRead(IRQ_PIN2)) {
    uint32_t buttons = ss2.digitalReadBulk(button_mask2);
    if (! (buttons & (1 << BUTTON_A))) {
      state = STATE_Menu;
    }
    //////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////exit menu////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////

    if ((! (buttons & (1 << BUTTON_X)) && tft.collideRectRect( rectA.x, rectA.y, rectA.width, rectA.height, rectC.x, rectC.y, rectC.width, rectC.height)))
    {
      save();
      state = STATE_Player;
    }
    else if ((! (buttons & (1 << BUTTON_X)) && tft.collideRectRect( rectB.x, rectB.y, rectB.width, rectB.height, rectC.x, rectC.y, rectC.width, rectC.height)))
    {
      checkLoad(); {
        load();
        state = STATE_Player;
      }
    }
  }
}



what am I doing wrong?

Re: Saving and writing to eeprom

Posted: Sun Feb 11, 2018 6:02 am
by Duhjoker
Almost there..... I can save and load the game as long as the console is on. If I turn the console off and then repower it and try to load I get black screen.

What could cause the eeprom to lose its data after powering off. Heres my latest code....

Code: Select all

struct Player
{
  int player_x;
  int player_y;
  int w;
  int h;
  int room;
  int player_direction;
  int player_directionRepeat;
};

Player player = { 160, 170, 16, 16, 3, 2, 0};


void save()
{
  EEPROM.put(0, player);
  Serial.print("saved");
}

void load()
{
    EEPROM.get(0, player);
  Serial.println(player.player_x);
  Serial.println(player.player_y);
  Serial.println(player.w);
  Serial.println(player.h);
  Serial.println(player.room);
  Serial.println(player.player_direction);
  Serial.println(player.player_directionRepeat);
}


Re: Saving and writing to eeprom

Posted: Sun Feb 11, 2018 2:56 pm
by martinayotte
Are you calling EEPROM.commit() somewhere ?

Re: Saving and writing to eeprom

Posted: Sun Feb 11, 2018 7:42 pm
by Duhjoker
Uhhh no. Should that be in save or load or both?

EDIT::::

adding commit to the save did the trick! Thank you!!!!

Re: Saving and writing to eeprom

Posted: Mon Feb 12, 2018 1:47 am
by ESP_Sprite
Moved: general discussion -> arduino

Re: Saving and writing to eeprom

Posted: Mon Feb 12, 2018 2:18 am
by Duhjoker
Now I'm having the same problem with slots....

Code: Select all

struct CursorE
{
  int cursorE_x;
  int cursorE_y;
  int cursor_direction;
};

CursorE cursore = { 142, 14, 1};
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
#define ITEM_Spice          0
#define ITEM_Water          1
#define ITEM_Ring           2
#define ITEM_Crysknife      3
#define ITEM_Thumper        4
#define ITEM_Glowglobe      5
#define ITEM_Stilltent      6
#define ITEM_Abba           7
#define ITEM_Stillsuit      8
#define ITEM_Fremkit        9

#define NoItem              255
#define MaxChests           2

#define MAX_ITEM            10
#define SLOT_AVAILABLE      255
#define NO_SLOT_AVAILABLE   254
#define NUMBER_OF_SLOTS     12

struct Slot {
  int slot_x;
  int slot_y;
  int slot_w;
  int slot_h;
  uint8_t itemId;
  uint8_t quantity;
};

Slot slots[NUMBER_OF_SLOTS] = {
  { 180, 16,  81, 16, SLOT_AVAILABLE},
  { 180, 34,  81, 16, SLOT_AVAILABLE},
  { 180, 52,  81, 16, SLOT_AVAILABLE},
  { 180, 70,  81, 16, SLOT_AVAILABLE},
  { 180, 88,  81, 16, SLOT_AVAILABLE},
  { 180, 106, 81, 16, SLOT_AVAILABLE},
  { 180, 124, 81, 16, SLOT_AVAILABLE},
  { 180, 142, 81, 16, SLOT_AVAILABLE},
  { 180, 160, 81, 16, SLOT_AVAILABLE},
  { 180, 178, 81, 16, SLOT_AVAILABLE},
  { 180, 196, 81, 16, SLOT_AVAILABLE},
  { 180, 214, 81, 16, SLOT_AVAILABLE},
};


 
void save()
{
  EEPROM.put(0, player);
  EEPROM.put(1, slots);
  EEPROM.commit();
  Serial.println(player.player_x);
  Serial.println(player.player_y);
  Serial.println(player.w);
  Serial.println(player.h);
  Serial.println(player.cameraX);
  Serial.println(player.cameraY);
  Serial.println(player.player_direction);
  Serial.println(player.player_directionRepeat);
  Serial.print("saved");
}

void load()
{
    EEPROM.get(0, player);
    EEPROM.get(1, slots);
  Serial.println(player.player_x);
  Serial.println(player.player_y);
  Serial.println(player.w);
  Serial.println(player.h);
  Serial.println(player.cameraX);
  Serial.println(player.cameraY);
  Serial.println(player.player_direction);
  Serial.println(player.player_directionRepeat);
}
if I save with two items in slots and save, it disappears as well when the console is powered off and powered back on.

Re: Saving and writing to eeprom

Posted: Mon Feb 12, 2018 5:44 am
by tele_player
No offense intended, but It looks like you have fundamental misunderstanding of how the EEPROM library works. Are you able to understand the code in EEPROM.h and EEPROM.h?

Specifically, I see misuse of the ‘address’ argument to the get() and put() methods. Writing the player structure to address 0, then writing the slots to address 1, will cause the slots to overwrite the player data.