i'm not really a bumblebee
cookies
romillyhills.co.uk uses cookies! cookies are used for the login system and removing this bubble.
usb%20laser%20turret.jpg
clock17:52 on 03.08.11 eye961 speech bubble0
today i woke up and decided to build a mouse controlled usb laser turret. then i did. in this post is a true story about a guy who built a laser turret. with cardboard...

ok, some context, the whole thing isn't build with cardboard, just the bit which holds the laser.

to build your very own mouse controlled laser turret you will need:

an arduino uno,

2 servos,

a laser,

various wires, cardboard and servo fittings,

as well as python 2.7, pyserial and pygame for the software.

i'm not going to detail the physical construction of the turret, i literally bolted one servo onto the other then nailed a piece of folded cardboard to it to hold the laser. the laser and servos all require +5v power supply so they're connected to the +5v pin of the arduino via the breadboard. the laser is connected to digital pin 12.

the servos require analog inputs; the rotation servo is connected to analog pin a0 and the up/down servo is in a1.

and that's the hardware set up!

for whatever reason people don't like python 3.x, so there aren't a lot of packages or forum support for it. for this project i *needed* pygame, which will only work on python 2.x, so that's what i'm switching too.

pygame [obviously] provides all the useful functions for making games with python, like mouse input and events, these are the features i need for real-time control of stuff. pygame makes controlling this turret effortless, here is the code:

import serial
from time import sleep

import pygame
from pygame.locals import *

print 'starting usb...'
s = serial.Serial('COM3', 9600,timeout=1) #start the serial port
sleep(2) #required while the arduino does it's thing

print 'ready'

pygame.init()
#opens a pygame window
pygame.display.set_mode((1000,1000))
#set window to 1000x1000px
pygame.display.set_caption('mouse input')
running = True

while 1:
x,y = pygame.mouse.get_rel() #gets how much the mouse has moved


#sends commands to move up/down/left/right
if x>=1:
s.write('d')
elif x<=-1:
s.write('a')

if y>=1:
s.write('s')
elif y<=-1:
s.write('w')


for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
s.write('q')
#fires laser when the mouse is clicked
elif event.type == MOUSEBUTTONUP:
s.write('e')
#stops fireing when released


and the corresponding arduino code:

#include

Servo servo1;//rotate servo
Servo servo2;//up/down servo

int moo,servo1position,servo2position,rawr;

void setup() {
pinMode(12, OUTPUT); //laser

servo1.attach(14); //analog pin 0
servo2.attach(15); //analog pin 2
Serial.begin(9600);

servo1position=90;
servo2position=90;
rawr=1;//sensitivity
}
void loop(){
if (Serial.available() > 0) {
moo = Serial.read();//read serial data
}

if(moo=='q'){
digitalWrite(12, HIGH);//turn on laser
}else if(moo=='e'){
digitalWrite(12, LOW);//turn off laser
}
//change servo position based on input
if(moo=='w'){
moo=0;
servo2position=servo2position+rawr;
}else if(moo=='s'){
moo=0;
servo2position=servo2position-rawr;
}else if(moo=='a'){
moo=0;
servo1position=servo1position+rawr;
}else if(moo=='d'){
moo=0;
servo1position=servo1position-rawr;
}
//prevent the servos over-rotating
if(servo1position>=180){
servo1position=180;
}else if (servo1position<=0){
servo1position=0;
}else if (servo2position>=180){
servo2position=180;
}else if (servo2position<=0){
servo2position=0;
}
//set the position of the servos
servo1.write(servo1position);
servo2.write(servo2position);
}


which when run should look something like this:



the pygame inputs only work while the cursor is in the pygame window, so i made it nice and large.

this system is still very limited, when i refine it more i'll make sure the cursor starts in the center and it would be a good idea to make the height/width of the window correspond to the 180 degrees of servo motion [the system now just moves the servo when the mouse moves].

however a simple but dumb mouse controlled usb laser turret. great success!
what happened to my arms!?