Created
October 22, 2014 03:58
-
-
Save angelagabereau/265d4f369366c10d77d3 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
/* | |
Ghost Clyde | |
Halloween Hacks | |
Fabule Fabrications | |
by Angela Gabereau | |
Oct 21, 2014 | |
Motion triggers the haunting, Clyde's eye light turns on, and the task light | |
and fan fade in and out several times, then the ghost goes silent again. | |
Arduino's basic fade example was the starting points of this code. | |
*/ | |
int taskLight = 11; // the pin that Clyde's task light is attached to | |
int fan = 13; // the pin that the fan 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 = 255; // The color values for the R,G amd B pins | |
int pirPin = 10; //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 | |
// 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(fan, OUTPUT); | |
// Turn off fan by setting pin low. | |
digitalWrite(fan, 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() { | |
// Does Ghost Clyde perform his haunting behaviour? | |
if(haunting){ | |
// Fade light and fan. | |
spookyClyde(); | |
// Check if the spooky light and fan fading cycle has run enought times. | |
if(spookCount>spookLimit){ | |
// Who you gonna call?! | |
ghostbusters(); | |
} | |
}else{ | |
checkMotionSensor(); | |
} | |
} | |
// Fade Clyde's task light and fan, turn on his eye light. | |
void spookyClyde(){ | |
// Set Clyde's eye light to color | |
digitalWrite(R, rColor); | |
digitalWrite(G, gColor); | |
digitalWrite(B, bColor); | |
// Set the brightness of the task light: | |
analogWrite(taskLight, 255-brightness); | |
// Set the brightness of the fan: | |
analogWrite(fan, brightness); | |
// Change the brightness for next time through the loop: | |
brightness = brightness + fadeAmount; | |
// Reverse the direction of the fading at the ends of the fade: | |
if (brightness == 0 || brightness == 255) { | |
fadeAmount = -fadeAmount ; | |
} | |
//A moment of darkness. | |
if (brightness == 0){ | |
delay(1000); | |
spookCount++; | |
} | |
// Wait for 30 milliseconds to see the dimming effect | |
delay(30); | |
} | |
// 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 fan by setting pin low. | |
digitalWrite(fan, 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment