Play sounds and light up LED with IR remote, how do I loop things?

Okay, so I am quite new to programming Arduino so I would love some help or pointers to places where I can learn how to solve my problem. Teach a man to fish and what not :slight_smile:

My project is, I believe, quite simple. I have an LED and an Audio chip that can play multiple sounds. I have an IR sensor so I can use a remote to trigger things.

At this point I can turn the LED on and off and I can play either specific sounds or random sounds with the buttons on the remote.

I have two issues at this point:

  • If I turn the LED on and then trigger a sound, the LED turns off until the sound file has finished, after which the LED turns back on again. This is not a huge deal, but it would be nice if the LED stays on until I specifically tell it to turn off.

  • Bigger issue is looping random audio. I can play sounds no problem, but I want to have a button that makes it play random sounds at a random time interval. Doing just that without the remote is easy, the void loop just repeats the [play sound] [wait this many seconds] commands, but that obviously doesn't happen in the Switch..case I use to trigger things now.

My current code looks like this:

#include <IRremote.h>
       #include "SoftwareSerial.h"
       #include "MP3FLASH16P.h"
       MP3FLASH16P myPlayer;

//IR Remote button codes
#define audio_one  41565 
#define audio_two  25245 
#define audio_three  57885 
#define audio_four  8925 
#define audio_five  765
#define audio_six  49725
#define audio_seven  57375
#define audio_eight  43095
#define audio_nine  36975
#define audio_ten  26775
#define audio_eleven  39015
#define audio_twelve  45135
#define audio_random  23205
#define audio_random_loop  6375
#define holo_key  4335

int receiver_pin = 8;   
                        //Variables
                        int autoplay = 0;

int LED_pin = 6;

int led[] = {0}; 
IRrecv receiver(receiver_pin); 
decode_results output;

void setup()
{
  Serial.begin(9600);
  receiver.enableIRIn();  
  pinMode(holo_pin, OUTPUT);

//initialize MP3 chip
myPlayer.init(3);
}

void loop(){

if (receiver.decode(&output)) {
    unsigned int value = output.value;
    switch(value) {
      
//play specific sound
       case audio_one:    
                        myPlayer.playFileAndWait(1, 10);
          break; 
      
       case audio_two:    
                        myPlayer.playFileAndWait(2, 10);
          break; 
                
       case audio_three:    
                        myPlayer.playFileAndWait(3, 10);
          break; 
                
       case audio_four:    
                        myPlayer.playFileAndWait(4, 10);
       
//turn LED on or off
       case holo_key:    
                        if(led[1] == 1) {
                            digitalWrite(holo_pin, LOW);
                            led[1] = 0;
                      } else {
                            digitalWrite(holo_pin, HIGH);
                            led[1] = 1;
                      }
          break; 
//play random sound
          case audio_random:    
                        myPlayer.playFileAndWait(random(1, 9),10);
          break; 
//play random sound in loop with delay 
          case audio_random_loop:    
                        myPlayer.playFileAndWait(random(1, 9),10);
                        delay (random(1200, 3000));    
          break; 
    }
    Serial.println(value); 
    receiver.resume(); 
  }
}

I am sure there is an easier or better way to do what I'm trying to accomplish here. I would love some solutions or pointers to information to help me figure this out.

Couple of issues here:

      case audio_three:
        myPlayer.playFileAndWait(3, 10);
        break;

      case audio_four:
        myPlayer.playFileAndWait(4, 10);

      //turn LED on or off
      case holo_key:
        if (led[1] == 1) {
          digitalWrite(holo_pin, LOW);
          led[1] = 0;
        } else {
          digitalWrite(holo_pin, HIGH);
          led[1] = 1;
        }
        break;

The audio_three case has a break after it. audio_four doesn't so it falls through to execute the holo_key case too.

The led array has one element (why?) but it is led[0], not led[1].

wildbill:
Couple of issues here:

      case audio_three:

myPlayer.playFileAndWait(3, 10)

The audio_three case has a break after it. audio_four doesn't so it falls through to execute the holo_key case too.

The led array has one element (why?) but it is led[0], not led[1].
[/quote][quote author=wildbill link=msg=3678183 date=1522939676]
Couple of issues here:

The audio_three case has a break after it. audio_four doesn't so it falls through to execute the holo_key case too.

The led array has one element (why?) but it is led[0], not led[1].
[/quote]

Oh sorry, the missing break is a copy paste error. There are actually 12 sounds in there, I figured a few was more than enough for you guys to get the gist, but I removed a line to many :slight_smile:

The LED array is something I just moved over from my test with just the LED, went through learning how to turn it on and off through a tutorial, it worked, so figured it was fine. But that [1] should be a [0]?

It would be simpler, IMO, to put all the IR codes that correspond to tracks in an array like this:

// ir codes for tracks
unsigned int trackCodes[] = {41565, 25245, 57885};

// record # of tracks
int numTrackCodes = sizeof(trackCodes) / sizeof(trackCodes[0]);

Then your switch statement could be turned upside down to handle the "special" keys first and if one of those has not been clicked look at the codes for each track. If one of those keys has been clicked use the IR code index in the array to play the proper track.

void loop() {

  if (receiver.decode(&output)) {
    unsigned int value = output.value;
    switch (value) {

      //turn LED on or off
      case holo_key:
        if (led[1] == 1) {
          digitalWrite(holo_pin, LOW);
          led[1] = 0;
        } else {
          digitalWrite(holo_pin, HIGH);
          led[1] = 1;
        }
        break;

      //play random sound
      case audio_random:
        myPlayer.playFileAndWait(random(1, 9), 10);
        break;

      //play random sound in loop with delay
      case audio_random_loop:
        myPlayer.playFileAndWait(random(1, 9), 10);
        delay (random(1200, 3000));
        break;

      default:

        int idx;

        // find index based on ir code
        for (idx = 0; idx < numTrackCodes; idx++) if ( trackCodes[idx] == value ) break;

        //play specific sound
        if ( idx < numTrackCodes ) myPlayer.playFileAndWait(idx+1, 10);

        break;
    }
    Serial.println(value);
    receiver.resume();
  }
}

That does make a lot of sense. Thanks, I'll give this approach a try :slight_smile: