【解決方法】複数の定義済み配列の 1 つを関数に渡すにはどうすればよいですか?


私はArduinoドアベルを構築しており、スピーカーで再生する曲のメモを含む曲ごとに個別の配列を持っています。 曲を選択して Arduino にダウンロードしたら、ドアベル割り込みで曲を再生します。 私が理解できないのは、各曲を再生するための個別の関数なしで、曲配列のアドレスを共通の「PLAY」関数に渡す方法です。

私が得ているエラーは

error: cannot convert 'const int (*)[8]' to 'int*' in assignment

私が試したこと:

POINTERS と REFERENCING を使用してみましたが、使い方に慣れていないため、動作しないようです。
私の関連コード:

C
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 

      break;
    case 2:
      tuneToPlay = &song2;
      break;
    default:
       tuneToPlay = song1;
      break;
  }

割り込み関数は別の関数を呼び出すべきではないため、これをインラインコードに変更しました(私が理解しているように)。

C
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;
    delay(pauseBetweenNotes);
    noTone(speakerPin);  // stop the tone playing:
    buttonFlag = 1;      // indicate the ISR has been executed
  }

解決策 1

仮定 song1 と定義されている

C++
int song1[8];

を削除してみてください addressOf 配列名からの演算子:

C++
tuneToPlay = song1;

しかし、あなたが含まれていないので、それは少し推測です song1 あなたのコードサンプルで。

コメント

タイトルとURLをコピーしました