Stap 4: Programma Ghost Clyde's spooky gedrag
Kopieer en plak de volgende code in de Arduino IDE. Uploaden naar Clyde en laat de hauntings beginnen!
De code is ook beschikbaar op GitHub gist:
<p>/*<br> 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</p><p>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</p><p>// the setup routine runs once when you press reset: void setup() { //Initialize pins.</p><p> // 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);</p><p> // Declare fan pin to be an output: pinMode(fan, OUTPUT); // Turn off fan by setting pin low. digitalWrite(fan, LOW);</p><p> // 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);</p><p> calibrateMotionSensor(); // turn Clyde's eye light off to show that calibration is complete digitalWrite(R, 0); digitalWrite(G, 0); digitalWrite(B, 0); } </p><p>// The loop routine runs over and over again forever: void loop() {</p><p> // 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(); } }</p><p>// Fade Clyde's task light and fan, turn on his eye light. void spookyClyde(){</p><p> // Set Clyde's eye light to color digitalWrite(R, rColor); digitalWrite(G, gColor); digitalWrite(B, bColor);</p><p> // Set the brightness of the task light: analogWrite(taskLight, 255-brightness); // Set the brightness of the fan: analogWrite(fan, brightness); </p><p> // Change the brightness for next time through the loop: brightness = brightness + fadeAmount;</p><p> // Reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; }</p><p> //A moment of darkness. if (brightness == 0){ delay(1000); spookCount++; }</p><p> // Wait for 30 milliseconds to see the dimming effect delay(30); }</p><p>// Turn off all haunting behavior. void ghostbusters(){ // Reset haunting values. haunting = false; spookCount = 0;</p><p> // Turn Clyde's eye light off digitalWrite(R, 0); digitalWrite(G, 0); digitalWrite(B, 0);</p><p> // Turn off Clyde by setting pin high. digitalWrite(taskLight, HIGH);</p><p> // Turn off fan by setting pin low. digitalWrite(fan, LOW); }</p><p>//Wait a little while so that the motion sensor can calibrate. void calibrateMotionSensor(){</p><p> for(int i = 0; i < calibrationTime; i++){ delay(1000); } delay(50); }</p><p>void checkMotionSensor(){ //If the PIR pin is high, trigger the haunting behavior. if(digitalRead(pirPin) == HIGH){ haunting = true; } }</p>