array within struct causing data problems

I have been trying to make a music player that runs off of an uno using tone, with notes stored in arrays that progresses every time that the loop statement in arduino runs (set with a delay of 10). The problem I am having is that it is just outputting static. I have tried multiple different methods all with the same results. Any help or advice is very appreciated.

#include "note.h"

void playSong(song music[], int pin);
void beats(int timer);
void nextPhrase();
void playPhrase(note music[], int pin);
void endPlaying();
boolean isPlayingMusic();

int delayAmount;
const int dottedWholeNote = (((10000 / delayAmount) / delayAmount) * delayAmount) * 1.3;
const int wholeNote = (((6666 / delayAmount) / delayAmount) * delayAmount) * 1.3;
const int dottedHalfNote = (((5000 / delayAmount) / delayAmount) * delayAmount) * 1.3;
const int halfNote = (((3333 / delayAmount) / delayAmount) * delayAmount) * 1.3;
const int dottedQuarterNote = (((2500 / delayAmount) / delayAmount) * delayAmount) * 1.3;
const int quarterNote = (((1666 / delayAmount) / delayAmount) * delayAmount) * 1.3;
const int dottedENighthNote = (((1250 / delayAmount) / delayAmount) * delayAmount) * 1.3;
const int eighthNote = (((833 / delayAmount) / delayAmount) * delayAmount) * 1.3;
const int dottedSixteenthNote = (((625 / delayAmount) / delayAmount) * delayAmount) * 1.3;
const int sixteenthNote = (((416 / delayAmount) / delayAmount) * delayAmount) * 1.3;

int noteCount = 0;
int nextNote = 0;
int currentPhrase = 0;
int currentNote = 0;

boolean isPlaying = false;

