Playing a song with IR remote

Hello everyone. I’m kind of a newbie and I need help on how to play “melody” when a button is pressed on my remote and stops when pressed again. I assigned the button as variable “songButton”.

Any help is appreciated. Thanks :slight_smile:

*/

// ************************
// Includes
// ************************
#include <IRremote.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <Ultrasonic.h>
#include "pitches.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// ************************
// Constants
// ************************
// pin allocation
Ultrasonic ultrasonic(5, 6); // trig = 5, echo = 6
const int redLed =  10;      // red LED
const int yellowLed = 11;     // yellow LED
const int greenLed =  12;    // green LED
const int RECV_PIN = 2;      // remote sensor receive
const int speaker = 8;

// define the controls for remote
const long upButton =  0xFF629D;
const long downButton = 0xFFA857;
const long okButton =  0xFF02FD;
const long rightButton = 0xFFC23D;
const long leftButton = 0xFF22DD;
const long songButton = 0xFF18E7;       // button 5




// ************************
// Initializations
// ************************

// notes in melody
int melody[] = {
  NOTE_DS5, NOTE_E5, NOTE_FS5, 0, NOTE_B5, NOTE_E5, NOTE_DS5, NOTE_E5, NOTE_FS5 
};

// note durations: 4 = quarter note, 8 = eighth note, etc
int noteDurations[] = {
  16,16,16,16,16,16,16,16 
};


// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *leftMotor = AFMS.getMotor(1);
Adafruit_DCMotor *rightMotor = AFMS.getMotor(2);


IRrecv irrecv(RECV_PIN);
decode_results results;



// ************************
// Setup
// ************************
void setup()
{

  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(speaker, OUTPUT);

  Serial.begin(9600);
  // In case the interrupt driver crashes on setup, give a clue
  // to the user what's going on.
  Serial.println("Enabling IRin");
  irrecv.enableIRIn(); // Start the receiver
  Serial.println("Enabled IRin");

  AFMS.begin();  // create with the default frequency 1.6KHz
  rightMotor->setSpeed(255);
  leftMotor->setSpeed(255);

  //iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < sizeof(melody) / sizeof(melody[0]); thisNote++) {

    /*
      to calculate the note duration, take one second divided by the note type.
      e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
     */
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);

    /*
      to distinguish the notes, set a minimum time between them.
      the note's duration + 30% seems to work well:
     */
    int pauseBetweenNotes = noteDuration * 1.7;
    delay(pauseBetweenNotes);
    
    noTone(8); //stop the tone playing:
  }
  digitalWrite(8,LOW);

}



// ************************
// Main Loop
// ************************
void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX); // print out the value of the remote button
    if (results.value == upButton) {  // "Up" - check the value (notice the 0x at the beginning
      digitalWrite(greenLed, HIGH);   
      leftMotor->run(FORWARD);
      rightMotor->run(FORWARD);
    }
    else if  (results.value == leftButton) {
      digitalWrite(greenLed, HIGH);
      leftMotor->run(BACKWARD);
      rightMotor->run(FORWARD);
    }
    else if  (results.value == rightButton) {
      digitalWrite(greenLed, HIGH);
      leftMotor->run(FORWARD);
      rightMotor->run(BACKWARD);
    }
    else if  (results.value == downButton) {
      digitalWrite(greenLed, HIGH);
      leftMotor->run(BACKWARD);
      rightMotor->run(BACKWARD);
    }
    else if  (results.value == okButton) {      //stops motor
      digitalWrite(greenLed, HIGH);
      leftMotor->run(RELEASE);
      rightMotor->run(RELEASE);
    }
    else if  (results.value == songButton) {    //plays song
      digitalWrite(greenLed, HIGH);
      digitalWrite(speaker, HIGH);
      

    }

    irrecv.resume(); // Receive the next value

  }


    delay(100);

    // put your main code here, to run repeatedly:
    long distance = ultrasonic.distanceRead(); // CM or INC
    Serial.print("Object distance (in cm's) = ");
    Serial.println(distance);


    if (distance >= 0 && distance <= 10) {
      digitalWrite(greenLed, LOW);            
      digitalWrite(redLed, HIGH);
      
      leftMotor->run(RELEASE);
      rightMotor->run(RELEASE);
      delay(500);                    // stops for 5 ms when ultrasonic detects <= 10 cm
      
      digitalWrite(redLed, LOW);
      digitalWrite(yellowLed, HIGH);
      
      leftMotor->run(BACKWARD);       // robot turns left
      rightMotor->run(FORWARD);
      
      digitalWrite(speaker, HIGH); 
      delay(1000);                    // makes robot turn left for 1000 ms until distance > 10
      
      leftMotor->run(FORWARD);
      rightMotor->run(FORWARD);
      
      
      
      


      // when distance is greater than 0
    }
    else if (distance > 10) {
      digitalWrite(speaker, LOW);
      digitalWrite(redLed, LOW);
      digitalWrite(yellowLed, LOW);
      digitalWrite(greenLed, HIGH);
      
      // do something different if the range is between 5 and 15
    }
  }
else if  (results.value == songButton) {    //plays song
      static bool theSongIsPlaying = false;
      if (theSongIsPlaying)
      {
        stopTheSong();
        theSongIsPlaying = false;
      }
    else
      {
        startTheSong();
        theSongIsPlaying = true;
      }
    }