Servo Control Issues

Hello!

Having an issue with this program that is trying to control a servo and stepper. The stepper works great.

(also to note that the hex codes for the IR are correct for all)

The issue is with the servo: specifically the second the program loads onto the board the servo tries to turn one way hard, can’t find out why.

Note: I can control the servo fine with a program I setup to manually do it from serial monitor inputs, so i know it’s correctly connected and fully functional

#include "Stepper.h"
#include "IRremote.h"
#include <Servo.h>      // Load servo library
/*----- Variables, Pins -----*/
int pos = ;               // Variable to store the servo position
int servoPin = 5;
#define STEPS  32   // Number of steps per revolution of Internal shaft
int  Steps2Take;  // 2048 = 1 Revolution
int receiver = 6; // Signal Pin of IR receiver to Arduino Digital Pin 6
/*-----( Declare objects )-----*/
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, 8, 10, 9, 11);
IRrecv irrecv(receiver);    // create instance of 'irrecv'
decode_results results;     // create instance of 'decode_results'
Servo myPointer;        // Defines the name of our servo

void setup()
{
  myPointer.attach(servoPin);  // Attaches our defined servo "myPointer" by variable "servoPin"(pin 9)
  irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    switch (results.value)
    {
      case 0x3D9AE3F7: // UP/2 button pressed
        small_stepper.setSpeed(500); //Max seems to be 700
        Steps2Take  =  2048;  // Rotate CW
        small_stepper.step(Steps2Take);
        delay(20);
        break;
      case 0x1BC0157B: // DOWN/8 button pressed
        small_stepper.setSpeed(500);
        Steps2Take  =  -2048;  // Rotate CCW
        small_stepper.step(Steps2Take);
        delay(20);
        break;


      // Servo control
      case 0xFF5AA5: // RIGHT/6 button pressed
        myPointer.write((pos - 30));
        delay(20);
        break;
      case 0x8C22657B: // LEFT/4 button pressed
        myPointer.write((pos + 30));
        delay(20);
        break;


    }
    irrecv.resume(); // receive the next value
  }
}/* --end main loop -- */

Try adding myPointer.write(90); immediately after myPointer.attach(servoPin);

I have not heard of the Stepper library interfering with the Servo library but it is possible. If you think that i the problem you might try the ServoTimer2 library

It would also be good to put in some Serial.println() statements so you could see what is happening and when. For example does the servo misbehave BEFORE the code gets to myPointer.attach(), or afterwards.

I presume you are not powering the servo from the Arduino? Have you the servo GND connected to the Arduino GND?

...R

Try adding myPointer.write(90); immediately after myPointer.attach(servoPin);

Actually, you should issue the write() before attach(). That way, the servo won't start wandering off in some random direction before it gets the write() to tell it where to go.

#include "Stepper.h"

Are you using a non-standard stepper library then? Why not the system library?

The issue is with the servo: specifically the second the program loads onto the board the servo tries to turn one way hard, can't find out why.

Servos are fairly simple devices. Out of control turning in one direction are usually due to improper grounding, or malformed/out of bound control pulses. If the servo works ok with a servo test program, then I'd suspect some type of timer conflict in the code or libraries.