EEPROM - constant buzzing from piezo transducer

Hello,

I am new to Arduino.

My project is an electronic piano and it records and plays back tunes. The frequencies are recorded into EEPROM. When I am getting the frequencies out of EEPROM, the notes are clear, however there is a constant loud low frequency buzzing in the background (stops when the 500 loop ends). This only started happening when I saved the frequencies into EEPROM (worked fine when I did not use EEPROM).

Is the placement of EEPROM put and get correct? What do you think is the problem?

Thanks in advance.

Code:

#include <EEPROM.h> 
#include "pitches.h"
#define speaker A5
#define record_button 3
#define play_button 5
#define record_led 2
#define play_led 4
#define melody_button A0

int customVar;
int eeAddress = 0;
int eeAddressplay = 0;

const int inPot = A1; //potentiometer to change octaves (8 octaves in total)
byte octave; 
byte tone_out; // specifies elements in melody array
int tone_key; // frequency of key
boolean silence; // silence variable is either true or false

int maxNumber = 500;
byte time = 20; //increase, teach about sampling, break data into discrete time segments, memory

byte buttons[] = {6,7,8,9,10,11,12,13};
int saveSounds[500];
int saveNumber = 0;

byte melody1[8] = { 
  NOTE_C1 , NOTE_D1, NOTE_E1, NOTE_F1, NOTE_G1, NOTE_A1, NOTE_B1, NOTE_C2};
byte melody2[8] = { 
  NOTE_C2 , NOTE_D2, NOTE_E2, NOTE_F2, NOTE_G2, NOTE_A2, NOTE_B2, NOTE_C3};
int melody3[8] = { 
  NOTE_C3 , NOTE_D3, NOTE_E3, NOTE_F3, NOTE_G3, NOTE_A3, NOTE_B3, NOTE_C4};
int melody4[8] = { 
  NOTE_C4 , NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5};
int melody5[8] = { 
  NOTE_C5 , NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6};
int melody6[8] = { 
  NOTE_C6 , NOTE_D6, NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6, NOTE_C7};
int melody7[8] = { 
  NOTE_C7 , NOTE_D7, NOTE_E7, NOTE_F7, NOTE_G7, NOTE_A7, NOTE_B7, NOTE_C8};

//PIANO FUNCTION
void freePlay() // piano function which plays 8 octaves (8 octaves in total, 8 keys per octave)
{
  octave = map(analogRead(inPot),0,1023,1,7); // analogRead: 0-5V input voltage maps to integer values 0-1023
                                              // integer values 0-1023 maps to 1-8  
  if (digitalRead(buttons[0]) == HIGH) {      
    tone_out = 0;                             // if the first button is pressed, tone_out = 0 and silence = false
    silence = false;                          // skips to if (silence == false) line
  } 
  else if (digitalRead(buttons[1]) == HIGH) {
    tone_out = 1;  
    silence = false;
  } 
  else if (digitalRead(buttons[2]) == HIGH) {
    tone_out = 2; 
    silence = false;
  } 
  else if (digitalRead(buttons[3]) == HIGH) {
    tone_out = 3;  
    silence = false;
  } 
  else if (digitalRead(buttons[4]) == HIGH) {
    tone_out = 4;  
    silence = false;
  } 
  else if (digitalRead(buttons[5]) == HIGH) {
    tone_out = 5; 
    silence = false;
  } 
  else if (digitalRead(buttons[6]) == HIGH) {
    tone_out = 6; 
    silence = false;
  } 
  else if (digitalRead(buttons[7]) == HIGH) {
    tone_out = 7;
    silence = false;
  } 
  else {
    silence = true; // if none of the if/elseif statements are true, execute silence = true            
  }

  if (silence == false)
  {
    switch(octave) // depending on which octave is selected (1-8), the corresponding switch case statement is executed
    {
    case 1: 
      tone_key = melody1[tone_out]; break;
    case 2: 
      tone_key = melody2[tone_out]; break;
    case 3: 
      tone_key = melody3[tone_out]; break;
    case 4: 
      tone_key = melody4[tone_out]; break;
    case 5: 
      tone_key = melody5[tone_out]; break;
    case 6: 
      tone_key = melody6[tone_out]; break;
    case 7: 
      tone_key = melody7[tone_out]; break;
    }
    tone(speaker,tone_key); // Arduino inbuilt tone function generates a square wave of the specified frequency  
  }                         // at 50% duty cycle on the speaker pin
  else {
   noTone(speaker); // wave continues until a call to noTone()      
   delay (5); } // 5 millisecond delay
}

//SETUP FUNCTION
void setup() 
{ 
 for(int i = 0; i < 8; i++)
 {
  pinMode(buttons[i], INPUT); 
 } 
 pinMode(speaker, OUTPUT); 
 pinMode(melody_button, INPUT);
 pinMode(record_led, OUTPUT);
 pinMode(play_led, OUTPUT);
 pinMode(inPot, INPUT);
 Serial.begin(9600);
}

//MAIN CODE
void loop() {

freePlay(); 
if (digitalRead(record_button) == HIGH) {
  digitalWrite(record_led, HIGH);
    for(int i = 0; i < maxNumber ; i++) {
      octave = map(analogRead(inPot),0,1023,1,7);                                           
        if (digitalRead(buttons[0]) == HIGH) {      
        tone_out = 0;                             
        silence = false;                          
        } 
        else if (digitalRead(buttons[1]) == HIGH) {
        tone_out = 1;  
        silence = false;
        } 
        else if (digitalRead(buttons[2]) == HIGH) {
        tone_out = 2; 
        silence = false;
        } 
        else if (digitalRead(buttons[3]) == HIGH) {
        tone_out = 3;  
        silence = false;
        } 
        else if (digitalRead(buttons[4]) == HIGH) {
        tone_out = 4;  
        silence = false;
        } 
        else if (digitalRead(buttons[5]) == HIGH) {
        tone_out = 5; 
        silence = false;
        } 
        else if (digitalRead(buttons[6]) == HIGH) {
        tone_out = 6; 
        silence = false;
        } 
        else if (digitalRead(buttons[7]) == HIGH) {
        tone_out = 7;
        silence = false;
        } 
        else {
        silence = true;
        tone_key = 0;
        }

        if (silence == false) {
        switch(octave) { 
        case 1: tone_key = melody1[tone_out]; break;
        case 2: tone_key = melody2[tone_out]; break;
        case 3: tone_key = melody3[tone_out]; break;
        case 4: tone_key = melody4[tone_out]; break;
        case 5: tone_key = melody5[tone_out]; break;
        case 6: tone_key = melody6[tone_out]; break;
        case 7: tone_key = melody7[tone_out]; break;
        }
        tone(speaker,tone_key); 
        delay(time);
        }                         
        else {
        noTone(speaker);
        tone_key = 0; 
        delay (time);
        } 
   
        saveSounds[saveNumber] = tone_key;
        saveNumber++;
        if (digitalRead(play_button) == HIGH){
        break;
        }
    }

    digitalWrite(record_led, LOW);
    tone(speaker, 700);
    delay(200);
    noTone(speaker);
    
    for (int i = 0; i<500; i++) {
        EEPROM.put(eeAddress,saveSounds[i]);
        eeAddress = eeAddress + 2;
    }
    
}

    if(digitalRead(play_button) == HIGH) {
      digitalWrite(play_led, HIGH);
        for(int i = 0; i < maxNumber; i++) {
          EEPROM.get(eeAddressplay,customVar);
          tone(speaker,customVar);
          eeAddressplay = eeAddressplay + 2;
          delay(time);
        }
      noTone(speaker);
      digitalWrite(play_led,LOW);
    }
}

I have no idea about the cause of your problem, so the below will not solve that

I have however an idea to reduce the code by around a 100 lines and make it a lot more readable (in my opinion)

For your button reads, you can use a for-loop. e.g. in freePlay()

  silence = true;
  for (int btnCnt = 0; btnCnt < sizeof(buttons) / sizeof(buttons[0]); btnCnt++)
  {
    if (digitalRead(buttons[btnCnt]) == HIGH)
    {
      tone_out = btnCnt;
      silence = false;
      break;
    }
  }

You can also use a two-dimensional array for your melodies

int melodies[][8] =
{
  {NOTE_C1 , NOTE_D1, NOTE_E1, NOTE_F1, NOTE_G1, NOTE_A1, NOTE_B1, NOTE_C2},
  {NOTE_C2 , NOTE_D2, NOTE_E2, NOTE_F2, NOTE_G2, NOTE_A2, NOTE_B2, NOTE_C3},
  {NOTE_C3 , NOTE_D3, NOTE_E3, NOTE_F3, NOTE_G3, NOTE_A3, NOTE_B3, NOTE_C4},
  {NOTE_C4 , NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5},
  {NOTE_C5 , NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6},
  {NOTE_C6 , NOTE_D6, NOTE_E6, NOTE_F6, NOTE_G6, NOTE_A6, NOTE_B6, NOTE_C7},
  {NOTE_C7 , NOTE_D7, NOTE_E7, NOTE_F7, NOTE_G7, NOTE_A7, NOTE_B7, NOTE_C8}
};

And in freePlay()

  if (silence == false)
  {
    if (octave >= 1 || octave <= 7)
    {
      tone_key = melodies[octave - 1][tone_out];
    }
    tone(speaker, tone_key); // Arduino inbuilt tone function generates a square wave of the specified frequency
  }                         // at 50% duty cycle on the speaker pin
  else
  {
    noTone(speaker); // wave continues until a call to noTone()
    delay (5);
  } // 5 millisecond delay

I leave the implementation to you

The complete loop()

//MAIN CODE
void loop()
{

  freePlay();
  if (digitalRead(record_button) == HIGH)
  {
    digitalWrite(record_led, HIGH);
    for (int i = 0; i < maxNumber ; i++)
    {
      octave = map(analogRead(inPot), 0, 1023, 1, 7);

      silence = true;
      tone_key = 0;
      for (int btnCnt = 0; btnCnt < sizeof(buttons) / sizeof(buttons[0]); btnCnt++)
      {
        if (digitalRead(buttons[btnCnt]) == HIGH)
        {
          tone_out = btnCnt;
          silence = false;
          break;
        }
      }

      if (silence == false)
      {
        if (octave >= 1 || octave <= 7)
        {
          tone_key = melodies[octave - 1][tone_out];
        }
        tone(speaker, tone_key);
        delay(time);
      }
      else
      {
        noTone(speaker);
        tone_key = 0;
        delay (time);
      }

      saveSounds[saveNumber] = tone_key;
      saveNumber++;
      if (digitalRead(play_button) == HIGH)
      {
        break;
      }
    }

    digitalWrite(record_led, LOW);
    tone(speaker, 700);
    delay(200);
    noTone(speaker);

    for (int i = 0; i < 500; i++)
    {
      EEPROM.put(eeAddress, saveSounds[i]);
      eeAddress = eeAddress + 2;
    }
  }

  if (digitalRead(play_button) == HIGH)
  {
    digitalWrite(play_led, HIGH);
    for (int i = 0; i < maxNumber; i++)
    {
      EEPROM.get(eeAddressplay, customVar);
      tone(speaker, customVar);
      eeAddressplay = eeAddressplay + 2;
      delay(time);
    }
    noTone(speaker);
    digitalWrite(play_led, LOW);
  }
}

Compiles, not tested