Minnar – Halloweeno!

Timid Ghostie: Description

I wanted to create on object that would encourage and react to certain types of interaction. The ghost I created will move forward to your hand smoothly and sweetly when you approach it slowly, but freaks out (jerky movement backward and forward fast) when you approach it too quickly. I’m using an infrared distance sensor to detect where someone is in relation to ghostie, and then I also calculate the rate at which someone is approaching ghostie.

 Video

[flickr video=8148281807 secret=6641205d38 w=400 h=225]

 

Code & Fritzing Diagram

#include  
Servo servo1;

float runningAverage = 0;
int startTime = 0;
int spookyDuration = 1000; // one second
boolean bAmBeingSpooky = false;

void setup()
{
  Serial.begin(9600); 
  servo1.attach(9);

  Serial.println("Program Starting"); 
  pinMode (13, OUTPUT);
}

void loop(){  
  delay(50); 
  int sensorVal = analogRead(2);
  int previousAverage = runningAverage;
  runningAverage = 0.91*runningAverage + 0.09*sensorVal; // higher previous runningAvg = slower movement
  //Serial.println(runningAverage); 

  if (runningAverage > 400){
    startTime = millis();
  }

  int prevPos = map(previousAverage, 0, 800, 0, 180);
  int curPos = map(runningAverage, 0,800, 0,180); 

  if (curPos - prevPos > 5){
    servo1.write(0);
  } else {
   servo1.write( curPos );
  }

}

 

 

Post a comment