Issue with EEPROM as memory for a MIDI drum controller

Hi! I hace this weird issue I can´t get a solution: I´m building a 16 channel MIDI drum controller with an Arduino Mega, and I´m using the EEPROM memory to save a preset. The info to be saved are only ints so every saved data uses 2 bytes.

I noticed that when saving to the EEPROM the sistem put random numbers instead the ones I wanted, so I made a small program to check the memory, here is the test program:


// Pablo Martín
// pmartinchile@gmail.com 
// www.pmartinchile.com / www.pmartinlab.com
//Librerías:
#include <SPI.h>
#include <Wire.h>
#include <EEPROM.h>

//------------------------------------------------------------------
//II.- Setup -------------------------------------------------------
//------------------------------------------------------------------

void setup(){
  Serial.begin(115200);
//Matriz de Partes: 
//Mapeo: MIN, MAX, NOTA General MIDI Drum (GM):
int notahh = 42;
int notabd = 36;
int notasn1 = 38;
int notasn2 = 40;
int notaT1LowFloorTom = 41;
int notaT1HighFloorTom = 43;
int notaLowTom = 45;
int notaLowMidTom = 47;
int notaHighMidTom = 48;
int notaHighTom = 50;
int notaChina = 52;
int notaSplash = 55;
int notaRideCym = 51;
int notaRideBell = 53;
int notaCrashCym = 49;
int notaCrashBell = 57;
//Mapeo: MIN, MAX, NOTA General MIDI Drum (GM):
int PARTES[48] = {70, 1020 , notahh, 70, 1020, notabd, 70, 1020 , notasn1, 70, 1020 , notasn2,200, 1020 , notaT1LowFloorTom,200, 1020 , notaT1HighFloorTom,200, 1020 , notaLowTom,200, 1020 , notaLowMidTom,200, 1020 , notaHighMidTom,200, 1020 , notaHighTom,100, 1020 , notaChina,100, 1020 , notaSplash,100, 1020 , notaRideCym,100, 1020 , notaRideBell,100, 1020 , notaCrashCym, 200, 1020 , notaCrashBell};
Serial.println("Writing to EEPROM: ");
for(int i =0; i<=47; i++){
  EEPROM.put(2*i, PARTES[i]);
  }
    Serial.println("Read int from EEPROM: ");
    int aux;
for(int i =0; i<=47; i++){
  EEPROM.get(2*i, aux);delay(200);
  Serial.println(aux);    //This may print 'ovf, nan' if the data inside the EEPROM is not a valid float.
}
}//setup

//------------------------------------------------------------------
//III.- Loop -------------------------------------------------------
//------------------------------------------------------------------
void loop(){
}//void loop

It worked fine so I put the same one on the main code, but it didnt work. Adding delays worked for reading from the EEPROM but not for saving. Im working a 115200 baud, the libraries I´m using are:

//Librerías:

#include <MIDI.h> 
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <Wire.h>
#include <EEPROM.h>

What do you think that could be bugging the code?

Thanks!

Please post the full code that produces the problem.

PS
Thanks for using code tags in your first post.

If you put all of those values into a struct and save the struct with EEPROM.put you will not have to worry about supplying EEPROM addresses and endianess when you read them back.

EEPROM put code:

#include <EEPROM.h>

struct MyObject
{
   int notahh;
   int notabd;
   int notasn1;
   int notasn2;
   int notaT1LowFloorTom;
   int notaT1HighFloorTom;
   int notaLowTom;
   int notaLowMidTom;
   int notaHighMidTom;
   int notaHighTom;
};

MyObject myObject = {42, 36, 38, 40, 41, 43, 45, 47, 48, 50};
int eepromAddress = 0;

void setup()
{
   Serial.begin(115200);

   EEPROM.put(eepromAddress, myObject);

   Serial.println("Written struct data type!");
}

void loop()
{
   /* Empty loop */
}

EEPROM get code:

#include <EEPROM.h>

struct MyObject
{
   int notahh;
   int notabd;
   int notasn1;
   int notasn2;
   int notaT1LowFloorTom;
   int notaT1HighFloorTom;
   int notaLowTom;
   int notaLowMidTom;
   int notaHighMidTom;
   int notaHighTom;
};

MyObject myObject;
int eepromAddress = 0;

void setup()
{
   Serial.begin(115200);
   Serial.println("Reading EEPROM");
   EEPROM.get(eepromAddress, myObject);
   Serial.print("notahh = ");
   Serial.println(myObject.notahh);
   Serial.print("notasn1 = ");
   Serial.println(myObject.notasn1);
   Serial.print("notaT1HighFloorTom = ");
   Serial.println(myObject.notaT1HighFloorTom);
   Serial.print("notaHighTom = ");
   Serial.println(myObject.notaHighTom);
   
}

void loop()
{
    // empty loop
}
1 Like

Make sure you have "Compiler warnings:" set to "All" in Preferences. Then make sure you can prove that any compiler warnings are not pointing out an actual programming mistake. Fix as many warnings as you can.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.