Build a cat toy from a old IP camera

Cat toy from old IP camera

I wanted to create something with arduino that controls the steppermotors in my old ipcamera. a univeral motor driven unit which I can attach stuff to, fo example a laserpointer or a ligth source.

 

 

The board

The board has two stepper drivers ( ULN2003APG ) which can easily move steppermotors

Prototype motherboard for arduino nano

The Code

In order to test the motors out, this code will generate a random path for it to move around freely on both axis.


#include 
#define motorPin1  10
#define motorPin2  11
#define motorPin3  12
#define motorPin4  13

#define motorPin5  6
#define motorPin6  7
#define motorPin7  8
#define motorPin8  9

#define uTrigger  4
#define dTrigger  5

#define lTrigger  2  
#define rTrigger  3
/*
#define uTrigger  2 
#define dTrigger  3

#define lTrigger  4  
#define rTrigger  5
*/


#define DEBUG

#ifdef DEBUG
 #define DEBUG_PRINT(x)  Serial.print (x)
 #define DEBUG_PRINTLN(x)  Serial.println (x)
#else
 #define DEBUG_PRINT(x)
 #define DEBUG_PRINTLN(x)
#endif

int triggerL = LOW;
int triggerR = LOW;
int PanMaxRight = 0;
int PanMiddlePoint = 0;
int PanMaxLeft = 0;
int initialize_pan_status = 0;
int initialized_pan = 0;

int triggerU = LOW;
int triggerD = LOW;
int TiltMaxUp = 0;
int TiltMiddlePoint = 0;
int TiltMaxDown = 0;

int initialize_tilt_status = 0;
int initialized_tilt = 0;


