Oliver – Hallowuino!!

I decided to make a creepy doll arm “graveyard” where the ground is made of (fake) fur. Three plastic doll arms are connected to Servo motors, and they rotate at a speed controlled by a sliding potentiometer. If the potentiometer is at its lowest value, the arms twitch instead of rotating.

Photo:

 

Video:

[flickr video=8148299589 secret=8463568572 w=400 h=225]

 

Fritzing diagram:

 

Code:

 
#include Servo.h 
// WordPress will not allow the less than / greater than 
// symbols that normally go around Servo.h 

Servo servo1, servo2, servo3;
const int sensorPin = 0;
int sensorValue, delayTime, position;

void setup() {
  // servos are connected to pins 7, 8 and 9
  servo1.attach(9);
  servo2.attach(8);
  servo3.attach(7);
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(sensorPin);
  Serial.println (sensorValue); 
  moveServos();
}

void moveServos() {
  // rotate to 180 degrees
  for(position = 0; position = 0; position -= sensorValue) {       
    changePositions();         
  }
}

void changePositions() {
  // test for the sensor value as servo rotates to see if it should 
  // change speed
  sensorValue = analogRead(sensorPin);
  // map potentiometer values to speeds 1-7
  sensorValue = map(sensorValue, 0,900, 1,7);
  servo1.write(position);
  servo2.write(position);
  servo3.write(position);
  // test for delay time as servo rotates to see if it should 
  // switch to twitching
  delayTime = testDelayTime();
  delay(delayTime);  
}

int testDelayTime() {
  // if the potentiometer value is less than 2, switch to the 
  // twitching motion by setting delay time to zero
  if (sensorValue < 2) {
    delayTime = 0;
  }
  else {
    delayTime = 20;
  }
  return delayTime;
}

Post a comment