Components and supplies
Laser Diode
Sonoff smart socket (optional)
Jumper wires (M-M and F-M)
Arduino Nano R3
Breadbord
Servo motor 9g SG90
Tools and machines
Super Glue
Multitool, Screwdriver
Project description
Code
CatLaserToy.ino
c_cpp
1/* 2 Laser Tower for the CAT - LA FABRIQUE DIY And modded (changed 3 LED to PWM pin to make it less bright) 4 Pseudo-randomly moves a servo tower 5 (on X and Y axis) and lights up a laser. 6 x_servo is attached to pin 6 and moves 7 in the X plan 8 y_servo is attached to pin 9 and moves in the Y plan 9 Laser 10 is on pin 3 11 HOW IT WORKS : 12 The program randomly choose a new position 13 for the laser inside a square you can define below. 14 It checks the new position 15 is different from the old one of at least "minimal_movement". 16 It moves the 17 tower to the new position and stays still for a time between min_freeze and max_freeze 18 19 (this aims to reproduce the behaviour of an insect landing somewhere for a 20 bit and then flying off, 21 that's the variable you need to increase if your 22 cat is fat). 23 Ans starts the process over and over again. 24 25 Created 26 30 Sep 2016 by Lucas Berbesson 27*/ 28 29 30#include <Servo.h> 31 32/* YOU 33 CAN CUSTOM THESE VARIABLES IF YOU WANT TO ALTER THE TOWER BEHAVIOUR */ 34 35// 36 X servo angle will stay in [min_x, max_x] range 37// Y servo angle will stay in 38 [min_y, max_y] range 39// to be ajsuted to the size of your living room 40 41float 42 min_x = 5; 43float max_x = 50; 44float min_y = 5; 45float max_y = 35; 46int 47 min_freeze = 600; 48int max_freeze = 3000; 49float minimal_movement = 5; 50int 51 LaserValue = 180; // 0 =0V and 255 =5V lower the value if you want to dim the laser, 52 higher it if you want to brighten it, higher the value to max 255 53/* YOU SHOULD 54 NOT HAVE TO MODIFY THE CODE BELOW THIS LINE */ 55 56// finding center of square 57 for starting point 58int random_delay; 59float x_position = min_x + (max_x - min_x)/2; 60float 61 y_position = min_y + (max_y - min_y)/2; 62float x_old_position = x_position; 63float 64 y_old_position = y_position; 65float x_new_position; 66float y_new_position; 67float 68 x_speed; 69float y_speed; 70int movement_time; 71 72// Instantiating two servos 73Servo 74 x_servo; 75Servo y_servo; 76int pos = 0; 77 78void setup() { 79 y_servo.attach(6); 80 // attaches the y servo on pin 6 to the servo object 81 x_servo.attach(9); // 82 attaches the x servo on pin 9 to the servo object 83 84 pinMode (3, OUTPUT); 85 86 analogWrite(3,LaserValue); // switch on the laser 87 88 //Place the servos 89 in the center at the beginning 90 y_servo.write(y_position); 91 x_servo.write(x_position); 92 93 94} 95 96void loop() { 97 movement_time = random(10,40); 98 random_delay 99 = random(min_freeze, max_freeze); 100 x_new_position = random(min_x+minimal_movement, 101 max_x-minimal_movement); 102 y_new_position = random(min_y+minimal_movement, max_y-minimal_movement); 103 104 105 if( (y_new_position > y_old_position) && (abs(y_new_position - y_old_position) 106 < 5 )) { 107 y_new_position = y_new_position + minimal_movement; 108 } else 109 if ( (y_new_position < y_old_position) && (abs(y_new_position - y_old_position) 110 < 5 )) { 111 y_new_position = y_new_position - minimal_movement; 112 } 113 114 115 if( (x_new_position > x_old_position) && (abs(x_new_position - x_old_position) 116 < 5 )) { 117 x_new_position = x_new_position + minimal_movement; 118 } else 119 if ( (x_new_position < x_old_position) && (abs(x_new_position - x_old_position) 120 < 5 )) { 121 x_new_position = x_new_position - minimal_movement; 122 } 123 124 125 x_speed = (x_new_position - x_old_position)/movement_time; 126 y_speed = (y_new_position 127 - y_old_position)/movement_time; 128 for (pos = 0; pos < movement_time; pos += 129 1) { 130 x_position = x_position + x_speed; 131 y_position = y_position 132 + y_speed; 133 x_servo.write(x_position); 134 y_servo.write(y_position); 135 136 delay(10); 137 } 138 x_old_position = x_new_position; 139 140 y_old_position = y_new_position; 141 delay(random_delay); 142 143} 144
CatLaserToy.ino
c_cpp
1/* 2 Laser Tower for the CAT - LA FABRIQUE DIY And modded (changed LED to PWM pin to make it less bright) 3 Pseudo-randomly moves a servo tower (on X and Y axis) and lights up a laser. 4 x_servo is attached to pin 6 and moves in the X plan 5 y_servo is attached to pin 9 and moves in the Y plan 6 Laser is on pin 3 7 HOW IT WORKS : 8 The program randomly choose a new position for the laser inside a square you can define below. 9 It checks the new position is different from the old one of at least "minimal_movement". 10 It moves the tower to the new position and stays still for a time between min_freeze and max_freeze 11 (this aims to reproduce the behaviour of an insect landing somewhere for a bit and then flying off, 12 that's the variable you need to increase if your cat is fat). 13 Ans starts the process over and over again. 14 15 Created 30 Sep 2016 by Lucas Berbesson 16*/ 17 18 19#include <Servo.h> 20 21/* YOU CAN CUSTOM THESE VARIABLES IF YOU WANT TO ALTER THE TOWER BEHAVIOUR */ 22 23// X servo angle will stay in [min_x, max_x] range 24// Y servo angle will stay in [min_y, max_y] range 25// to be ajsuted to the size of your living room 26 27float min_x = 5; 28float max_x = 50; 29float min_y = 5; 30float max_y = 35; 31int min_freeze = 600; 32int max_freeze = 3000; 33float minimal_movement = 5; 34int 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 35/* YOU SHOULD NOT HAVE TO MODIFY THE CODE BELOW THIS LINE */ 36 37// finding center of square for starting point 38int random_delay; 39float x_position = min_x + (max_x - min_x)/2; 40float y_position = min_y + (max_y - min_y)/2; 41float x_old_position = x_position; 42float y_old_position = y_position; 43float x_new_position; 44float y_new_position; 45float x_speed; 46float y_speed; 47int movement_time; 48 49// Instantiating two servos 50Servo x_servo; 51Servo y_servo; 52int pos = 0; 53 54void setup() { 55 y_servo.attach(6); // attaches the y servo on pin 6 to the servo object 56 x_servo.attach(9); // attaches the x servo on pin 9 to the servo object 57 58 pinMode (3, OUTPUT); 59 analogWrite(3,LaserValue); // switch on the laser 60 61 //Place the servos in the center at the beginning 62 y_servo.write(y_position); 63 x_servo.write(x_position); 64 65} 66 67void loop() { 68 movement_time = random(10,40); 69 random_delay = random(min_freeze, max_freeze); 70 x_new_position = random(min_x+minimal_movement, max_x-minimal_movement); 71 y_new_position = random(min_y+minimal_movement, max_y-minimal_movement); 72 73 if( (y_new_position > y_old_position) && (abs(y_new_position - y_old_position) < 5 )) { 74 y_new_position = y_new_position + minimal_movement; 75 } else if ( (y_new_position < y_old_position) && (abs(y_new_position - y_old_position) < 5 )) { 76 y_new_position = y_new_position - minimal_movement; 77 } 78 79 if( (x_new_position > x_old_position) && (abs(x_new_position - x_old_position) < 5 )) { 80 x_new_position = x_new_position + minimal_movement; 81 } else if ( (x_new_position < x_old_position) && (abs(x_new_position - x_old_position) < 5 )) { 82 x_new_position = x_new_position - minimal_movement; 83 } 84 85 x_speed = (x_new_position - x_old_position)/movement_time; 86 y_speed = (y_new_position - y_old_position)/movement_time; 87 for (pos = 0; pos < movement_time; pos += 1) { 88 x_position = x_position + x_speed; 89 y_position = y_position + y_speed; 90 x_servo.write(x_position); 91 y_servo.write(y_position); 92 delay(10); 93 } 94 x_old_position = x_new_position; 95 y_old_position = y_new_position; 96 delay(random_delay); 97 98} 99
Downloadable files
Cat laser Toy schematics
Cat laser Toy schematics
Comments
Only logged in users can leave comments
Seafox_C
0 Followers
•0 Projects
Table of contents
Intro
6
0