//Ancient Guardian Music
note phrase0[] = {(BN4, eighthNote), (BN3, sixteenthNote), (EN3, sixteenthNote), (FNS3, sixteenthNote), (BN3, sixteenthNote), (EN3, sixteenthNote), (BN3, sixteenthNote), (AN4, sixteenthNote), (BN3, sixteenthNote), (FNS3, sixteenthNote), (BN3, sixteenthNote)};
note phrase1[] = {(BN4, sixteenthNote), (BN3, sixteenthNote), (FNS3, sixteenthNote), (BN3, sixteenthNote), (EN3, sixteenthNote), (BN3, sixteenthNote), (AN3, sixteenthNote), (BN3, sixteenthNote), (GN3, sixteenthNote), (BN3, sixteenthNote), (AN4, sixteenthNote), (BN3, sixteenthNote)};
note phrase2[] = {(BN3, eighthNote), (AN3, eighthNote), (FNS3, eighthNote), (EN3, eighthNote), (AN4, eighthNote), (FNS3, eighthNote)};
note phrase3[] = {(EN3, wholeNote + quarterNote), (GN3, halfNote), (AN4, dottedHalfNote), (CN3, wholeNote + dottedHalfNote), (FN3, dottedWholeNote)};
note phrase4[] = {(AN3, dottedQuarterNote), (BN3, dottedQuarterNote), (EN3, eighthNote), (DN3, quarterNote), (BN4, eighthNote), (AN4, eighthNote), (GN3, eighthNote), (DN3, eighthNote), (EN3, eighthNote), (FNS3, eighthNote), (CN3, eighthNote), (DN3, eighthNote), (EN3, eighthNote), (FNS2, eighthNote), (AN3, eighthNote), (DN2, eighthNote), (DNS2, eighthNote), (EN2, eighthNote), (DN4, eighthNote), (DNS4, eighthNote), (EN4, eighthNote), (FN3, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (FN3, eighthNote), (FN3, eighthNote), (FN3, eighthNote)};

song guardianRobotMusic[] = {(phrase0, 1), (phrase1, 3), (phrase2, 72), (phrase3, 6), (phrase2, 4), (phrase4, 1)};
//************************************

void playSong (song music[], int pin) {
  noteCount = noteCount + delayAmount;
  if (currentPhrase < sizeof(music)) {
    if (!isPlaying || noteCount == music[currentPhrase].phrase[nextNote].duration) {
      tone(pin, music[currentPhrase].phrase[nextNote].noteTone, music[currentPhrase].phrase[nextNote].duration / 1.3);
      nextNote++;
      isPlaying = true;
    } else if (nextNote >= sizeof(music[currentPhrase].phrase)) {
      nextPhrase();
    }
  } else if (currentPhrase >= sizeof(music) && nextNote >= sizeof(music[currentPhrase].phrase)) {
    endPlaying();
  }
}

void beats (int timer) {
  delayAmount = timer;
}

void nextPhrase () {
  currentPhrase++;
  nextNote = 0;
  currentNote = 0;
}

void endPlaying () {
  currentPhrase = 0;
  nextNote = 0;
  currentNote = 0;
}

code.zip (1.86 KB)

int delayAmount;
const int dottedWholeNote = (((10000 / delayAmount) / delayAmount) * delayAmount) * 1.3;

divide-by-zero?

Of course, it could be as simple as the fact that the code you posted lacks both setup and loop functions.
But the compiler already told you that.

The code actually compiled perfectly fine. That is the code from a .h file. The loop and setup are in a different .ino. I will try declaring all of those variables in a different function instead of having them be constant though. I never thought of that.

I tried changing those statements around so that they were declared in the same place as delayAmount but that still led to a constant buzzing noise. I think the problem lies somewhere in my void playSong method. I'm just not sure where that happens to be.

The code actually compiled perfectly fine

The compiler won't spot divide-by-zero problems.

But it will notice the absence of required functions.

So while I fixed the divide by zero error you said is causing a problem I still believe there is a different problem, I’m just not sure what it is. void delay() sets all of the constants, it is declared in void setup before anything else happens, yet the uno still freaks out.

here is the updated code

#include "note.h"

void playSong(song music[], int pin);
void beats(int timer);
void nextPhrase();
void playPhrase(note music[], int pin);
void endPlaying();
boolean isPlayingMusic();

int dottedWholeNote;
int wholeNote;
int dottedHalfNote;
int halfNote;
int dottedQuarterNote;
int quarterNote;
int dottedENighthNote;
int eighthNote;
int dottedSixteenthNote;
int sixteenthNote;
int delayAmount;

int noteCount = 0;
int nextNote = 0;
int currentPhrase = 0;
int currentNote = 0;

boolean isPlaying = false;

//Ancient Guardian Music
note phrase0[] = {(BN4, eighthNote), (BN3, sixteenthNote), (EN3, sixteenthNote), (FNS3, sixteenthNote), (BN3, sixteenthNote), (EN3, sixteenthNote), (BN3, sixteenthNote), (AN4, sixteenthNote), (BN3, sixteenthNote), (FNS3, sixteenthNote), (BN3, sixteenthNote)};
note phrase1[] = {(BN4, sixteenthNote), (BN3, sixteenthNote), (FNS3, sixteenthNote), (BN3, sixteenthNote), (EN3, sixteenthNote), (BN3, sixteenthNote), (AN3, sixteenthNote), (BN3, sixteenthNote), (GN3, sixteenthNote), (BN3, sixteenthNote), (AN4, sixteenthNote), (BN3, sixteenthNote)};
note phrase2[] = {(BN3, eighthNote), (AN3, eighthNote), (FNS3, eighthNote), (EN3, eighthNote), (AN4, eighthNote), (FNS3, eighthNote)};
note phrase3[] = {(EN3, wholeNote + quarterNote), (GN3, halfNote), (AN4, dottedHalfNote), (CN3, wholeNote + dottedHalfNote), (FN3, dottedWholeNote)};
note phrase4[] = {(AN3, dottedQuarterNote), (BN3, dottedQuarterNote), (EN3, eighthNote), (DN3, quarterNote), (BN4, eighthNote), (AN4, eighthNote), (GN3, eighthNote), (DN3, eighthNote), (EN3, eighthNote), (FNS3, eighthNote), (CN3, eighthNote), (DN3, eighthNote), (EN3, eighthNote), (FNS2, eighthNote), (AN3, eighthNote), (DN2, eighthNote), (DNS2, eighthNote), (EN2, eighthNote), (DN4, eighthNote), (DNS4, eighthNote), (EN4, eighthNote), (FN3, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (FN3, eighthNote), (FN3, eighthNote), (FN3, eighthNote)};

song guardianRobotMusic[] = {(phrase0, 1), (phrase1, 3), (phrase2, 72), (phrase3, 6), (phrase2, 4), (phrase4, 1)};
//************************************

void playSong (song music[], int pin) {
  noteCount = noteCount + delayAmount;
  if (currentPhrase < sizeof(music)) {
    if (!isPlaying || noteCount == music[currentPhrase].phrase[nextNote].duration) {
      tone(pin, music[currentPhrase].phrase[nextNote].noteTone, music[currentPhrase].phrase[nextNote].duration / 1.3);
      nextNote++;
      isPlaying = true;
    } else if (nextNote >= sizeof(music[currentPhrase].phrase)) {
      nextPhrase();
    }
  } else if (currentPhrase >= sizeof(music) && nextNote >= sizeof(music[currentPhrase].phrase)) {
    endPlaying();
  }
}

void beats (int timer) {
  delayAmount = timer;
  dottedWholeNote = (((10000) / timer) * timer) * 1.3;
  wholeNote = (((6666) / timer) * timer) * 1.3;
  dottedHalfNote = (((5000) / timer) * timer) * 1.3;
  halfNote = (((3333) / timer) * timer) * 1.3;
  dottedQuarterNote = (((2500) / timer) * timer) * 1.3;
  quarterNote = (((1666) / timer) * timer) * 1.3;
  dottedENighthNote = (((1250) / timer) * timer) * 1.3;
  eighthNote = (((833) / timer) * timer) * 1.3;
  dottedSixteenthNote = (((625) / timer) * timer) * 1.3;
  sixteenthNote = (((416) / timer) * timer) * 1.3;
}

void nextPhrase () {
  currentPhrase++;
  nextNote = 0;
  currentNote = 0;
}

void endPlaying () {
  currentPhrase = 0;
  nextNote = 0;
  currentNote = 0;
}

code.zip (1.88 KB)

I wrote a code that plays tones and enters them in using an array. Since it wasn’t working I decided to add serial in so that I could see if the number are being entered correctly. What I got was weird. If I told it to manually go to a place in the array it would give the correct number, but if it was going through it automatically it wouldn’t. At place 0, 0 it should return 493; instead it is returning 72. And I did check to make sure it was checking the right place in the array, which I believe it is.

The code is in the zip file below. All of the important parts are in the .h files.

If anyone has any ideas on what is going on wrong any help is appreciated greatly.

#include "note.h"

void playSong(song music[], int pin);
void nextPhrase();
void playPhrase(note music[], int pin);
void endPlaying();
boolean isPlayingMusic();

int dottedWholeNote = 1000;
int wholeNote = 660;
int dottedHalfNote = 500;
int halfNote = 330;
int dottedQuarterNote = 250;
int quarterNote = 160;
int dottedENighthNote = 120;
int eighthNote = 80;
int dottedSixteenthNote = 60;
int sixteenthNote = 40;
int delayAmount = 10;

int noteCount = 0;
int nextNote = 0;
int currentPhrase = 0;
int currentNote = 0;

boolean isPlaying = false;

//Ancient Guardian Music
note phrase0[] = {(BN4, eighthNote), (BN3, sixteenthNote), (EN3, sixteenthNote), (FNS3, sixteenthNote), (BN3, sixteenthNote), (EN3, sixteenthNote), (BN3, sixteenthNote), (AN4, sixteenthNote), (BN3, sixteenthNote), (FNS3, sixteenthNote), (BN3, sixteenthNote)};
note phrase1[] = {(BN4, sixteenthNote), (BN3, sixteenthNote), (FNS3, sixteenthNote), (BN3, sixteenthNote), (EN3, sixteenthNote), (BN3, sixteenthNote), (AN3, sixteenthNote), (BN3, sixteenthNote), (GN3, sixteenthNote), (BN3, sixteenthNote), (AN4, sixteenthNote), (BN3, sixteenthNote)};
note phrase2[] = {(BN3, eighthNote), (AN3, eighthNote), (FNS3, eighthNote), (EN3, eighthNote), (AN4, eighthNote), (FNS3, eighthNote)};
note phrase3[] = {(EN3, wholeNote + quarterNote), (GN3, halfNote), (AN4, dottedHalfNote), (CN3, wholeNote + dottedHalfNote), (FN3, dottedWholeNote)};
note phrase4[] = {(AN3, dottedQuarterNote), (BN3, dottedQuarterNote), (EN3, eighthNote), (DN3, quarterNote), (BN4, eighthNote), (AN4, eighthNote), (GN3, eighthNote), (DN3, eighthNote), (EN3, eighthNote), (FNS3, eighthNote), (CN3, eighthNote), (DN3, eighthNote), (EN3, eighthNote), (FNS2, eighthNote), (AN3, eighthNote), (DN2, eighthNote), (DNS2, eighthNote), (EN2, eighthNote), (DN4, eighthNote), (DNS4, eighthNote), (EN4, eighthNote), (FN3, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (REST, eighthNote), (FN3, eighthNote), (FN3, eighthNote), (FN3, eighthNote), (FN3, eighthNote)};

song guardianRobotMusic[] = {(phrase0, 1), (phrase1, 3), (phrase2, 72), (phrase3, 6), (phrase2, 4), (phrase4, 1)};
//************************************

void playSong (song music[], int pin) {
  noteCount = noteCount + delayAmount;
  if (currentPhrase < sizeof(music)) {
    if (!isPlaying || noteCount == music[currentPhrase].phrase[nextNote].duration) {
      tone(pin, music[currentPhrase].phrase[nextNote].noteTone, music[currentPhrase].phrase[nextNote].duration / 1.3);
      nextNote++;
      Serial.println(BN4);
      Serial.println(music[currentPhrase].phrase[nextNote].noteTone);
      Serial.println(music[currentPhrase].phrase[nextNote].duration);
      Serial.println(currentPhrase);
      Serial.println(nextNote);
      isPlaying = true;
    } else if (nextNote >= sizeof(music[currentPhrase].phrase)) {
      nextPhrase();
    }
  } else if (currentPhrase >= sizeof(music) && nextNote >= sizeof(music[currentPhrase].phrase)) {
    endPlaying();
  }
}

void nextPhrase () {
  currentPhrase++;
  nextNote = 0;
  currentNote = 0;
}

void endPlaying () {
  currentPhrase = 0;
  nextNote = 0;
  currentNote = 0;
}

code.zip (1.84 KB)

@ZGoode, please do not cross-post. Threads merged.