Apr 112012
 

Laser pointer turret, x y coordinate laser pointer, overgrown cat toy, whatever you want to call it. It has been done before 1001 times over, but I wanted to do it again, for myself. The hardware part of this is extremely simple. I just switched the device from the cat annoyance post from solderless breadboard to perfboard and headers. The X axis servo is on pin 9 and Y axis servo is on pin 10. I broke the Aixiz laser module I had on there, so I replaced it with a cheapo laser pointer from wal-mart.

20120411-232712.jpg

http://www.ragingcomputer.com/2011/11/cat-annoyance

I didn’t care for the code I had written before. It just moved the laser from one point to another random point. The cats seemed more confused by its erratic behavior than amused. I figured circles would be a good idea. I swiped snippets of code and inspiration from the following forum post.

http://arduino.cc/forum/index.php?topic=97984.0

I want it to turn off after 15 minutes, and reset after an hour. To add some variability, the diameter of the circle increases and decreases, the speed is random, the circle point of the circle is random too (all within certain limits).

I’ve included the code below

Advertisement:
#include <Servo.h> 

Servo servox;
Servo servoy;

long i;
unsigned long timer;
unsigned long timer2;

void setup() 
{  
  servox.attach(9);
  servox.write(90);
  servoy.attach(10);
  servoy.write(90);

  pinMode(5, OUTPUT);
  analogWrite(5, 128);

  delay(300);
  timer = millis()+900000;
  timer2 = millis()+3600000;
}

void loop() 
{
 if(millis() > timer){
  digitalWrite(5, LOW);
  servox.write(90);
  servoy.write(90);
  servox.detach();
  servoy.detach();
  if(millis() > timer2){
    timer2 = millis()+3600000;
    timer = millis()+900000;
    servox.attach(9);
    servoy.attach(10);
    servox.write(90);
    servoy.write(90);
    pinMode(5, OUTPUT);
    analogWrite(5, 128);
    delay(300);
  }
 }
 if(millis() < timer){

  int CircleXCenter = 90;
  int CircleYCenter = 90;
  int loopDelay = 30;
  
  randomSeed(analogRead(4));
  CircleXCenter = random(80,100);
  randomSeed(analogRead(5));
  CircleYCenter = random(80,100);
  randomSeed(analogRead(6));
  loopDelay = random(15,30);

  for (int Rad = 5; Rad < 20; Rad++)
  {
    for (int i = 0; i < 100; i++)
    {
      float angle = i*2*3.14/100;
      int xPos = CircleXCenter + (cos(angle) * Rad);
      int yPos = CircleYCenter + (sin(angle) * Rad);
      
      servox.write(xPos);
      servoy.write(yPos);
      delay(loopDelay);
    
    }
  }
  
  randomSeed(analogRead(4));
  CircleXCenter = random(80,100);
  randomSeed(analogRead(5));
  CircleYCenter = random(80,100);
  randomSeed(analogRead(6));
  loopDelay = random(15,30);
  
  for (int Rad = 20; Rad > 5; Rad--)
  {
    for (int i = 100; i > 0; i--)
    {
      float angle = i*2*3.14/100;
      int xPos = CircleXCenter + (cos(angle) * Rad);
      int yPos = CircleYCenter + (sin(angle) * Rad);
      
      servox.write(xPos);
      servoy.write(yPos);
      delay(loopDelay);
    
    }
  }
 }
}

  One Response to “Arduino Laser Turret Cat Toy”

  1. Been looking for something like this for a while. So glad i found it.

    Made a couple of minor changes by adding a slight random factor and a small servo change.

    Modded code below

    #include

    Servo servox;
    Servo servoy;

    long i;
    unsigned long timer;
    unsigned long timer2;

    void setup()
    {
    servox.attach(9);
    servox.write(90);
    servoy.attach(10);
    servoy.write(90);

    pinMode(8, OUTPUT);
    analogWrite(8, 128);

    delay(300);
    timer = millis()+90000000; // restored O.V.
    timer2 = millis()+360000000; // restored O.V.

    }

    void loop()
    {
    float r1 = random (.1,10); // added
    int r2 = random (10,130); // added
    int r3 = random (50,300); // added

    if(millis() > timer){
    digitalWrite(5, LOW);
    servox.write(90);
    servoy.write(90);
    servox.detach();
    servoy.detach();
    if(millis() > timer2){
    timer2 = millis()+3600000;
    timer = millis()+900000;
    servox.attach(9);
    servoy.attach(10);
    servox.write(90);
    servoy.write(90);
    pinMode(8, OUTPUT); // was 5
    analogWrite(8, 128); //was 5
    delay(r3); // was 300900000
    }
    }
    if(millis() < timer){

    int CircleXCenter = 90;
    int CircleYCenter = 90;
    int loopDelay = r3; // was 30

    randomSeed(analogRead(4));
    CircleXCenter = random(80,100);
    randomSeed(analogRead(5));
    CircleYCenter = random(80,100);
    randomSeed(analogRead(6));
    loopDelay = random(r1,r2); // was 15, 30

    for (int Rad = 5; Rad < 30; Rad++)
    {
    for (int i = 0; i 5; Rad–)
    {
    for (int i = 100; i > 0; i–)
    {
    float angle = i*2*3.14/100;
    int xPos = CircleXCenter + (cos(angle) * Rad);
    int yPos = CircleYCenter + (sin(angle) * Rad);

    servox.writeMicroseconds(xPos);
    servoy.writeMicroseconds(yPos);
    delay(loopDelay);

    }
    }
    }
    }

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)