Built-in project · Arduino Uno R3

Cat laser toy on an Arduino Uno

A KY-008 laser on a pan and tilt pair of servos wanders its dot around the floor for a minute, then rests until the button is pressed. The whole build, an Arduino Uno and 4 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

The parts retail for about $36. On Mokxi they're free.

Cat laser toy · Arduino Uno R3live0.000 s 0.00x
Press Run and watch the servos; press the button for another round
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Cat laser toy: a KY-008 laser on a pan and tilt pair of servos.
//
//   pin 7    the laser's S (the laser runs from the pin itself)
//   pin 9    the pan servo's signal
//   pin 10   the tilt servo's signal
//   pin 2    the button, to GND (INPUT_PULLUP: pressed reads LOW)
//   rails    5 V and ground for the servos and the laser
//
// A round is a minute of the dot wandering around the floor: pick a spot,
// glide to it a degree at a time, pause, pick another. Then the laser goes
// off and the servos rest until the button asks for another round.
//
// The tilt never goes above 60 degrees from straight down, so the dot stays
// on the floor and out of anyone's eyes.

#include <Servo.h>

const int LASER = 7;
const int PAN = 9;
const int TILT = 10;
const int BUTTON = 2;

const unsigned long ROUND_MS = 60000;
const int PAN_MIN = 30, PAN_MAX = 150;
const int TILT_MIN = 100, TILT_MAX = 160;

Servo pan;
Servo tilt;
int panAt = 90;
int tiltAt = 130;
int rounds = 0;

void glideTo(int panTo, int tiltTo) {
  while (panAt != panTo || tiltAt != tiltTo) {
    if (panAt < panTo) panAt++;
    if (panAt > panTo) panAt--;
    if (tiltAt < tiltTo) tiltAt++;
    if (tiltAt > tiltTo) tiltAt--;
    pan.write(panAt);
    tilt.write(tiltAt);
    delay(15);
  }
}

void play() {
  rounds++;
  Serial.print("round ");
  Serial.print(rounds);
  Serial.println(": laser on");
  digitalWrite(LASER, HIGH);
  unsigned long start = millis();
  while (millis() - start < ROUND_MS) {
    int p = random(PAN_MIN, PAN_MAX + 1);
    int t = random(TILT_MIN, TILT_MAX + 1);
    Serial.print("dot to ");
    Serial.print(p);
    Serial.print(", ");
    Serial.println(t);
    glideTo(p, t);
    delay(random(200, 900));
  }
  digitalWrite(LASER, LOW);
  glideTo(90, 130);
  Serial.println("resting: press the button to play again");
}

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: cat laser toy");
  pinMode(LASER, OUTPUT);
  pinMode(BUTTON, INPUT_PULLUP);
  pan.attach(PAN);
  tilt.attach(TILT);
  pan.write(panAt);
  tilt.write(tiltAt);
  randomSeed(analogRead(A0));
  play();
}

void loop() {
  if (digitalRead(BUTTON) == LOW) play();
  delay(20);
}

Parts list

6 parts, plus the jumper wires. They retail for about $36; on Mokxi they're free.

How it is wired

6 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 10; Hobby servo (2) pin PWM
  • Arduino Uno R3 pin 9; Hobby servo (1) pin PWM
  • Arduino Uno R3 pin 7; Laser module, KY-008 pin S
  • Arduino Uno R3 pin 2; Pushbutton pin 1a
  • Arduino Uno R3 pin 5V; Hobby servo (1) pin VCC; Hobby servo (2) pin VCC
  • Ground: Arduino Uno R3 pin GND; Laser module, KY-008 pin GND; Hobby servo (1) pin GND; Hobby servo (2) pin GND; Pushbutton pin 2a

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.