Hi Terrypin: Thanks for the reply. I am not sure why the code didn’t work at you rend. Using the code I posted above, it works fine here. Here is a screen shot of the serial monitor:
It works repeatedly and doesn’t fail. Curious; I guess I won’t question the magic, and I’ll take the “win” with the code.
You asked me to describe this 'whirring' in more detail. I understand your need to know more, but I struggle with how to describe it. As soon as the sketch is uploaded to the Arduino Uno, the servo makes a rhythmic (repeating) sound, that I would best describe as a series of faint clicks. It sounds (to me) like a timer you plug into the wall to turn a lamp on and off; the timer makes a rhythmic clicking sound.
The servo stops making the sound if I disconnect its signal pin from the Arduino.
Having said that, I may have a solution. I was researching the replies from forum members, and tried something that seems to work. I used “Detach” when setting up the servo, and “Attach” just before I needed it to turn. I found I need to add a command of “delay(300);” between the “Attach” command, and the command to move the servo. I assume this is to give the servo time to attach. This is the set of commands I use:
HeadTurnServo.attach(servoHeadPin); //turns servo back on
delay(300);
HeadTurnServo.write(170); //turns the head to the right
HeadTurnServo.detach(); //turns off servo to stop its jittering
Here is the whole sketch, if you want to see it, but is basically what I have been using, with the “Detach” and “Attach” lines added in a few places.
#include <SoftwareSerial.h> //Allows us to assign different pins for serial use
#include <DFRobotDFPlayerMini.h>
#include <IRremote.h>
#include <Servo.h>
//This sketch is an upgraded sketch that adds the IR Remote triggering the MP3 player
//It tries some ideas form DeBaros video, substituting the IR remote for the PIR
//THIS WORKS - EACH BUTTON PRESS PLAYS TRACK FOR THAT BUTTON AND TEH DETACH STOPSS THE JITTERING
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.
HeadTurnServo.detach(); //turns off servo to stop its jittering
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() {
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.attach(servoHeadPin); //turns servo back on
delay(300);
HeadTurnServo.write(170); //turns the head to the right
HeadTurnServo.detach(); //turns off servo to stop its jittering
if (fxPlayer.available()) {
//printDetail(fxPlayer.readType(), fxPlayer.read()); //Print the detail message from DFPlayer to handle different errors and states.
}
fxPlayer.play(1); // plays message 1 because button 1 was pressed
delay(10000);
HeadTurnServo.attach(servoHeadPin); //turns servo back on //gives the head a short pause before turning back.
delay(300);
HeadTurnServo.write(90);
HeadTurnServo.detach(); //turns off servo to stop its jittering //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
My limited understanding of this method is that by using the “Detach” method, the primary problem is that the servo will stop receiving the control signal and will likely lose its position. It won't maintain its last set position unless it's continuously powered and receiving the control signal. I have not found this to be a problem in my project. The servo will only be used every now and then, and it seems to hold it’s position fine when detached. I could see this as more of an issue in a project where the servo is called often and automatically by something like a motion sensor.
I think the root cause for the servo’s issues is that the Arduino Servo and SoftwareSerial libraries can sometimes conflict due to their reliance on timers and interrupts. I understand this conflict can lead to erratic servo behavior. I believe the servo library uses Timer 1 and the SoftwareSerial library uses Timer 2, but they still don’t play well with each other.
One Forum member said: “SoftwareSerial turns off interrupts while each character is sent. Usually this just causes some jitter in the Servos because the pulse length get lengthened when the timer interrupt is delayed”.
I am sorry, but this is getting beyond my knowledge here. All I know at this point is that using the “Detach /Attach” method seems to work, and is not hurting anything , so I think I will go with it.