   timerValuePrev = 0;
   isCloudBitInputHigh = false;
   melodyCount = 0;
-  noteIndex = 0;
-  nextToneTime = 0;
-  nextNoToneTime = 0;
+  resetMelody();
 }
 
 // if start button is pressed, increase timerValue by START_VAL

 {_B, _A, _G, _F, _D, _C, _D, _C, 
  _A, _B, _A, _G, _F, _G, _A,
  _C, _B, _A, _G, _F, _G, _c, _A, _F,
  _C, _D, _E, _F, _E, _D, _C, _F};

// durations of the melody
const int duration[] = 
 {1, 1, 1, 2, 1, 2, 1, 2,
  1, 1, 1, 1, 2, 1, 4,
  1, 1, 1, 1, 2, 1, 2, 1, 2,
  1, 1, 1, 1, 1, 1, 1, 4};

// total number of notes
const int NOTES_COUNT = 4;

// duration of each note
const int NOTE_DUR = 200;

// states
int timerValue = 0; // current timer value

  digitalWrite(SPEAKER_OUT, blinkerValue);
}

// start playing melody for MAX_MELODY_COUNT times
void startPlayingMelody() {
  melodyCount = MAX_MELODY_COUNT;
  analogWrite(COUNTER_LED, 0); // to avoid conflict with tone()
}

// play next tone if melodyCount > 0
void playNextTone() {  
  
  // if it is not playing melody, return
  if (melodyCount == 0) return;
  
  // if next tone time has reached, play next note
  if (millis() > nextToneTime) {
    int nextTone = pooh_melody[noteIndex];
    if (nextTone != _0) {
      tone(SPEAKER_OUT, nextTone);
    } else {
      noTone(SPEAKER_OUT);
    }
    nextToneTime = millis() + (duration[noteIndex] * NOTE_DUR);
    nextNoToneTime = millis() + (duration[noteIndex] * NOTE_DUR * NOTE_DUTY);
    noteIndex++;
  }

