I'm having a problem getting my servo to function within an 'if' statement

Hello,

I’m building a tree topper for the Christmas tree, that I’m hoping will consist of a servo, lights, and a wav file being activated simultaneously by way of an IR remote control. I’m fairly new at this, but I have been teaching myself some basic programming for about a month, so I feel I’ve got basics under my belt. That being said, I’m experiencing an issue with the servo not actuating with prompted to do so by the IR remote.

Here’s the code I’ve put together so far:

#include <SD.h>
#include <Servo.h>
#define SD_ChipSelectPin 10
#include <TMRpcm.h>
#include <IRremote.h>
int IRpin = 2;
int servoPin = 3;
int servoPos = 0;
int light1 = 6;
int light2 = 7;
IRrecv IR(IRpin);
Servo myServo;
decode_results cmd;
TMRpcm tmrpcm;

void setup() {

  tmrpcm.speakerPin = 9;

  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println("SD fail");
    return;
  }
  tmrpcm.setVolume(5);
  IR.enableIRIn();
  myServo.attach(servoPin);
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  myServo.write(servoPos);
  while (IR.decode(&cmd) == 0) {
  }
  //delay(1000);
  IR.resume();

  if (cmd.value == 0xFF6897) {

    servoPos = 0;
    //tmrpcm.play("metstune.wav");   
    digitalWrite(light1, HIGH);
    digitalWrite(light2, HIGH);

    
  }
  if (cmd.value == 0xFF30CF) {

    servoPos = 180;
    //tmrpcm.disable();    
    digitalWrite(light1, LOW);
    digitalWrite(light2, LOW);

    
  }
  myServo.write(servoPos);
}

When I download the code to my Elegoo Uno R3 arduino, the led’s light up, and the .wav file I’ve saved to the SD card plays, but the servo doesn’t actuate. What I’ve noticed is that when I comment out the ‘tmrpcm.play’ and ‘tmrpcm.disable’ commands in the void loop, the servo works fine in response to the IR remote commands.

It seems like there is some kind of incompatibility between the .wav file being played and the servo being actuated, but I’m stumped as to what it might be.

Any help would be greatly appreciated.

Thanks very much

It seems like there is some kind of incompatibility between the .wav file being played and the servo being actuated, but I'm stumped as to what it might be.

It is that they both want to use the same timer / counter.

The simple solution is to use one of these boards to drive the servo:- Adafruit 16-Channel 12-bit PWM/Servo Driver - I2C interface [PCA9685] : ID 815 : $14.95 : Adafruit Industries, Unique & fun DIY electronics and kits

Hi "treetopper",

you should re-edit your post so the code is inside of a code-section

  • make the Arduino-IDE the active window

  • press Ctrl.T for auto-formatting your code

  • point with the mouse-arrow somewhere into the code

  • do a rightclick on the mouse

  • and choose "copy for forum"
    This will copy the whole code into the clipboard
    This will add the code-tags right on top of the first line of code and right below the last line of code

  • make your browser the active window and paste clipboard

As a code-Section it shows up as a smaller window which is browsable on its own
and has a select-function which makes it possible to select the entire code by one mouseclick for other users.

about the issue google is your friend
www.google.de/search?newwindow=1&ei=HFbgX9b-CozaUob_jegG&q=arduino+servo+TMRpcm.h&oq=arduino+servo+TMRpcm.h

best regards Stefan

Thanks very much both of you for your response.

I followed up on the Google suggestion and located and copied the ServoTimer2 library into my program in an attempt to alleviate the timer issue. I then spent a day trying to get it to work, but just now discovered that it accepts a different set of parameters than does the stock Servo library - In other words ServoTimer2 650 = Servo 0, and ServoTimer2 = 2250 = Servo 180 etc. That being said, now that I’ve tried to incorporate the ServoTimer2 library into my project, nothing works. After my first attempt, I noticed I was getting a ‘SD fail’ message in my serial monitor, so I changed the ChipSelect pin from 10 to 3, and my servo pin from 3 to 5. When I compiled again, I was no longer receiving the ‘SD fail’ message, but still nothing is happening when I attempt to initiate the code using the IR remote. Here’s my latest code - any further advice would be greatly appreciated.

#include <SD.h>
#include"ServoTimer2.h"
#define SD_ChipSelectPin 3
#include <TMRpcm.h>
#include <IRremote.h>
int IRpin = 2;
int servoPin = 5;
int light1 = 6;
int light2 = 7;
IRrecv IR(IRpin);
ServoTimer2 myServo;
decode_results cmd;
TMRpcm tmrpcm;

void setup() {

  tmrpcm.speakerPin = 9;

  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println("SD fail");
    return;
  }
  tmrpcm.setVolume(5);
  IR.enableIRIn();
  myServo.attach(servoPin);
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  //myServo.write(servoPos);
  while (IR.decode(&cmd) == 0) {
  }
  //delay(1000);
  IR.resume();

  if (cmd.value == 0xFF6897) {

    myServo.write(2250);
    tmrpcm.play("metstune.wav");   
    digitalWrite(light1, HIGH);
    digitalWrite(light2, HIGH);

    
  }
  if (cmd.value == 0xFF30CF) {

    myServo.write(650);
    tmrpcm.disable();    
    digitalWrite(light1, LOW);
    digitalWrite(light2, LOW);

    
  }
}

Although my code is compiling w/o error, I am getting this warning message after the code has compiled, "C:\Users\adamb\Documents\Arduino\Tree_Topper\Tree_Topper.ino: In function ‘void loop()’:
C:\Users\adamb\Documents\Arduino\Tree_Topper\Tree_Topper.ino:42:31: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
tmrpcm.play(“metstune.wav”);

I’m not sure if this warning is contributing to the issue, but probably worth mentioning.

Thanks,

Adam

any further advice would be greatly appreciated.

Seeing how you ignored the advice you were given the question is why should I bother?

The compiler warnings and errors have information about exact the place in the code where the error occured

C:\Users\Stefan\Documents\Arduino\Tree-hopper-Servo2-001\Tree-hopper-Servo2-001.ino:42:31: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]

tmrpcm.play(“metstune.wav”);

**42:31 means **line 42 colum 31

tmrpcm.play(“metstune.wav”); <== that’s the command that causes the warning

Knowing that it is easy to lookup where the error occured.

So it is always a good idea to read the documentation of a library

the values 650 and 2250 to me seem a liitle low / high usually servos work between 1000 / 2000
some of them 800 / 2100
So one try is to change these values.

Not receiving IR does this mean wav-playing and servo-move is working?

If notthing works what have you done to narrow down the problem.
Didi you try the servo standalone? No WAV n o IR
soes this work?

then adding online wav.file-playing. does this work?

then remove WAV-playing add IR-receiviing? does this work?

etc.

**best regards Stefan **