Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Smalls
Published

Randomizing Servos with Arduino

This project will show you how to randomly trigger up to 12 servos using Arduino code.

BeginnerFull instructions provided1 hour1,759
Randomizing Servos with Arduino

Things used in this project

Hardware components

SG90 Micro-servo motor
SG90 Micro-servo motor
I purchased from amazon: https://a.co/d/gydX6PO
×3
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
AlloverPower 3V - 24V 1.5A 36W Universal Adjustable DC Power Supply
Any generic power supply able to generate 5V and connect to a breadboard will work.
×1
Arduino UNO
Arduino UNO
×1
USB-A to B Cable
USB-A to B Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Servo and Uno Circuit Diagram

How to connect the servos, uno, and power supply.

Code

Randomized Multiple Servo Code

Arduino
This code is currently setup to control 3 servos but can easily be adjusted to control as many servos as your board can support.
#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;
  }            
}

Credits

Smalls

Smalls

6 projects • 27 followers
Making stuff, breaking stuff, and learning everything I can. IG & TT: @smallswonderworks
Thanks to Mike Theevil.

Comments