Sketches, Servos and wiring

I’m new to remote control, and I’ve been having problems with this setup (my first).

here’s the code/sketch:

#include <IRLibAll.h>
    #include <Servo.h> 
    #define MY_PROTOCOL NEC
    #define RIGHT_ARROW   0xffaa5 //Move 135 clockwise
    #define LEFT_ARROW    0xff10ef //Move 45 counterclockwise
    #define SELECT_BUTTON 0xff38c7 //Center the servo at 90
    #define UP_ARROW      0xff18e7 //Move 90 counterclockwise
    #define DOWN_ARROW    0xff4ab5 //Move 90 cockwise
    IRrecv myReceiver(2); //pin number for the receiver
    IRdecode myDecoder;
     
    Servo myServo;  // create servo object to control a servo 
    
int16_t pos;         // variable to store the servo position
uint32_t Previous;//handles NEC repeat codes

void setup() {
  // put your setup code here, to run once:
myServo.attach(9);      // attaches the servo on pin 9 to the servo object 
      pos = 90;               // start at midpoint 90 degrees
      myServo.write(pos);     // Set initial position
      myReceiver.enableIRIn(); // Start the receiver
}

void loop() {
  // put your main code here, to run repeatedly:
  if (myReceiver.getResults()) {
           myDecoder.decode();
           if(myDecoder.protocolNum==MY_PROTOCOL) {
             if(myDecoder.value==0xFFFFFFFF)
               myDecoder.value=Previous;
               switch(myDecoder.value) {
                case LEFT_ARROW:    pos=45; break;
                case RIGHT_ARROW:   pos=135; break;
                case SELECT_BUTTON: pos=90; break;
                case UP_ARROW:      pos=0; break;
                case DOWN_ARROW:    pos=180; break;

}
  myServo.write(pos); // tell servo to go to position in variable 'pos' 
             Previous=myDecoder.value;
           }
           myReceiver.enableIRIn();
        }
}

This shoudl be simple, but???
The above code verifies and compiles, and it uploads to the UNO board without any errors.
I’m just trying to get the servo arm to turn right and left and center at 90 degrees. Turns, 45 degrees lefte and right and a shrp turn left and right at 90 degrees.

See Pics

I tried hooking up a 9 volt battery (see ServoPower pic) and a 4 AA battery pack (to power the servo. I read that that might be better at supplying power (this needs to be a standalone unit).
When powered up the servo moves a few degrees, bu thats it.
I hooked up the receiver in the same manner as the pic, except the signal is connected to pin 11.
for the servo set up, I treid both pin 13 and pin 9 (same results)
I also had tried this without the two battery connections (i.e.USB)
So as it stands now, The servo arm moves a little upon hooking up the power, then there is a slight hum or buzz, but no movement. Pointing the transmitter at the receiver and pushing a button causes the light to go on - on the receiver - but no movement.
The transmitter/receiver seems to work. I got the hex codes from that (with the serial monitor), and in that case pushing a button did give me the hex codes.
So I suspect it’s maybe the code in the sketch, but ???
Any ideas as to why this doesn’t work???

Sounds like a power problem if the servo only moves a little bit. Did you miss a ground?

Please provide a drawing (photo/scan of hand-drawn one is fine) how everything is connected (including power and ground) that matches the code that you provided.

Run it on USB and put a few Serial.prints in to see what codes, if any, you are really receiving and what actual values you are writing to the servo.

Steve

It is possible that the IR library and the Servo library are both trying to use Hardware Timer1. Maybe try the ServoTimer2 library.

...R

Thanks for the suggestions; I'll try them over the weekend, and post again.

           if(myDecoder.protocolNum==MY_PROTOCOL) {
             if(myDecoder.value==0xFFFFFFFF)
               myDecoder.value=Previous;
               switch(myDecoder.value) {
                case LEFT_ARROW:    pos=45; break;
                case RIGHT_ARROW:   pos=135; break;
                case SELECT_BUTTON: pos=90; break;
                case UP_ARROW:      pos=0; break;

Indentation level doesn't mean squat to the compiler. You should ALWAYS use curly braces after an if statement, so that the code to be executed if the condition is true is clear.

You should always indent properly regardless of whether the compiler cares, or not. People care, and you are asking people for help with your code.