Sleepy Servo

The Sleepy Servo is a cute desk toy meant to simulate a living creature to keep you company while you work. It can be placed in any location where a cute companion is needed provided there is power readily available.

Initially full of energy, the Sleepy Servo tends to become slower the longer it is left on, mimicking a living being. When observed over a long period, the motions become much slower; however, it’s motions remain smooth rather than the jerky motions of a machine to give the illusion of a living creature. Even though the Sleepy Servo gets tired over time, it always maintains its smile to motivate you to give your best.

Sleepy Servo features a simplistic design face design comprised of paper to allow for easy customization and assembly.

 

My code works by smoothly moving the servo arm to a new randomly determined position over a randomly determined time; however, the determined time interval's random range gets larger and larger as time goes on. This increasing range leads to an longer transition times on average which gives the appearance of the servo growing tired.
</pre>
#include <Servo.h>

const int SERVO_PIN = 9;
Servo motion;
int initLocation = 60;
int startLocation;

void setup() {
// put your setup code here, to run once:
motion.attach(SERVO_PIN);
motion.write(initLocation);
Serial.begin(115200);
}

long currTime = 0; // gets tired as time goes on

void smoothMotion(int startLoc, int newLoc, long timeInterval)
{
int currentLocation = startLoc;
double magnitude = (abs(newLoc-startLoc));
double timeSteps = timeInterval/magnitude;
int servoSteps = magnitude/(timeInterval/timeSteps);
for(double i =0; i < timeInterval; i+=timeSteps)
{
if(startLoc > newLoc)
{
currentLocation -= servoSteps;
}
else
{
currentLocation += servoSteps;
}
motion.write(currentLocation);
// Serial.println(timeInterval);
// Serial.println(servoSteps);
//Serial.println(currentLocation);
delay(timeSteps);
}
startLocation = currentLocation;
}

void loop()
{
currTime = millis()/50;
long timeToCompleteAction = random(15,currTime);
int newLocation = random(20,170);
smoothMotion(startLocation, newLocation, timeToCompleteAction);
}
<pre>