Hi! I am currently working on a project that requires a servo and a DF player to both be operated with an IR remote. Based on stuff I found on the web, I got a good running code for getting the servo to work, but whenever I add in the appropriate codes for the DF player I get no luck. Below is the current code I am using. ANY help would be greatly appreciated! Thanks!
include <IRremote.h>
include <Servo.h>
Servo myservo;
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() { myservo.attach(9); Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver }
void loop() { if (irrecv.decode(&results)) { String s = String(results.value, HEX); if(s != "ffffffff"){ Serial.println(s); }
if(s == "ff22dd"){
myservo.write(2370); // set servo counter clockwise
}
else if(s == "ffc23d"){
myservo.write(570); // set servo clockwise
}
else if(s == "ff02fd"){
myservo.write(1420); // set servo to mid-point
}
//Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
} }
Hi Matthew,
Does this code compile? The following line has a problem:
void setup() { myservo.attach(9); Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver }
The comment "// Start the receiver" will also comment-out the closing brace }
Pat.
Yeah it compiled just fine. I went and double checked-in the code it is a separate line, when I copied and pasted here it got deleted. The servo works via the remote, now it's just getting sound to work.
Hi Matthew,
What exactly are you trying to do with the sound?
Are there separate IR codes to control the sounds or are they initiated with the servo movements?
Pat.
The functions are not related. At the moment, when I click the arrows on the remote the servo moves back and forth perfectly. I would like to program it so that when I click one of the various number buttons on the remote it plays a sound.
I am using it for a full scale model of Wall-E. The servo moves the head and the sounds are the various noises he makes. Again, thanks for your help!
Hi Matthew,
What??? You didn't try to make a BB-8? I guess Wall-E is a better choice, though. Loved that movie.
Start by adding additional:
else if(s == "xxxxx"){
...
}
lines for each of the appropriate buttons. In the { ... }, add the commands to stop playing the existing file, if playing, and then start the one you want.
Are you familiar with how to use the DF Player library ?
Pat.
Also... do you have the appropriate libraries installed?
This and this?
Hi Matthew,
The code you're looking for is something like this:
#include <DFPlayer_Mini_Mp3.h>
...
if(s == "xxxxxx") {
mp3_play (1); // play file "0001.mp3" in the folder "mp3"
}
if(s == "xxxxxx") {
mp3_play (2); // play file "0002.mp3" in the folder "mp3"
}
if(s == "xxxxxx") {
mp3_play (3); // play file "0003.mp3" in the folder "mp3"
}