Midterm Pumpkin by Aneta Genova 

Castle of the Black Crow

The pumpkin castle exudes a soft fading light when it is at peace. As soon as somebody approaches the light starts blinking faster. I am using a PIR motion sensor to detect motion for this action. The black crow sits next to its castle and guards its against predators. If you come closer it attacks (using my own hands and imagination here) and the crow’s eyes start blinking, triggered by a tilt switch.



Code
// Midterm project Aneta Genova
// fading LED and motion sensor

int timer = 500;
int sensorPin = A0;
int sensorValue = 0;
int ledPin = 11;

void setup()

{
pinMode(11, OUTPUT);
Serial.begin (9600);
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
delay (2000); // it takes the sensor 2 seconds to scan the area around it before it can detect presence.
}

void fade(int pin, int start, int finish, int milliseconds)   // fading set up
{
uint32_t startMillis = millis();  // remember when we started
while (true)  // we will ‘break’ when we are done
{
uint32_t elapsedTime = millis() – startMillis;  // track the time

// convert the elapsed time into a brightness range
int brightness = map(elapsedTime, 0, milliseconds, start, finish);
analogWrite(pin, brightness);

// exit when milliseconds have elapsed
if (elapsedTime >= milliseconds)
{
break;
}
delay(1);
}
}

void loop()
{

//PIR motion sensor is introduced
sensorValue = analogRead(sensorPin);
if (sensorValue < 100)
while(analogRead(sensorPin) < 100)  //execute the command while statement is true
{
digitalWrite(11,HIGH);
delay(200);
digitalWrite(11,LOW);
delay(200); // blinks when the motion has been detected
}
else
{

fade(11, 0, 255, 2000);  // fade led on pin 11 from min to max over three second
delay(2000);  // hold for 2 seconds
fade(11, 255, 0, 2000);  // fade pin 11 from max to min over 2 seconds
delay(1000);  // hold for 1 second

}
}