Devices & Components
Arduino Nano
jumper wires for arduino
SG90 Micro Servo
3D Printer
KY-008 Laser Transmitter Module
Protoboards
Hardware & Tools
3D printer filament (PLA)
Software & Tools
Arduino IDE
Project description
Code
SERVOS_X_Y
cpp
Main file to make the servos move as required
1#include <Servo.h> 2 3#define servo_pin_x 9 4#define servo_pin_y 3 5#define LASER_PIN 5 6 7Servo myServox; 8Servo myServoy; 9 10int min_deg_x= 45; 11int max_deg_x= 135; 12int min_deg_y= 35; 13int max_deg_y= 80; 14int min_x=-100; 15int max_x= 100; 16int min_y=-100; 17int max_y= 100; 18 19 20int position_x = 90; 21int position_y = 90; 22 23int target_x = 0; 24int target_y = 0; 25 26 27 28int angle_rate = 15; 29int time_angle_rate = 150; 30 31struct coord { 32 int x; 33 int y; 34}; 35 36//Here is where you can input the coordinates you want, manually or using AI. 37coord shape [] = { 38 39{50, 0}, {49, 6}, {48, 12}, {46, 18}, {43, 24}, {40, 29}, {36, 34}, {31, 38}, {26, 42}, {21, 45}, 40{15, 47}, {9, 49}, {3, 49}, {-3, 49}, {-9, 49}, {-15, 47}, {-21, 45}, {-26, 42}, {-31, 38}, {-36, 34}, 41{-40, 29}, {-43, 24}, {-46, 18}, {-48, 12}, {-49, 6}, {-50, 0}, {-49, -6}, {-48, -12}, {-46, -18}, {-43, -24}, 42{-40, -29}, {-36, -34}, {-31, -38}, {-26, -42}, {-21, -45}, {-15, -47}, {-9, -49}, {-3, -49}, {3, -49}, {9, -49}, 43{15, -47}, {21, -45}, {26, -42}, {31, -38}, {36, -34}, {40, -29}, {43, -24}, {46, -18}, {48, -12}, {49, -6}, 44{25, 0}, {24, 3}, {24, 6}, {23, 9}, {21, 12}, {20, 14}, {18, 17}, {15, 19}, {13, 21}, {10, 22}, 45{7, 23}, {4, 24}, {1, 24}, {-1, 24}, {-4, 24}, {-7, 23}, {-10, 22}, {-13, 21}, {-15, 19}, {-18, 17}, 46{-20, 14}, {-21, 12}, {-23, 9}, {-24, 6}, {-24, 3}, {-25, 0}, {-24, -3}, {-24, -6}, {-23, -9}, {-21, -12}, 47{-20, -14}, {-18, -17}, {-15, -19}, {-13, -21}, {-10, -22}, {-7, -23}, {-4, -24}, {-1, -24}, {1, -24}, 48{4, -24}, {7, -23}, {10, -22}, {13, -21}, {15, -19}, {18, -17}, {20, -14}, {21, -12}, {23, -9}, {24, -6}, {24, -3}, 49{-50, 0}, {-40, 0}, {-40, 10}, {-30, 10}, {-30, 20}, {-20, 20}, {-20, 30}, {-10, 30}, {-10, 40}, {0, 40}, 50{0, 50}, {10, 50}, {10, 60}, {20, 60}, {20, 70}, {30, 70}, {30, 80}, {40, 80}, {40, 90}, {50, 90}, 51{50, 100}, {60, 100} 52}; 53 54 55 56void setup() { 57 58 Serial.begin(9600); 59 myServox.attach(servo_pin_x); 60 myServox.write(position_x); 61 62 myServoy.attach(servo_pin_y); 63 myServoy.write(position_y); 64 delay(500); 65 pinMode(LASER_PIN, OUTPUT); 66 digitalWrite(LASER_PIN, HIGH); 67} 68 69void loop() { 70 71 int shape_point_qty = sizeof(shape)/sizeof(shape[0]); 72 for (int i = 0; i < shape_point_qty ; i++){ 73 target_x = map(shape[i].x,min_x,max_x,min_deg_x,max_deg_x); 74 target_y = map(shape[i].y,min_y,max_y,min_deg_y,max_deg_y); 75 76 move_servos(); 77 78 } 79 80} 81 82 83void move_servos(){ 84 85 bool flag_x = true; 86 bool flag_y = true; 87 88 while (flag_x || flag_y){ 89 // 90 91 if(position_x == target_x){ 92 flag_x = false; 93 } 94 95 if(position_y == target_y){ 96 flag_y = false; 97 } 98 99 if (position_x < target_x) { 100 101 position_x += angle_rate; 102 if (position_x > target_x){ 103 position_x = target_x; 104 flag_x=false; 105 } 106 107 108 } 109 110 if (position_y < target_y){ 111 112 position_y += angle_rate; 113 if (position_y > target_y){ 114 position_y = target_y; 115 flag_y=false; 116 } 117 118 } 119 120 if (position_y > target_y){ 121 122 position_y -= angle_rate; 123 if (position_y < target_y){ 124 position_y = target_y; 125 flag_y=false; 126 } 127 128 } 129 130 131 132 if (position_x > target_x){ 133 134 position_x -= angle_rate; 135 if (position_x < target_x){ 136 position_x = target_x; 137 flag_x=false; 138 } 139 140 } 141 142 myServox.write(position_x); 143 myServoy.write(position_y); 144 delay (time_angle_rate); 145 146 } 147 148 149}
Calibrate servos
cpp
Instructions on the link at description
1#include <Servo.h> 2 3#define servo_pin_x 9 4#define servo_pin_y 3 5 6Servo myServox; 7Servo myServoy; 8 9int speed = 50; 10 11void setup() { 12 13 myServox.attach(servo_pin_x); 14 myServox.write(0); 15 myServoy.attach(servo_pin_y); 16 myServoy.write(0); 17 delay(3000); 18} 19 20 21void loop() { 22 23 for(int i=0; i<=90; i++){ 24 myServox.write(i); 25 myServoy.write(i); 26 delay(speed); 27 } 28 29 while (true){} 30 31}
Downloadable files
Breadboard box support-A
to be 3D printed and assemblied to protoboard
Breadboard box support-A.stl
Breadboard box support-B
to be 3D printed and assemblied to protoboard
Breadboard box support-B.stl
PAN N TILT X AXIS - A
FOR HORIZONTAL MOVEMENT
PAN N TILT X-Axis-A.stl
PAN N TILT X AXIS - B
SAME AS PAN N TILT X AXIS - A
PAN N TILT X-AXIS-B.stl
PAN N TILT Y AXIS - A
FOR VERTICAL MOVEMENT
PAN N TILT Y-AXIS-A.stl
PAN N TILT Y AXIS - B
SAME AS PAN N TILT Y AXIS - A
PAN N TILT Y-AXIS-B.stl
Comments
Only logged in users can leave comments