Advertisement
Guest User

Arduino Peek-a-boo dragon

a guest
Jan 18th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo myservo;  // create servo object to control a servo
  4. // twelve servo objects can be created on most boards
  5.  
  6. int pos = 90;    // variable to store the servo position
  7. int ledPin = 13;                // choose the pin for the LED
  8. int inputPin = 2;               // choose the input pin (for PIR sensor)
  9. int pirState = LOW;             // we start, assuming no motion detected
  10. int val = 0;                    // variable for reading the pin status
  11.  
  12. void setup() {
  13.   pinMode(ledPin, OUTPUT);      // declare LED as output
  14.   pinMode(inputPin, INPUT);     // declare sensor as input
  15.   pinMode(pinSpeaker, OUTPUT);
  16.   Serial.begin(9600);
  17.   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  18.   myservo.write(pos);
  19.   delay(200);
  20. }
  21.  
  22. void loop(){  
  23.   val = digitalRead(inputPin);  // read input value
  24.   if (val == HIGH) {            // check if the input is HIGH
  25.     digitalWrite(ledPin, HIGH);  // turn LED ON
  26.     playTone(300, 160);
  27.     delay(150);
  28.  
  29.    
  30.     if (pirState == LOW) {
  31.       // we have just turned on
  32.       Serial.println("Motion detected!");
  33.       // We only want to print on the output change, not state
  34.       pirState = HIGH;
  35.        myservo.write(90); // This will cause if motion is detected for the servo to go to a 90 degree angle
  36.        
  37.     }
  38.   } else {
  39.       digitalWrite(ledPin, LOW); // turn LED OFF
  40.       playTone(0, 0);
  41.       delay(300);    
  42.       if (pirState == HIGH){
  43.       // we have just turned off
  44.       Serial.println("Motion ended!");
  45.       // We only want to print on the output change, not state
  46.       pirState = LOW;
  47.       myservo.write(random(45, 135)); // This will cause the servo to pick a random angle between 45 and 135 degrees
  48.      
  49.      
  50.     }
  51.   }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement