Playing song using I2c with 2 UNO boards

I am fairly new to the world of Arduino programming and need help with some code. The intended purpose of the code is to designate one board to be a “master” and the other as a “slave”. I am trying to get the master to send a simple “1” integer to the slave, which then runs the music playing program. The master program runs fine but I am having trouble with the slave program. The “void playTone ()” function is meant to be run outside of the void loop and I cannot get it to call it and run the function. The code should work but any help would be appreciated. Thanks for the help! I have attached the slave code below

#include <Wire.h>
#define c 3830 // 261 Hz
#define d 3400 // 294 Hz
#define e 3038 // 329 Hz
#define f 2864 // 349 Hz
#define g 2550 // 392 Hz
#define a 2272 // 440 Hz
#define b 2028 // 493 Hz
#define C 1912 // 523 Hz
#define R 0

int speakerOut = 9;
int DEBUG = 1;
int x = 1;

void setup() {
int melody = { C, b, g, C, b, e, R, C, c, g, a, C };
int beats = { 16, 16, 16, 8, 8, 16, 32, 16, 16, 16, 8, 8 };
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables
int tone_ = 0;
int beat = 0;
long duration = 0;

Wire.begin(9);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
}

void receiveEvent(int bytes) {
x = Wire.read(); // read one character from the I2C
if (x == ‘1’){
playTone ();
}
}}

void loop() {
// Set up a counter to pull from melody and beats
for (int i=0; i<MAX_COUNT; i++) {
tone_ = melody*;*
_ beat = beats*;_
_ duration = beat * tempo; // Set up timing_
_
playTone();_
_
// A pause between notes…_
_
delayMicroseconds(pause);_
_
if (DEBUG) { // If debugging, report loop, tone, beat, and duration*_
* Serial.print(i);*
* Serial.print(":");*
* Serial.print(beat);*
* Serial.print(" "); *
* Serial.print(tone_);
_
Serial.print(" ");_
_
Serial.println(duration);_
_
}_
_
}_
_
}_
void playTone() {
long elapsed_time = 0;
if (tone_ > 0) { // if this isn’t a Rest beat, while the tone has*

* // played less long than ‘duration’, pulse speaker HIGH and LOW*
* while (elapsed_time < duration) {
_
digitalWrite(speakerOut,HIGH);_
delayMicroseconds(tone_ / 2);
_
// DOWN*_
* digitalWrite(speakerOut, LOW);*
* delayMicroseconds(tone_ / 2);
_
// Keep track of how long we pulsed*_
* elapsed_time += (tone_);
_
}_
_
}_
_
else { // Rest beat; loop times delay*_
* for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count*
* delayMicroseconds(duration); *
* } *

* }*

}

The receiveEvent() should be very short and fast. It is (part of) an interrupt handler.

It is possible to send a commend from the receiveEvent() to the loop() with a variable. If only a 8-bit variable is used, there is no need to turn off interrupts when it is read or written in the loop. But it must be made 'volatile'.

...
volatile char command = 0;              // set de default : no command
...

void receiveEvent(int bytes) 
{
  command = Wire.read();    // read one character from the I2C
}
...
void loop()
{
  if( command != 0)        // received a command ?
  {
    playTone...
    ...
    command = 0;            // clear the command
  }
}