Last active
August 29, 2015 14:08
-
-
Save angelagabereau/020166b81c8d116bb14f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Dracula Clyde | |
Halloween Hacks | |
Fabule Fabrications | |
by Angela Gabereau | |
Oct 21, 2014 | |
Motion triggers the haunting, Clyde's eye light and task light turns on, | |
and a spooky bit tune is played. Then Dracula Clyde goes silent again. | |
This musical part of this sketch is based on the Arduino Melody example. | |
http://arduino.cc/en/Tutorial/Tone | |
*/ | |
#include "pitches.h" | |
int taskLight = 11; // the pin that Clyde's task light is attached to | |
int piezo = 10; // the pin that the piezo is attached to | |
int R = 5, G = 6, B = 9; // the pins for the red, green and blue of the Clyde's eye light | |
int rColor = 255, gColor = 255, bColor = 0; // The color values for the R,G amd B pins | |
int pirPin = 3; //the digital pin connected to the PIR sensor's output | |
int calibrationTime = 30; //the time we give the sensor to calibrate (10-60 secs according to the datasheet) | |
boolean haunting = false; // should the task light and fan fade in and out and the eye light turn on. | |
int spookCount = 0; //The number of times the spooky light/fan cycle has run since trigger. | |
int spookLimit = 3; //The number of times the spooky light/fan cycle runs on trigger. | |
int brightness = 0; // how bright the LED is | |
int fadeAmount = 5; // how many points to fade the LED by | |
// A bittune loosely based on the theme song from Mike Meyer's Halloween. | |
// notes in the melody: | |
int halloweenMelody[] = { | |
NOTE_FS4, NOTE_A3,NOTE_AS3, | |
NOTE_FS5, NOTE_A4,NOTE_AS4, | |
NOTE_FS5, NOTE_A4,NOTE_AS4, | |
NOTE_FS4, NOTE_A3,NOTE_AS3, | |
NOTE_FS5, NOTE_A4,NOTE_AS4, | |
NOTE_FS5, NOTE_A4,NOTE_AS4, | |
NOTE_FS5, NOTE_A4,NOTE_AS4, | |
NOTE_FS5, NOTE_A4,NOTE_AS4, | |
NOTE_FS5, NOTE_A4,NOTE_AS4, | |
NOTE_FS5, NOTE_A4,NOTE_AS4, | |
NOTE_FS4, NOTE_A3,NOTE_AS3, | |
NOTE_FS5, NOTE_A4,NOTE_AS4, | |
NOTE_FS5, NOTE_A4,NOTE_AS4, | |
NOTE_FS6, NOTE_A5,NOTE_AS5, | |
NOTE_FS6, NOTE_A5,NOTE_AS5, | |
NOTE_FS7 | |
}; | |
// note durations: 4 = quarter note, 8 = eighth note, etc.: | |
int halloweenNoteDurations[] = { | |
2,2,1, | |
4,4,4, | |
4,4,4, | |
2,2,1, | |
4,4,4, | |
4,4,4, | |
8,8,8, | |
8,8,8, | |
8,8,8, | |
1,1,1, | |
8,8,8, | |
8,8,8, | |
8,8,8, | |
8,8,8, | |
1 | |
}; | |
// the setup routine runs once when you press reset: | |
void setup() { | |
//Initialize pins. | |
// Declare Clyde's task light pin to be an output: | |
pinMode(taskLight, OUTPUT); | |
// Turn off Clyde's task light by setting pin high. | |
digitalWrite(taskLight, HIGH); | |
// Declare fan pin to be an output: | |
pinMode(piezo, OUTPUT); | |
// Turn off fan by setting pin low. | |
digitalWrite(piezo, LOW); | |
// Declare R,G and B pins to be outputs, for Clyde's eye light. | |
pinMode(R, OUTPUT); | |
pinMode(G, OUTPUT); | |
pinMode(B, OUTPUT); | |
// Turn Clyde's eye light red to show that it is calibrating. | |
digitalWrite(R, 255); | |
digitalWrite(G, 0); | |
digitalWrite(B, 0); | |
calibrateMotionSensor(); | |
// turn Clyde's eye light off to show that calibration is complete | |
digitalWrite(R, 0); | |
digitalWrite(G, 0); | |
digitalWrite(B, 0); | |
} | |
// The loop routine runs over and over again forever: | |
void loop() { | |
/* spookyClyde(); | |
ghostbusters(); | |
delay(2000); | |
Serial.println("done");*/ | |
// Does Ghost Clyde perform his haunting behaviour? | |
if(haunting){ | |
// Turn on lights, play spooky song. | |
spookyClyde(); | |
//Turn off lights, end spooky song. | |
ghostbusters(); | |
delay(5000); | |
} | |
else{ | |
checkMotionSensor(); | |
} | |
} | |
// Turn on Clyde's task light and eye light, play a spooky halloween song. | |
void spookyClyde(){ | |
// Set Clyde's eye light to color | |
digitalWrite(R, rColor); | |
digitalWrite(G, gColor); | |
digitalWrite(B, bColor); | |
// Turn on the task light by setting pin low | |
analogWrite(taskLight, LOW); | |
playHalloweenSong(); | |
} | |
// Turn off all haunting behavior. | |
void ghostbusters(){ | |
// Reset haunting values. | |
haunting = false; | |
spookCount = 0; | |
// Turn Clyde's eye light off | |
digitalWrite(R, 0); | |
digitalWrite(G, 0); | |
digitalWrite(B, 0); | |
// Turn off Clyde by setting pin high. | |
digitalWrite(taskLight, HIGH); | |
// Turn off music by setting pin low. | |
digitalWrite(piezo, LOW); | |
} | |
//Wait a little while so that the motion sensor can calibrate. | |
void calibrateMotionSensor(){ | |
for(int i = 0; i < calibrationTime; i++){ | |
delay(1000); | |
} | |
delay(50); | |
} | |
void checkMotionSensor(){ | |
//If the PIR pin is high, trigger the haunting behavior. | |
if(digitalRead(pirPin) == HIGH){ | |
haunting = true; | |
} | |
} | |
//Thanks to Arduino's Melody example for this code. | |
void playHalloweenSong(){ | |
// iterate over the notes of the melody: | |
for (int thisNote = 0; thisNote < 44; thisNote++) { | |
// to calculate the note duration, take one second | |
// divided by the note type. | |
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. | |
int noteDuration = 1000/halloweenNoteDurations[thisNote]; | |
tone(10, halloweenMelody[thisNote],noteDuration); | |
// to distinguish the notes, set a minimum time between them. | |
// the note's duration + 30% seems to work well: | |
int pauseBetweenNotes = noteDuration * 1.30; | |
delay(pauseBetweenNotes); | |
// stop the tone playing: | |
noTone(10); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment