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
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.