Mod edit:
The earlier topic the OP is referring to is this one:
Hi everyone:
My earlier post got kind of out of hand, and branched out into too many paths (sorry). I am posting a new post which specifically addresses my main issue. The earlier post is obsolete.
This project is basically a skeleton that (when an IR Remote is pressed) turns his head, says an MP3 file, and turns his head back. The current version of my sketch is attached. It works perfectly (no complier errors); when button 1 is pressed, the servo turns, and the MP3 plays, and the servo turns back. This happens every time I try it (which is good), until all of a sudden, when button 1 is pressed, the servo turns, and the MP3 only plays and buzzes for a second. I thought I had this fixed, but it still fails once in a while. It will work perfectly maybe a dozen times, and then stop. When it fails, the IR receiver still lights, and the blue light on the MP3 player lights, but the MP3 player just gives a one second buzz and stops. The only way to fix this is to re-load the sketch. The wiring has not changed, the code has not changed. and the board was not moved.
I am using:
Arduino Uno R3
DFRobot DFPlayer Mini
Infrared IR Wireless Remote Control Module HX1838 with a 38 KHZ infrared receiving module
Hitec HS425BB servo
Here is the wiring:
and here is the code:
#include <SoftwareSerial.h> //Allows us to assign different pins for serial use
#include <DFRobotDFPlayerMini.h>
#include <IRremote.h>
#include <Servo.h>
//THIS WORKS - EACH BUTTON PRESS PLAYS TRACK FOR THAT BUTTON
int rxPin = 3;
int txPin = 2; //Sets up the send/receive from the Mp3 player
int track = 0001; //This is the track number on the micro SD card
SoftwareSerial fxSerial(rxPin, txPin); //calls the Mp3 player fxSerial
DFRobotDFPlayerMini fxPlayer;
int remoteStatus = 0; //This sets the trigger to start the talking as zero (no talking, just shoveling).
int remoteState = 0; //This is the label of the current remote status (on or off)
int IRPin = 11; //for the ir remote
Servo HeadTurnServo;
int servoHeadPin = 12; //This is the servo that rotates the head. Does this need to be a squiggly pin? NO it does not
void setup() {
// put your setup code here, to run once:
HeadTurnServo.attach(servoHeadPin);
HeadTurnServo.write(90); //Zeroes the head to straight ahead.
pinMode(IRPin, INPUT); //sets the pin from the IR Remote to input
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT); // Tells Arduino what the Mp3 pins are doing
fxSerial.begin(9600); //Sets up the serial function for the Mp3 player
fxPlayer.begin(fxSerial); //this tells Arduino that the serial path for the Mp3 player is fxSerial (the name of the MP3 player)
Serial.begin(9600); // Do I need this?
fxPlayer.volume(20); // Volume can be 10 to 30). Set this to 20 to use less power
Serial.println("Enabling IRin"); //for the IR remote
IrReceiver.begin(IRPin, ENABLE_LED_FEEDBACK); //for the IR remote
Serial.println("Enabled IRin"); //for the IR remote
delay(1000); //Gives things a chance to stabilize
}
void loop() {
// put your main code here, to run repeatedly:
if (IrReceiver.decode()) { //TRUE if an IR button was pressed, FALSE if not //for the IR remote
Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX); /*prints the decoded raw IR data to the serial monitor in hexadecimal format. The decodedRawData field holds the raw, unprocessed IR signal. */
if (IrReceiver.decodedIRData.decodedRawData == 0xBA45FF00) { //add the next rows to try other buttons
Serial.println("Button 1 was pressed");
HeadTurnServo.write(170); //turns the head to the right
fxPlayer.play(1); // plays message 1 because button 1 was pressed
delay(10000); //gives the head a short pause before turning back.
HeadTurnServo.write(90); //turns the head back to straight ahead
} //end of IF statement
//
if (IrReceiver.decode()) { //TRUE if an IR button was pressed, FALSE if not //for the IR remote
if (IrReceiver.decodedIRData.decodedRawData == 0xB946FF00) { //add the next rows to try other buttons
Serial.println("Button 2 was pressed");
//fxplayer.play(2)// plays message 2 because button 2 was pressed
} //end of IF statement
}
//fxPlayer.play(2);// Plays track 2
//delay(10000); //this plays track for 10 seconds. The speech must be less than 10 seconds long -Does this need to be in the loop?
IrReceiver.resume(); /*prepares the IR receiver to start listening for the next IR signal. It essentially tells the IR receiver to reset its internal state and prepare for a new incoming signal.*/
}
} //End of void loop
These facts may be irrelevant but I include in case they are important.
When the code uploads, the speaker buzzes for a brief spurt.
The servo is constantly whirring/clicking.
Thanks.