Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h>
- Servo myservo; // create servo object to control a servo
- // twelve servo objects can be created on most boards
- int pos = 90; // variable to store the servo position
- int ledPin = 13; // choose the pin for the LED
- int inputPin = 2; // choose the input pin (for PIR sensor)
- int pirState = LOW; // we start, assuming no motion detected
- int val = 0; // variable for reading the pin status
- void setup() {
- pinMode(ledPin, OUTPUT); // declare LED as output
- pinMode(inputPin, INPUT); // declare sensor as input
- pinMode(pinSpeaker, OUTPUT);
- Serial.begin(9600);
- myservo.attach(9); // attaches the servo on pin 9 to the servo object
- myservo.write(pos);
- delay(200);
- }
- void loop(){
- val = digitalRead(inputPin); // read input value
- if (val == HIGH) { // check if the input is HIGH
- digitalWrite(ledPin, HIGH); // turn LED ON
- playTone(300, 160);
- delay(150);
- 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;
- myservo.write(90); // This will cause if motion is detected for the servo to go to a 90 degree angle
- }
- } else {
- digitalWrite(ledPin, LOW); // turn LED OFF
- playTone(0, 0);
- delay(300);
- if (pirState == HIGH){
- // we have just turned off
- Serial.println("Motion ended!");
- // We only want to print on the output change, not state
- pirState = LOW;
- myservo.write(random(45, 135)); // This will cause the servo to pick a random angle between 45 and 135 degrees
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement