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-

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.

#include <Servo.h> 

Servo myservo;  // creating a servo object 

int 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 detected
int val = 0;              // variable for reading the pin status
int pinSpeaker = 10;      //Set up a speaker on a PWM pin.
int pos = 0;              // variable to store the servo position 

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(pinSpeaker, OUTPUT);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600);
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, HIGH);  // turn LED ON
  delay(100);               // wait for a second
  
  digitalWrite(ledPin, LOW);    // turn the LED off by making the voltage LOW
  delay(100);                   // wait for a second
    playTone(300, 160);         // play the alarm
    delay(15);
    
     for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                       // in steps of 1 degree 
    myservo.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 monitor
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
      digitalWrite(ledPin, LOW); // turn LED OFF
      playTone(0, 0);
      delay(300);    
      if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}
// duration in mSecs, frequency in hertz
void 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

Popular Posts