0

I am working on an Arduino project (making it a music player) and I have an error that I cannot figure out, even after a solid google session. Here is my code, I stripped non-important stuff.

I have 5 songs witch I want to cycle through

//#define notes

int songLength;
int melody;
int noteDurations;
unsigned long verschil = 0;
int thisNote = 0;
double speed;
int song = 1;

void song1(melody, noteDurations) {
  memset(melody, 0, sizeof(melody));
  memset(noteDurations, 0, sizeof(noteDurations));
  songLength = 112;
  melody[] = {
//the notes of the song
 };

// note durations: 4 = quarter note, 8 = eighth note, etc.:
noteDurations[] = {
//the noteduration of the song
  };
}

void song2(melody, noteDurations) {
  memset(melody, 0, sizeof(melody));
  memset(noteDurations, 0, sizeof(noteDurations));
  songLength = 72;
  melody[] = {
//the notes of the song
};
noteDurations[] = {
//the noteduration of the song
};
}

void song3(melody, noteDurations) {
  memset(melody, 0, sizeof(melody));
  memset(noteDurations, 0, sizeof(noteDurations));
  songLength = 24;
  melody[] = {
//the notes of the song
};

noteDurations[] = {
//the noteduration of the song
};
}

void song4(melody, noteDurations) {
  memset(melody, 0, sizeof(melody));
  memset(noteDurations, 0, sizeof(noteDurations));
  songLength = 105;
  melody[] = {
//the notes of the song
};

noteDurations[] = {
//the noteduration of the song};
}

void song5(melody, noteDurations) {
  memset(melody, 0, sizeof(melody));
  memset(noteDurations, 0, sizeof(noteDurations));
  songLength = 80;
  melody[] = {
//the notes of the song
};

// note durations: 4 = quarter note, 8 = eighth note, etc
noteDurations[] = {
//the noteduration of the song
};
}

void loadSong(song) {
  if (song == 1) {
    song1(melody, noteDurations);
  }
  if (song == 2) {
    song2(melody, noteDurations);
  }
  if (song == 3) {
    song3(melody, noteDurations);
  }
  if (song == 4) {
    song4(melody, noteDurations);
  }
  if (song == 5) {
    song5(melody, noteDurations);
    song = 1;
  } else {
    song++;
  }
}

void setup()
{
  //setup
  loadSong(song);
}

void loop() {
  //playing the song
  int noteDuration = 750 / noteDurations[thisNote];
  tone(13, melody[thisNote], noteDuration);
  
  int sensorValue = analogRead(A1);
  //Serial.println(sensorValue);
  if (sensorValue < 200) {
    speed = 0;
  } else {
    speed = sensorValue * 0.001;
  }
  speed = speed + 0.2;
  Serial.println(speed);
  int pauseBetweenNotes = noteDuration * 1.30 * speed;
  delay(pauseBetweenNotes);
    
  noTone(13);
  if (thisNote < songLenght) {
    thisNote++;
  } else {
    thisNote = 0;
    loadSong(song);
  }
}

I get the following error at each song function I declare and define:

101:12: error: variable or field 'song1' declared void
169:12: error: variable or field 'song2' declared void
195:12: error: variable or field 'song3' declared void
214:12: error: variable or field 'song4' declared void
251:12: error: variable or field 'song5' declared void

I really don't know what to do know, all the answers I find on google are related when calling the function. but the error occurs on the line where I make the function and it's contents.

I hope someone can help me out

ps. I am also getting some other errors but they are not relevant to this question, I will show them anyways

287:19: error: variable or field 'loadSong' declared void
 In function 'void setup()':
325:3: error: 'loadSong' was not declared in this scope
325:3: note: suggested alternative: 'long'
 In function 'void loop()':
329:50: error: invalid types 'int[int]' for array subscript
330:27: error: invalid types 'int[int]' for array subscript
345:18: error: 'songLenght' was not declared in this scope
345:18: note: suggested alternative: 'songLength'
349:5: error: 'loadSong' was not declared in this scope
349:5: note: suggested alternative: 'long'
  • 4
    You mustn't omit types of parameters in function definition in C++. – MikeCAT Mar 23 '21 at 13:00
  • 1
    I honestly don't see much of C++ here. Maybe it's because you come from a different language like js or python. You can't expect that C++ simply works as other languages, a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) will help you get started. – Lukas-T Mar 23 '21 at 13:07
  • In the defintion of void loadSong(song) song doesn't appear to be a type. It appears to be an integer value. Among other things as pointed out by commenters. Edit: On inspection none of the formal parameters appear to be types. – systemcpro Mar 23 '21 at 13:16
  • 1
    Does this answer your question? [variable or field declared void](https://stackoverflow.com/questions/364209/variable-or-field-declared-void) – Proko Mar 23 '21 at 20:36

1 Answers1

0

While you declare a function you need to secify the type of arguments:

void playSong(std::string songName, float songDuration){
...
}
S.Seba
  • 121
  • 1
  • 11
  • When i tried to do that for example to song1, I get a lot more errors: `103:35: error: invalid conversion from 'int' to 'void*' [-fpermissive] 25:0, 1: 235:14: note: initializing argument 1 of 'void* memset(void*, int, size_t)' 104:49: error: invalid conversion from 'int' to 'void*' [-fpermissive] 25:0, 1: 235:14: note: initializing argument 1 of 'void* memset(void*, int, size_t)' 106:10: error: expected primary-expression before ']' token 138:15: error: expected primary-expression before ']' token` – Jelle Driessen Mar 23 '21 at 13:26
  • @JelleDriessen Because almost every line of your code is broken or non-sensical. I mean no offense, that's how it is in all due honesty. You started step 10 before properly completing step 1. It might be a good idea to pick up a good C++ book and start all over again. Don't write so much code at once. Write only a really small piece at a time, then compile it and if you get any errors you must understand and fix them before proceeding with the next small step. C++ is really hard to learn, especially if you dive into it unguided. – Lukas-T Mar 23 '21 at 13:45