Luo Yi Tan – Hallowuino

I made a horrifying ghoul for halloween, using a box, leds, servos, ping pong balls, vampire teeth and an arduino.

There is a photoresistor in its nose, which detects the amount of light in the environment. When the lights are off, the servos twitch at random angles to move the eyes and mouth. I used the autotune function from the circuit 6 in the SIK book so I didn’t have to calibrate it in different lighting environments.

Video of it in action:

[flickr video=8148307250 secret=351dfe9dff w=400 h=225]

 

Here’s a diagram of my circuit:

and my code:

#include 
Servo servo1, servo2, servo3;  // servo control object
int pos1 = 0;
int pos2 = 0;
int led1 = 11;
int led2 = 6;
int input = 3; 

int period = 500;
int sensorVal = 0;
unsigned long lastDidItTime = 0;
int high = 0, low = 1023;

void setup()
{
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(input, INPUT);
  servo1.attach(9);
  servo2.attach(10);
  servo3.attach(3);
  Serial.begin(9600);
} 

void loop() {
  sensorVal = analogRead(A0);
  autoTune();
  unsigned long presentTime = millis();
  Serial.println(sensorVal);
  if(sensorVal > 0){  
    unsigned long elapsedAmount = presentTime - lastDidItTime;
    if (elapsedAmount > period){
      pos1 = random (50, 140); 
      pos2 = random (30, 70);
      servo1.attach(9);
      servo2.attach(10);
      servo1.write(pos1);
      servo2.write(pos1);
      digitalWrite(led1, HIGH);
      digitalWrite(led2, HIGH);
      lastDidItTime = presentTime;
      servo3.write(pos2);
    }
  }
  else {
    servo1.write(90);
    servo2.write(90);
    servo3.write(90);
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
  }

}

void autoTune()
{
  if (sensorVal < low)   {     low = sensorVal;   }     if (sensorVal > high)
  {
    high = sensorVal;
  }

  sensorVal = map(sensorVal, low+30, high-30, 0, 255);
  sensorVal = constrain(sensorVal, 0, 255);

}

Post a comment