PLEASE HELP! Clearing + Reading from Flash memory

Hi there,

  1. How do I clear what I put in PROGMEM?

  2. Why am I getting garbage noise from the following, when I try to read from PROGMEM? I’ve spent hours trying to figure this out. Could someone please help! Thank you so much in advance.

#include <avr/pgmspace.h>
#define speaker A5
int k;

const int melody[56] PROGMEM = {
NOTE_G4, NOTE_C5, NOTE_G4, NOTE_E4,NOTE_G4,NOTE_C5,NOTE_C5,NOTE_C5,
NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_D5,
NOTE_G4, NOTE_C5, NOTE_G4, NOTE_E4, NOTE_C4, NOTE_G4,NOTE_G4,NOTE_G4,
NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4,
NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_E4,
NOTE_G4, NOTE_A4, NOTE_C5, NOTE_F5, NOTE_E5, NOTE_D5,
NOTE_G4, NOTE_A4,NOTE_B4, NOTE_C5, NOTE_A4, NOTE_G4, NOTE_C5, NOTE_C5,
NOTE_D5,NOTE_E5, NOTE_C5, NOTE_D5, NOTE_B4, NOTE_C5
};

void setup(){
pinMode(speaker, OUTPUT);
}

void loop() {
for (k = 0; k < 55; k++)
{
tone(A5, pgm_read_word_near(melody + k));
}
}

How long are you expecting each note to sound for ?
Try adding a duration when calling the tone() function or put your own delay() in the for loop.

Thank you for your reply.

I have put in a duration of 100msec but I am having the same problem - continuous, non stop noise.

I have put in a duration of 100msec

1/10th of a second for each note. That is not a lot, is it ? Try increasing the value. Does it take longer to play the sequence of notes ?

Also, because you have your for loop inside the loop() function the note sequence will repeat over and over. As an experiment put in a delay of 2000 as the last statement in the loop() function as well as changing the individual note durations. This will introduce a gap between each rendition of the notes.

You could also try printing the values read from PROGMEM before playing the note. It may give you more of an idea what is going on and when.

  1. How do I clear what I put in PROGMEM?

Load a new sketch. PROGMEM is read only. You can't write new data to it at runtime.

  1. Why am I getting garbage noise from the following, when I try to read from PROGMEM?

What makes you think that the problem is in any way related to PROGMEM? The tone() function is non-blocking. It does NOT wait for the specified interval to expire.

"Load a new sketch. PROGMEM is read only. You can't write new data to it at runtime."

You can do it, but you need to re-bootload your part:

Generally, just loading a new sketch is all that is needed tho.