int ledPin = 13; // choose the pin for the LED
int inputPin = 2;
int trigerpin= 7;
int echopin=12;
int speakerpin=10; // choose the input pin (for PIR sensor)
int pirState = LOW;
int pitch=262; // 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(trigerpin, OUTPUT);
pinMode(echopin, INPUT);
pinMode(speakerpin, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = digitalRead(inputPin); // read input value
if (val<200) { // check if the input is HIGH
digitalWrite(ledPin, HIGH);
tone(speakerpin,pitch); // turn LED ON
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
noTone(speakerpin);
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;
}
}
}