Note: This project assumes you have Arduino IDE setup and know how to connect and write to an Arduino Uno board.

Hook Up Your Servos

The first step is to hook up your servos to your UNO. If you purchased the same servos I used from Amazon the wires are as follows:

The Uno has issues powering servos consistently at 5V and you'll be hooking up multiple servos to 5V, so I recommend using an 5V power supply.

The signal wires will connect to Uno Pins 9, 10, and 11 and the servo power and ground will connect to the power supply. ** Make sure you connect your power supply's ground to your Uno's ground**


Write Sketch and Upload the Code

Plug your Uno into your computer and open a new sketch in Arduino IDE. Copy and paste the code from below and upload to your Uno:

#include <servo.h>

//original code transcribed from Mike Theevil's video: https://www.youtube.com/watch?v=1doy5nNS-cg
//modified by Smalls on Nov 8 2023

Servo tail; //create servo object to control a servo
Servo head; //create servo object to control a servo
Servo eyes; //create servo object to control a servo
// twelve servo objects can be created on most boards

void loop_tail() {
 int t1 = random(0, 170); //defines variable and sets servo step amount between 0 and 170 degrees
 int t2 = random(500, 800); //defines variable and sets random delay 
 int t3 = random(400, 1000); //defines variable and sets neutral random delay     

 tail.write(t1); //makes servo position random
 delay(t2); //triggers random relay
 tail.write(0); //resets servo to neutral position
 delay(t3); //triggers neutral random delay           
}

void loop_head() {
 int h1 = random(0, 170); //defines variable and sets servo step amount between 0 and 170 degrees
 int h2 = random(500, 800); //defines variable and sets random delay 
 int h3 = random(400, 1000); //defines variable and sets neutral random delay     

 head.write(h1); //makes servo position random
 delay(h2); //triggers random relay
 head.write(0); //resets servo to neutral position
 delay(h3); //triggers neutral random delay           
}

void loop_eyes() {
 int e1 = random(0, 170); //defines variable and sets servo step amount between 0 and 170 degrees
 int e2 = random(500, 800); //defines variable and sets random delay 
 int e3 = random(400, 1000); //defines variable and sets neutral random delay     

 eyes.write(e1); //makes servo position random
 delay(e2); //triggers random relay
 eyes.write(0); //resets servo to neutral position
 delay(e3); //triggers neutral random delay           
}

void setup() {
  tail.attach(9); //attaches the servo on pin 9 to the servo object
  head.attach(10); //attaches the servo on pin 9 to the servo object
  eyes.attach(11); //attaches the servo on pin 9 to the servo object
}

void loop() {
  byte randNum = random(3); //max range of numbers
  switch (randNum) //switches between the random numbers assigned in this loop
  {
    case 0:
      loop_tail();
      break;
    case 1:
      loop_head();
      break;
    case 2:
      loop_eyes();
      break;
  }            
}

Understanding the Code

  • Servo tail; //create servo object to control a servo Create one of these for each servo you have, you can replace "tail" with a unique name of your choosing
  • void loop_tail() create a loop for each servo, make sure to name it the same thing as your servo so you don't forget which loop controls which servo
  • int t1 = random(0, 170); defines variable and sets servo step amount between 0 and 170 degrees
  • int t2 = random(500, 800); defines variable and sets random delay in microseconds
  • int t3 = random(400, 1000); defines variable and sets neutral random delay in microseconds
  • tail.write(t1); sets the servo to a random position according to the range defined in int t1
  • delay(t2); pauses movement for a random amount of time according to the range defined in int 2
  • tail.write(0); resets the servo to its neutral starting position
  • delay(t3); pauses movement for a random amount of time according to the range defined in int 3
  • void setup() is run one to...
Read more »