EEPROM and TONE libs, together = strange behavoir ?

Hey Community,

I have done my first sketch, but now on the final bits and bytes I ran into a stange behavoir.
I made a little video poker, as a birthday present for my Dad, I use a Arduino mini pro, 328 Atmel and a Hitachi display wired up with pberry´s 2-Wire shift register.
When everything worked like a charm I was using the following libs to make the magic happen:

On startup, my sketch (and hardware) shows a titlescreen , playing a melodie with 2 tones/voices ( using timer 1 ) . Everything worked.

Then I added a highscore-list, using EEPROM.h , funny thing is, now tone() can only make 1 Voice, the first one.
At first I thought I got to much RAM in use , and checked on that with a memfree routine from Arduino test suits, I have no other Serial.prints etc. While the screen runs, I have 650kb free RAM left.
I uncommented my highscore function, and had 2 Voices playing…

Is the a specific order in setup() where you make your objects.
my code looks like this

//define DEBUG 

#include LiquidCrystal_SR2W.h
#include Tone.h 
#include frequencies.h // Noteplayer Frequencies
#include  avr/pgmspace.h
#include  eeprom.h

LiquidCrystal_SR2W lcd(3,4,POSITIVE);

Tone notePlayer[2] ;
struct HIGHSCORE {
char version[4];
long time,
int[8] name
} highscore_s = {
"poke",
123456789,
{'P', 'l' ,'a' ,'y','e','r','o','n'}
}

setup() {
void setup()
{   
    //Debugging
    #if defined DEBUG 
    Serial.begin(9600);
    Serial.println("--------------GO!--------------");
    #endif
    
    lcd.begin(16,2);                           // Initalisiere LCD 
    randomSeed(analogRead(RANDSEEDPIN));       // Zufallsgenerator
    pinMode(Button,INPUT);                     // Der Knopf
    digitalWrite(Button,HIGH);                 // interner PullUp Widerstand anschalten
    notePlayer[0].begin(5);                    // Stimme 1
    notePlayer[1].begin(7);                    // Stimme 2 
    gamestatus = 20;                           // Start mit Intro
   
   /*
    loadHighscore();                           // Highscore laden
    String ztime = TimeToString(highscore_s.time);
    String highroller;
    for(int i=0; i<8; i++) {
      highroller = highroller + highscore_s.name[i];  
    }
    MESSAGE = MESSAGE + " " + highroller + "has beaten this game in " + ztime + ",try to be better!";
    MESSAGE_LENGTH = MESSAGE.length() - 1 ;
*/
   
}

}

loop(){
//Do Stuff like play tone, make a little Intro etc....
}

void loadHighscore() {
  if (EEPROM.read(HIGHS_START + 0) == HIGHS_VERSION[0] &&
      EEPROM.read(HIGHS_START + 1) == HIGHS_VERSION[1] &&
      EEPROM.read(HIGHS_START + 2) == HIGHS_VERSION[2])
    for (unsigned int t=0; t<sizeof(highscore_s); t++)
      *((char*)&highscore_s + t) = EEPROM.read(HIGHS_START + t);
}

I can post the complete code, but I don´t think it is necessary.
When in setup the “load highscore” is uncommented, everything works. When I use it, notePlayer[1], doesn´t work any more, it´s just nothing coming out the speaker, values are passed by to notePlayer[1] , I checked that too.

So I am pretty shure that I have enough RAM left, and everything works too, only the second voice is not playing. I don´t have any other issues…

Is it the order of notePlayer[1].begin(7) and loadHighscore(), should i load first, then make notePlayer[1].begin(7) ? Maybe its a timer issue, because eeprom takes a timer, maybe I should take other pins than 5+7 . I don´t like guessing, I like to knkow what went wrong.

Cheers André

xlr8_at: I can post the complete code, but I don´t think it is necessary.

It is necessary - the code you posted would obviously not compile so we have no way of knowing what your real code is or what it may be doing that's related to the problem. For example, the critical code in loadHighscore() uses variables which are not defined.

When asking for help with a sketch you need to post a complete sketch that demonstrates the problem. If the sketch includes a significant amount of code that is not relevant to the problem then produce a minimal test sketch which demonstrates the problem in the simplest possible way. Quite often the mere act of creating that reveals a misunderstanding or false assumption which enables youto solve the problem for yourself, but if not then it has reduced the amount of code we need to wade through.

As a starting point, I suggest you use the EEPROM read anything and write anything functions from the playground to read/write your high scores structure to/from EEPROM, and I strongly suggest you stop using the problematic String class, and remember that when dealing with strings they almost always need to be null-terminated - which yours aren't. I can't tell whether that's going to cause a problem here because you haven't posted the code using this data, but it's pretty likely to cause a problem.

Hy Peter, thanks for the hints.Next time I ask for support , I will give a compiling code. You are totally right with using char[n] = "mystring" instead of String. While I´m doing this sketch, I´m reading two books about good Arduino coding same time. Both said the same. As I started a week ago with Arduino I am a total noob at such tiny enviroments.

Well after all, I got it solved working everything works fine, if I read the highscore from eeprom (now with no String), and then initalize the notePlayer[].beginn(); if I change back, same odd behavoir.

I am pretty sure, that it has something to do with the timer1 which is used by Tone library to generate the frequencies. But still I would like to know what is happening.

When I am finished later on, optimizing my sketch, I will put it up, maybe someone can answer the question.

Regards André

String related problems often result in memory corruption; the consequences of that can be altered dramatically by small and seemingly irrelevant changes in the code. I don't know what's causing your problems, and it is entirely possible that it's some form of hardware conflict as you suggest, but more likely IMO that there is a bug in your code which is causing memory corruption. Using string and String functions on char arrays which are not null-terminated is one excellent way to provoke this sort of problem, and there was enough going on in your sketch to make this the prime suspect IMO.

Hy Peter,

after optimizing the code and getting rid of String, everything works like a charm, even if I change the 2 lines in code like when it went bogus. After all, it lost about 5kb weight, thanks for the hint, after diggin deeper into String problems and how to get rid of it, I think it made my coding better. This was my first project, anyway. I am a little smarter now :stuck_out_tongue:
If anybody want to have a (now working) video-poker for LCD Display, feel free to download the sketch here http://www.xlr8.at/downloads/pokeruinoV100.rar. If anybody looks at the code, and sees bad coding (there will be a lot , I promise), please give me a poke, or a PM with improvement. I am willing to learn.

So long
André