This project covers how to make make multiple servos both trigger randomly as well as move to angles and delay randomly within a set range.
Notes: This project assumes you have Arduino IDE setup and know how to connect and write to an Arduino Uno board.
Hook Up Your ServosThe 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**
Plug your Uno into your computer and open a new sketch in Arduino IDE. Copy and paste the code from the code section of this project.
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 setup your board and pins
- tail.attach(9); attaches the servo on pin 9, each servo would get a line of code attaching it to its own pin
- void loop() this is where you'll list all the servo loops you created to be triggered at random
- byte randNum = random(3); creates a max range of random numbers that can be assigned
- switch (randNum) switches between the cases listed
- case 0: the number assigned to the loop which will be called upon with the switch command, if you add another loop to be controlled it would be case 1, followed by case 2 and so on
- loop_tail(); the name of the loop you want to run when the case number is randomly picked
- break; Tells the program to go back to the switch command and pick a new number as opposed to moving to the next case number in the sequence
Aside from it being hilarious to watch those little servos go, I'll be using these to control the head, tail, and eye movements with servos for a companion bot I plan to build. I'll need to fine tune the delay and angle ranges but my hope is that I'll get random movements that give my companion bot a life like appearance and personality.
Comments