-
Notifications
You must be signed in to change notification settings - Fork 3
/
PimpYourPumpkin.pde
43 lines (41 loc) · 1.13 KB
/
PimpYourPumpkin.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#define CANDLELED 11
#define REDLED 7
#define BUZZER 5
#define SENSOR 4
#define PROXIMITY_THRESHOLD 85
#define PROXIMITY_CONSECUTIVE_READINGS 3
#define BUZZER_FREQUENCY 38
#define FLICKER_INTERVAL 25
long previousMillis = 0;
long closeReadings = 0;
void setup() {
pinMode(CANDLELED, OUTPUT);
pinMode(REDLED, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(SENSOR, INPUT);
delay(1000); // Allow the proximity sensor to initialize
}
void loop() {
if (analogRead(SENSOR) > PROXIMITY_THRESHOLD) // Is someone close?
{
closeReadings++;
if (closeReadings >= PROXIMITY_CONSECUTIVE_READINGS) // require n consecutive "close" readings before going into EVIL mode. This prevents little blips of the buzzer.
{
digitalWrite(CANDLELED, LOW);
digitalWrite(REDLED, HIGH);
tone(BUZZER, BUZZER_FREQUENCY);
}
}
else
{ //regular candle flicker. Based on Arduino example BlinkWithoutDelay.
noTone(BUZZER);
digitalWrite(REDLED, LOW);
closeReadings = 0;
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > FLICKER_INTERVAL)
{
previousMillis = currentMillis;
analogWrite(CANDLELED, random(0, 256));
}
}
}