Sunday, October 30, 2011

Evil Pumpkin

I'm from Europe and we’re not celebrating Halloween, but I always wanted to carve a pumpkin.

So I’ve bought one and I carved it. At first I thought that a simple candle inside would be enough, but then I decided to go with arduino, LED, speaker and distance sensor. I’ve used my Nano, and for sensor I used my IR sensor. Here’s the video and code.


code:
int IRSense = A0;
int IRTrans = 2;
int Flick = 10;
int valueAmbient = 0;

void setup() {
 pinMode(IRSense, INPUT);
 pinMode(IRTrans, OUTPUT);
 digitalWrite(IRTrans,LOW);
}

void ambient()
{
 digitalWrite(IRTrans,HIGH);
 delay(20); 
 valueAmbient = analogRead(IRSense);
 delay(20);
}

void loop() 
{
 ambient();
 digitalWrite(IRTrans,LOW);
 delay(20);
 int value = analogRead(IRSense);
 int value2 = round(value - valueAmbient);
 int dist = (((value2+15.0)/(value2*(value2+100.0)))*2000.0);
 if(dist<20) 
  {
   analogWrite(Flick,random(0,256));
   tone(8, 38);
  }
 else
  {
   analogWrite(Flick,0);
   noTone(8);
  } 

2 comments:

  1. NICE! I like how you used your old IR sensor in this project. You would add a car horn like one of my hacks and scary the poo out of the kiddies, but using your IR sensor to trigger it instead of the button like I used:
    http://www.toddfun.com/2009/10/11/super-scary-honking-pumpkin/

    ReplyDelete