AccelStepper stepper1(8, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(8, motorPin5, motorPin7, motorPin6, motorPin8);
void setup() {
  stepper1.setMaxSpeed(400);
  stepper1.setAcceleration(400);
  stepper2.setMaxSpeed(400);
  stepper2.setAcceleration(400);
  pinMode(lTrigger, INPUT);
  pinMode(rTrigger, INPUT);
  pinMode(uTrigger, INPUT);
  pinMode(dTrigger, INPUT);
  Serial.begin(115200);
}

void loop() {
  // Read limitation on Pan
  triggerL = digitalRead(lTrigger);
  triggerR = digitalRead(rTrigger);
  triggerU = digitalRead(uTrigger);
  triggerD = digitalRead(dTrigger);
  
  // Reset the intialization if limits reached
  if(triggerR == HIGH | triggerL == HIGH)
    initialized_pan = 0;

  // Reset the intialization if limits reached
  if(triggerU == HIGH | triggerD == HIGH)
    initialized_tilt = 0;

  // Initialize Pan Left & Right if needed.
  initialize_pan();

  if(initialized_pan == 1) initialize_tilt();
  
  //if(initialized_tilt == 1) random_tilt();
  
  // Goto random position once initialization is done.
  if(initialized_pan == 1 && initialized_tilt == 1){ 
    random_pan();
    random_tilt();
  }
 // if(initialized_tilt == 1) random_tilt();
}





/**
 * Initialize the Pan of the Cat Toy.
 */

void initialize_pan(){
  if (initialized_pan == 0){
     if(initialize_pan_status == 0){
        stepper1.setCurrentPosition(0);
        stepper1.moveTo(3000);
        //stepper1.setMaxSpeed(400);
        //stepper1.setAcceleration(400);
        //stepper1.setSpeed(400);
        initialize_pan_status = 1;
     } else if(initialize_pan_status == 1 && triggerR == HIGH) {
          stepper1.stop();
          DEBUG_PRINTLN("pan right triggered, initializing...");
          stepper1.setCurrentPosition(1000);
          PanMaxRight = stepper1.currentPosition()-50;
          DEBUG_PRINT("max pan right set to: "); DEBUG_PRINTLN(PanMaxRight);
          stepper1.moveTo(-3000);
          stepper1.run();
          //stepper1.setMaxSpeed(400);
          //stepper1.setAcceleration(400);
          //stepper1.setSpeed(400);
          delay(3000);
          initialize_pan_status = 2;
     } else if(initialize_pan_status == 2 && triggerL == HIGH) {
          stepper1.stop();
          stepper1.run();
          PanMaxLeft = stepper1.currentPosition()+50;
          DEBUG_PRINT("PanMaxLeft: "); DEBUG_PRINTLN(PanMaxLeft);
          DEBUG_PRINT(" currpos: "); DEBUG_PRINTLN(stepper1.currentPosition());
          DEBUG_PRINT(" PanMaxRight: "); DEBUG_PRINTLN(PanMaxRight);
          PanMiddlePoint = (PanMaxRight+PanMaxLeft)/2;
          DEBUG_PRINT("...Middle position: "); DEBUG_PRINTLN(PanMiddlePoint);
          DEBUG_PRINTLN("DONE! ");
          stepper1.moveTo(PanMiddlePoint);
          stepper1.setMaxSpeed(400);
          //stepper1.setAcceleration(400);
          stepper1.setSpeed(400);
          initialize_pan_status = 3;
          delay(3000);
     } else if(initialize_pan_status == 3 && stepper1.distanceToGo() == 0) {
         initialize_pan_status = 0;
         initialized_pan = 1;
     }
     DEBUG_PRINTLN(stepper1.currentPosition());
     stepper1.run();
  }
}

void initialize_tilt(){
  if (initialized_tilt == 0){
     if(initialize_tilt_status == 0){
        stepper2.setCurrentPosition(0);
        stepper2.moveTo(3000);
        initialize_tilt_status = 1;
     } else if(initialize_tilt_status == 1 && triggerU == HIGH) {
          stepper2.stop();
          DEBUG_PRINTLN("tilt up triggered, initializing...");
          stepper2.setCurrentPosition(1000);
          TiltMaxUp = stepper2.currentPosition()-50;
          DEBUG_PRINT("max pan right set to: "); DEBUG_PRINTLN(TiltMaxUp);
          stepper2.moveTo(-3000);
          stepper2.run();
          delay(3000);
          initialize_tilt_status = 2;
     } else if(initialize_tilt_status == 2 && triggerD == HIGH) {
          stepper2.stop();
          stepper2.run();
          TiltMaxDown = stepper2.currentPosition()+50;
          DEBUG_PRINT("TiltMaxUp: "); DEBUG_PRINTLN(TiltMaxUp);
          DEBUG_PRINT(" currpos: "); DEBUG_PRINTLN(stepper2.currentPosition());
          DEBUG_PRINT(" TiltMaxDown: "); DEBUG_PRINTLN(TiltMaxDown);
          TiltMiddlePoint = (TiltMaxUp+TiltMaxDown)/2;
          DEBUG_PRINT("...Middle position: "); DEBUG_PRINTLN(TiltMiddlePoint);
          DEBUG_PRINTLN("DONE! ");
          stepper2.moveTo(TiltMiddlePoint);
          stepper2.setMaxSpeed(400);
          //stepper1.setAcceleration(400);
          stepper2.setSpeed(400);
          initialize_tilt_status = 3;
          delay(3000);
     } else if(initialize_tilt_status == 3 && stepper2.distanceToGo() == 0) {
         initialize_tilt_status = 0;
         initialized_tilt = 1;
     }
     DEBUG_PRINTLN(stepper2.currentPosition());
     stepper2.run();
  }
}

/**
 * Find a new random position for the Pan Axis.
 */

void random_pan(){
  initialize_pan_status = 0;
  if (stepper1.distanceToGo() == 0){
    DEBUG_PRINT("pan random between "); DEBUG_PRINT(PanMaxLeft);
    DEBUG_PRINT(" and "); DEBUG_PRINT(PanMaxRight);
    int randomNr = random(PanMaxLeft, PanMaxRight);
    DEBUG_PRINT(" became "); DEBUG_PRINTLN(randomNr);
    stepper1.moveTo(randomNr);
  }
  stepper1.run();
}

void random_tilt(){
  initialize_tilt_status = 0;
  if (stepper2.distanceToGo() == 0){
    DEBUG_PRINT("tilt random between "); DEBUG_PRINT(TiltMaxDown);
    DEBUG_PRINT(" and "); DEBUG_PRINT(TiltMaxUp);
    int randomNr = random(TiltMaxDown, TiltMaxUp);
    DEBUG_PRINT(" became "); DEBUG_PRINTLN(randomNr);
    stepper2.moveTo(randomNr);
  }
  stepper2.run();
}



 

Using it with lightsource in the dark

 

Using it with laserpointer

Shuffeling some pasta.

 

 

Cheers :)

Posted on: Wednesday 07 January 2015