Bob
Published © GPL3+

Control Mini-Pan-Tilt Camera Mount with Arduino Pro Mini

Test the mini-pan-tilt camera mount quickly with the 5 volt, 16MHz Arduino Pro Mini.

BeginnerProtip1 hour872
Control Mini-Pan-Tilt Camera Mount with Arduino Pro Mini

Things used in this project

Hardware components

Mini Pan Tilt Camera Mount with Servos from Ximimark
×1
Arduino Pro Mini 328 - 5V/16MHz
SparkFun Arduino Pro Mini 328 - 5V/16MHz
×1
Breadboard (generic)
Breadboard (generic)
×1
Male/Male Jumper Wires
×1
SparkFun break away header pins, long
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Pan Tilt test using arduino pro mini

Use the Arduino 5 volt 16 Mhz to test the mini-pan-tilt camera mount

Code

interactive_servo.ino

C/C++
Use this program to control a servo interactively using the Arduino IDE Serial Monitor. This program only controls one servo.
/*****************************************************************************
* Program Name: interactiveServo
* Author: Bob
* 
* Move a servo interactively with the Arduino IDE Serial Monitor
* 
* based upon  BARRAGAN <http://barraganstudio.com>
* and Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Sweep
* 
* ----------------------------------------------------------------------------
* Change History
* Date         Ver    Description
* ----------   ----   --------------------------------------------------------
* 02/25/2019   V1.0   Initial development
*****************************************************************************/
#include <Servo.h>

int    servoPIN = 12; //servo signal pin
int    servoPos = 90; //initial servo position
String inString = ""; //string to hold incomming command
Servo  servo;         //create servo object to control the servo



/*********************************************************************
 *  setup()
 *  Called once at power on
 ********************************************************************/
void setup() {
  servo.attach(servoPIN); // attaches the servo pin to the servo object
  servo.write(servoPos);  // set servo to initial position
  
  Serial.begin(115200);
  Serial.println("");
  Serial.println("-----------------------------------------------------------------------");
  Serial.println("- Welcome to Interactive Servo");
  Serial.println("- Open the serial monitor and ensure the Line Ending option (at bottom");
  Serial.println("- of the monitor window) is set to NewLine. To move the servo, enter a ");
  Serial.println("- number from 0 to 180(at top of monitor window)and press ENTER/SEND");
  Serial.println("-----------------------------------------------------------------------");
  Serial.print("Servo at "); Serial.print(servoPos); Serial.println(".\tNew position?");
}



/*********************************************************************
 *  loop()
 *  Main processing loop. Called after setup()
 ********************************************************************/
void loop() {
  
  /*process incomming characters (command) from serial monitor*/
  while(Serial.available()) {
    char inChar = Serial.read();
    if(inChar == '\n')  {
      int num = inString.toInt();
      inString = "";

      /*if valid number move servo*/
      if((num >= 0) && (num <= 180)) {
        for(int i = servoPos; i != num; i += (servoPos < num? 1 : -1)) {
          servo.write(i); delay(15); //move servo +/- 1 degree every 15 msec
        }
        servoPos = num; //update servo position
        Serial.print("Servo at "); Serial.print(servoPos); Serial.println(".\tNew position?");
      }
      else { Serial.print(num); Serial.println(" not valid.  Must be between 0 and 180"); }
    }

    /*add char to command string, if string is not too long and valid digit*/
    else {
      if(inString.length() > 2) continue;
      if(isDigit(inChar)) inString += inChar;
    }
  }
}

miniPanTiltTest.ino

C/C++
Use this program to cycle the pan and tilt servos of the mini pan tilt camera mount through a range of motions
/*****************************************************************************
* Program Name: miniPanTileTest
* Author: Bob
* 
* Cycle pan and tilt through a range of motions
* 
* based upon  BARRAGAN <http://barraganstudio.com>
* and Scott Fitzgerald http://www.arduino.cc/en/Tutorial/Sweep
* 
* ----------------------------------------------------------------------------
* Change History
* Date         Ver    Description
* ----------   ----   --------------------------------------------------------
* 02/25/2019   V1.0   Initial development
*****************************************************************************/
#include <Servo.h>
Servo pan;    // create servo object to control the yaw servo
Servo tilt;  // create servo object to control the pitch servo



/*********************************************************************
 *  setup()
 *  Called once at power on
 ********************************************************************/
void setup() {
  Serial.begin(115200);
  Serial.println("");
  pan.attach(12);   // attaches the servo on pin 12 to the servo object
  tilt.attach(13);  // attaches the servo on pin 13 to the servo object
}



/*********************************************************************
 *  loop()
 *  Main processing loop. Called after setup()
 ********************************************************************/
void loop() {

  /*Check left/right and center movement*/
  Serial.println("Pan Control");
  for(int i=90;  i<180; i++) { pan.write(i); delay(25); }
  for(int i=180; i>0;   i--) { pan.write(i); delay(25); }
  for(int i=0;   i<90;  i++) { pan.write(i); delay(25); }
  
  /*Check up/down and center movement*/
  Serial.println("Tilt Control");
  for(int i=90;  i<145; i++) { tilt.write(i); delay(25); }
  for(int i=145; i>10;   i--) { tilt.write(i); delay(25); }
  for(int i=10;   i<90;  i++) { tilt.write(i); delay(25); }


  /*Move both servos at the same time(ish)*/
  int pmin=10;  //right
  int pmax=170; //left
  int ppos=90;  //current position
  int pinc=1;   //movement increment in degrees

  int tmin=10;  //top
  int tmax=145; //bot
  int tpos=90;  //current position
  int tinc=3;   //movement increment in degrees

  /*go through cycle twice */
  Serial.println("Dual Control");
  for(int i=0; i<(180*2); i++) {

    ppos += pinc;                           //increment to next position
    if(ppos > pmax) {ppos=pmax; pinc = -1;} //check for limit, change direction if necessary
    if(ppos < pmin) {ppos=pmin; pinc = 1;}  //check for limit, change direction if necessary
  
    tpos += tinc;                           //increment to next position
    if(tpos > tmax) {tpos=tmax; tinc = -3;} //check for limit, change direction if necessary
    if(tpos < tmin) {tpos=tmin; tinc = 3;}  //check for limit, change direction if necessary
  
    pan.write(ppos);  //move servo
    delay(20);
    tilt.write(tpos); //move other servo
    delay(20);        //some delay
  }
  
  /*set pan/tilt to center */
  for(int i=ppos;  i!=90; i+=(ppos < 90 ? 1 : -1)) { pan.write(i); delay(25); }
  for(int i=tpos;  i!=90; i+=(tpos < 90 ? 1 : -1)) { tilt.write(i); delay(25); }
  delay(10000); //wait 10 seconds and do again
  
}

Credits

Bob

Bob

4 projects • 1 follower
Hobbiest

Comments