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);
}
}