Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm building an Arduino doorbell and have individual arrays for each song that contain song notes to play on a speaker. Once the song is selected and downloaded to Arduino, I want the doorbell interrupt to play the song. What I can't figure out is how to pass the address of the song array to a common "PLAY" function without a separate function to play each song.

The error I am getting is
error: cannot convert 'const int (*)[8]' to 'int*' in assignment

What I have tried:

I tried using POINTERS and REFERENCING but, being unfamiliar with how to use them, they don't seem to work.
My relevant code:
void ISR_setSong() {    // this is defined button interrupt  
  // This switch statement identifies the selected song to play

   switch (song) {
    case 1:
      tuneToPlay = &song1;    // Globally defined as int *tuneToPLay 

    case 2:
      tuneToPlay = &song2;
       tuneToPlay = song1;
I changed this to be inline code because an interrupt function should not call another function (as I understand).
void ISR_playSong(int tuneToPlay[], int beatsToPlay[]) {
  int allNotes = int(tuneToPlay[0]);  // cast first element to integer
  for (int thisNote = 1; thisNote < allNotes; thisNote++) {
    int noteDuration = 1000 / beatsToPlay[thisNote];
    tone(speakerPin, tuneToPlay[thisNote], noteDuration);  //play the note
    int pauseBetweenNotes = noteDuration * 1.30;
    noTone(speakerPin);  // stop the tone playing:
    buttonFlag = 1;      // indicate the ISR has been executed
Updated 30-Apr-23 7:10am
Rick York 26-Apr-23 11:34am    
Your code is incomplete because the types of variables song1 and song2 are not shown. That is likely to be the problem but we can only guess.

Assuming song1 is defined as
int song1[8];

The try removing the addressOf operator from the array name:
tuneToPlay = song1;

But that is a bit of a guess since you have not included song1 in your code sample.

It also may be due to the fact that you are effectively removing the const qualifier in the ISR_playSong function.
Share this answer
Just a wild guess: it looks there is a type mismatch between song1 (as well song2) and tuneToPlay. Namely, the former is a pointer to a function, while the latter is a pointer to int.
You should provide more code (e.g. song1 declaration) in order to get better help.
Share this answer
You better understand this pointer issue becaue it is a central pillar of C. Visit some tutorial and try out the example. For instance Learn C++ pointers.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900