From sketch to reality
Sid the garden protector
This project is a part of my university's prototype course. Please read my earlier posts about the topic:
Project plan: the garden protector and Building the prototype.
The components:
Piezo speaker, alligator clips, PIR motion sensor, servo, Arduino UNO, breadboard, 2 x yellow leds-
Project plan: the garden protector and Building the prototype.
The components:
Piezo speaker, alligator clips, PIR motion sensor, servo, Arduino UNO, breadboard, 2 x yellow leds-
PIR motion sensor connected. |
I built this project step by step. At first I wanted to make sure that all the components work individually before putting them work together.
Why there is just one wing you may ask. The answer simple - I have hunting dogs who love feathers and they got poor Sid in their mouth. The wings were destroyed and so was the other Servo... Sid also lost half of his feathers. But at least he survived.
The circuit
The code
Originally I had a problem to get all these three actions to work at the same time (blinking of LED lights, alarm sound, flapping of wings.) The solution was rather simple though, I just changed the values of the delay codes and got it work smoothly.
This code is based on PIR Sensor Arduino Alarm tutorial and Arduino's own examples of blinking lights and rotation of Servo.
This code is based on PIR Sensor Arduino Alarm tutorial and Arduino's own examples of blinking lights and rotation of Servo.
#include <Servo.h>Servo myservo; // creating a servo objectint ledPin = 13; // Led is placed to the pin 13.int inputPin = 7; // Another led places in the pin 7.int pirState = LOW; // we start, assuming no motion detectedint val = 0; // variable for reading the pin statusint pinSpeaker = 10; //Set up a speaker on a PWM pin.int pos = 0; // variable to store the servo positionvoid setup() {pinMode(ledPin, OUTPUT); // declare LED as outputpinMode(inputPin, INPUT); // declare sensor as inputpinMode(pinSpeaker, OUTPUT);myservo.attach(9); // attaches the servo on pin 9 to the servo objectSerial.begin(9600);}void loop(){val = digitalRead(inputPin); // read input valueif (val == HIGH) { // check if the input is HIGHdigitalWrite(ledPin, HIGH); // turn LED ONdelay(100); // wait for a seconddigitalWrite(ledPin, LOW); // turn the LED off by making the voltage LOWdelay(100); // wait for a secondplayTone(300, 160); // play the alarmdelay(15);for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees{ // in steps of 1 degreemyservo.write(pos); // tell servo to go to position in variable 'pos'delay(1); // waits 1ms for the servo to reach the position}for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees{myservo.write(pos); // tell servo to go to position in variable 'pos'delay(1); // waits 1ms for the servo to reach the position}//for the serial monitorif (pirState == LOW) {// we have just turned onSerial.println("Motion detected!");// We only want to print on the output change, not statepirState = HIGH;}} else {digitalWrite(ledPin, LOW); // turn LED OFFplayTone(0, 0);delay(300);if (pirState == HIGH){// we have just turned ofSerial.println("Motion ended!");// We only want to print on the output change, not statepirState = LOW;}}}// duration in mSecs, frequency in hertzvoid playTone(long duration, int freq) {duration *= 1000;int period = (1.0 / freq) * 1000000;long elapsed_time = 0;while (elapsed_time < duration) {digitalWrite(pinSpeaker,HIGH);delayMicroseconds(period / 2);digitalWrite(pinSpeaker, LOW);delayMicroseconds(period / 2);elapsed_time += (period);}}
The result
Based on the prototype course by Tero Karvinen and the book Karvinen 2011: Make Arduino Bots and Gadgets.
Sources: http://makezine.com/projects/pir-sensor-arduino-alarm/ and Arduino's own example codes.
Comments
Post a Comment