Introduction: Automatic Cat Laser

I made a cat laser that moves by itself so that my new cat (Walter) can play with it while I'm working.

Supplies

Step 1: All the Parts

Make sure you have all your parts! Lay them out and check with your power source that your Arduino Nano lights up!

Step 2: Get Your 3D Print Going

Your 3D print will take about 5-7 hours to complete. Be sure this is the first thing you start to do so that when you are finished connecting all the wires you can put your final project together.

Step 3: Wiring the Laser Diode

You need a yellow and green female-female wire. The green wire should be on the left side of the diode IF the laser light is facing you (see drawing); the connection of the wire is shown in the first photo. It must be on this exact one. If your laser is malfunctioning this is the first thing I would check.

The yellow wire will connect to the right side if the laser light is facing you. The wire will connect to the D3 on your Arduino Nano as show in the picture.

Step 4: Wiring the Lower Servo and the Upper Servo

The wiring for each servo cannot be switched. View the drawing to see were each wire will connect noting that the upper servo and lower servo are different.

Note that there are 3 red wires. The male ends will not have a connection yet but do it exactly as shown. In the picture.

Step 5: The 3 Red Wires

Take your 3 red wired and put them together. At this point you can plug in your servo to your power source and see if it lights up (if you have already input the code, the servos should start to move). Connect the 3 red wires together with your wire clamp, clamp tightly, then cover with a heat shrink and heat it up to shrink it tight over your 3 wires and clamp.

Step 6: The Code

One the Arduino IDE is downloaded Input the following code

/*

 Laser Tower for the CAT - LA FABRIQUE DIY And modded (changed

 LED to PWM pin to make it less bright)

 Pseudo-randomly moves a servo tower

 (on X and Y axis) and lights up a laser.

 x_servo is attached to pin 6 and moves

 in the X plan 

 y_servo is attached to pin 9 and moves in the Y plan 

 Laser

 is on pin 3

 HOW IT WORKS : 

 The program randomly choose a new position

 for the laser inside a square you can define below. 

 It checks the new position

 is different from the old one of at least "minimal_movement".

 It moves the

 tower to the new position and stays still for a time between min_freeze and max_freeze

 (this aims to reproduce the behaviour of an insect landing somewhere for a

 bit and then flying off, 

 that's the variable you need to increase if your

 cat is fat).

 Ans starts the process over and over again. 

 Created

 30 Sep 2016 by Lucas Berbesson

*/



#include <Servo.h>


/* YOU

 CAN CUSTOM THESE VARIABLES IF YOU WANT TO ALTER THE TOWER BEHAVIOUR */


//

 X servo angle will stay in [min_x, max_x] range

// Y servo angle will stay in

 [min_y, max_y] range

// to be ajsuted to the size of your living room


float

 min_x = 5;

float max_x = 50;

float min_y = 5;

float max_y = 35;

int

 min_freeze = 600;

int max_freeze = 3000;

float minimal_movement = 5;

int

 LaserValue = 180; // 0 =0V and 255 =5V lower the value if you want to dim the laser,

 higher it if you want to brighten it, higher the value to max 255

/* YOU SHOULD

 NOT HAVE TO MODIFY THE CODE BELOW THIS LINE */


// finding center of square

 for starting point

int random_delay;

float x_position = min_x + (max_x - min_x)/2;

float

 y_position = min_y + (max_y - min_y)/2; 

float x_old_position = x_position;

float

 y_old_position = y_position;

float x_new_position;

float y_new_position;

float

 x_speed;

float y_speed;

int movement_time;


// Instantiating two servos

Servo

 x_servo;  

Servo y_servo;

int pos = 0;


void setup() {

 y_servo.attach(6);

 // attaches the y servo on pin 6 to the servo object

 x_servo.attach(9); //

 attaches the x servo on pin 9 to the servo object

 pinMode (3, OUTPUT);


 analogWrite(3,LaserValue); // switch on the laser

 //Place the servos

 in the center at the beginning 

 y_servo.write(y_position); 

 x_servo.write(x_position);


}


void loop() {

 movement_time = random(10,40);

 random_delay

 = random(min_freeze, max_freeze);

 x_new_position = random(min_x+minimal_movement,

 max_x-minimal_movement);

 y_new_position = random(min_y+minimal_movement, max_y-minimal_movement);


 if( (y_new_position > y_old_position) && (abs(y_new_position - y_old_position)

 < 5 )) {

  y_new_position = y_new_position + minimal_movement;

 } else

 if ( (y_new_position < y_old_position) && (abs(y_new_position - y_old_position)

 < 5 )) {

  y_new_position = y_new_position - minimal_movement;

 }


 if( (x_new_position > x_old_position) && (abs(x_new_position - x_old_position)

 < 5 )) {

  x_new_position = x_new_position + minimal_movement;

 } else

 if ( (x_new_position < x_old_position) && (abs(x_new_position - x_old_position)

 < 5 )) {

  x_new_position = x_new_position - minimal_movement;

 }


 x_speed = (x_new_position - x_old_position)/movement_time;

 y_speed = (y_new_position

 - y_old_position)/movement_time;  

 for (pos = 0; pos < movement_time; pos +=

 1) { 

   x_position = x_position + x_speed;

   y_position = y_position

 + y_speed;

   x_servo.write(x_position);  

   y_servo.write(y_position);

  delay(10); 

 }

 x_old_position = x_new_position;


 y_old_position = y_new_position;

 delay(random_delay);


}


Step 7: Verify and Upload

Connect your arduino nano to your computer USB after you have verified your code. Upload it and then your servos and Arduino should start to move around and the light on the laser should turn on. Make sure all this happens otherwise check your wiring.

Step 8: ChatGPT and Coding

If you want to change the code, type the same code previously listed in chatgpt and request what changes you would like to make.

For example: Make this code so the laser doesn't stop and so that it moves in a smaller area.

ChatGPT

Step 9: Assemble Your Laser

  1. With your green and yellow wires disconnected from the Arduino Nano (but still connected to the laser) thread them through the laser holder/casing. Use a hot glue gun to glue the laser inside the casing with the yellow and green wires sticking out the back. If you mix up the wires after gluing you will not be able to undo this step so make sure you have the green on the left and the yellow on the right.
  2. Hot glue a servo wing on the left side. Make sure it is facing the right was so to can connect to your servo

Step 10: Upper Servo Assembly

Take the upper servo (remember you can verify its the upper servo by checking to see if the orange wire is connected to D6) and place it in its stand. It might be a little wide so you can file down the sides of the servo and/or the 3D print. Use hot glue to keep its placement sturdy.

Step 11: Put Them Together

Put steps 9 and 10 together. The servo should fit nicely in the wing. Don't glue this down as you will want to make adjustments to the rotation if necessary.

Step 12: Lower Servo

Place the lower servo in the 3D print. This will not need to be glued but will definitely need to be filed down a bit to fit inside this hole.

Step 13: The Box

Thread the wires through as shown in the picture above, this is to hide your wires

Step 14: Organize

As best you can organize the wires without squishing them too much. I used some twist ties but this isn't necessary. By sure you place your arduino nano with the power source input facing the large hole for easy access.

Step 15:

Place the 2 parts together! They should fit nicely, don't force. If your lower servo slides out you might need to consider using hot glue to keep it in place.

Step 16: Shut the Box and You're Done!

With the box closed, you can plug it into a power source and everything should start to move around for your sweet fluffy friends entertainment! Once you are certain you are finished you can use some of the small screws that come with the servo to screw everything in place.