/* * IRrecvDemo_Vmusic2 - demonstrates receiving IR codes with IRremote, * and translating these into starting specific MP3 files on the VMUSIC2 * * 1] An IR detector/demodulator (e.g. TSOP1738) must be connected to the input RECV_PIN. * 2] A SONY remote control is expected * 3] The LED signals if an IR signal is received * IRreceive code based on IRrecvDemo by Ken Shirriff * * Version 1, 5 jan 2011 * Copyright 2010 Gidi van Liempd (gidi@geedesign.com) */ #include #define DELAY 500 // Since the Sony IR remote usually sends the same code a few times, we build in a delay // in case the IR code is the same #define CR 13 // $0D in hex, int RECV_PIN = 12; // conncected to the IR receiver (TSOP1738) via a 220 Ohm resistor int LED_PIN = 13; // connected to a 220 Ohm resistor, which is connected to a LED which connects to ground int RELAIS_PIN = 8; #define RELAIS_DELAY 10000 // time the relay will be held unsigned long lastrelaytime; // last time relay was activated boolean bRelayActiv; IRrecv irrecv(RECV_PIN); decode_results IRresults; unsigned long lastbutton; // last button pressed on the remote unsigned long lasttime; // last time a button was pressed unsigned long presenttime; boolean bNewButton; // did we see a "new" button? char* mySounds[]={"V_start.mp3", "V_een.mp3", "V_twee.mp3", "V_drie.mp3"}; void setup() { // initialize VMUSIC2 Serial.begin(9600); Serial.print("IPA"); // monitor commands use ASCII values Serial.print(13,BYTE); // carriage return delay(200); Serial.print("ECS"); // use the extended command set Serial.print(13,BYTE); // carriage return delay(2000); // wait two seconds irrecv.enableIRIn(); // Start the receiver pinMode(LED_PIN, OUTPUT); pinMode(RELAIS_PIN, OUTPUT); digitalWrite(RELAIS_PIN, LOW); // sets the relay off } void loop() { unsigned long curtime; if (irrecv.decode(&IRresults)) { // if a IR code is recognized // Do something with the results if (IRresults.value == lastbutton) { // the button is the same as the last one recognized presenttime = millis(); if ((presenttime - lasttime) > DELAY) { // If the interval was long enough bNewButton = true; lasttime = presenttime; // remember this time } else { // not long enough ago we received the same button! bNewButton = false; } } else { // the button decoded is a different one bNewButton = true; lastbutton = IRresults.value; lasttime = millis(); // remember this time }; if (bNewButton) { digitalWrite(LED_PIN, HIGH); switch (IRresults.value) { case 0x290: //Button "mute" on Sony remote control case 0x538: //Button "STOP" on UNIVERSAL remote control case 0x690: //Button "NORMAL" on UNIVERSAL remote control SwitchOffRelay(); StopSoundFile(); break; case 0xA90: //Button "Power" on Sony remote control SwitchOnRelay(); PlaySoundFile(mySounds[0]); break; case 0x10: //Button "1" on Sony remote control RepeatSoundFile(mySounds[1]); break; case 0x810: //Button "2" on Sony remote control RepeatSoundFile(mySounds[2]); break; case 0x410: //Button "3" on Sony remote control RepeatSoundFile(mySounds[3]); break; case 0xFD0: //Button "PAUSE" on UNIVERSAL remote control PauseSoundFile(); break; }; }; irrecv.resume(); // Receive the next value }; // switch off LED if on too long curtime = millis(); if ((curtime - lasttime) > DELAY) { digitalWrite(LED_PIN, LOW); } // switch off relay if active too long if (bRelayActiv) { if ((curtime - lastrelaytime) > RELAIS_DELAY) { SwitchOffRelay(); } } } /* Start a specific MP3 file on the VMUSIC2 */ void PlaySoundFile(char* AFile) { // stop what you were playing before - must be done explicitly if VRF is used!!! // N.B. I noticed the explicit "stop" command seems only to delay if ONLY VPF is used Serial.print("VST"); Serial.print(13,BYTE); // play the selected file Serial.print("VPF "); Serial.print(AFile); Serial.print(CR,BYTE); } /* Start a specific MP3 file on the VMUSIC2 and repeat */ void RepeatSoundFile(char* AFile) { // stop what you were playing before - must be done explicitly if VRF is used!!! Serial.print("VST"); Serial.print(13,BYTE); // repeatedly play the selected file Serial.print("VRF "); Serial.print(AFile); Serial.print(CR,BYTE); } /* Pause the MP3 file playing */ void PauseSoundFile() { Serial.print("VP"); Serial.print(CR,BYTE); } /* Stop the MP3 file playing */ void StopSoundFile() { Serial.print("VST"); Serial.print(CR,BYTE); } /* Switch on the relay */ void SwitchOnRelay() { digitalWrite(RELAIS_PIN, HIGH); // activate the relay lastrelaytime = millis(); // remember this time bRelayActiv = true; } /* Switch off the relay */ void SwitchOffRelay() { digitalWrite(RELAIS_PIN, LOW); // switch off the relay bRelayActiv = false; }