I am new to Arduino, and already tried to edit this many time. But I cant figure out whats wrong.
The following code has a PIR sensor, 1 LED and 1 buzzer. I want to add one more PIR sensor, but it should trigger a separate LED , and the same buzzer. In serial monitor. I have searched many sketches, but all of them seems to have a different method , cant figure out the basics!
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
int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
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
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;
}
} 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;
}
}
}
// 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);
}
}
-
1What have you tried? You basically just need to copy and paste the parts of the code that deal with the pir sensor and change the variable names in the copy to something else. How much of the code that you have now do you actually understand? If you understand what that code is doing, then repeating it for a second sensor should be trivial.Delta_G– Delta_G2017年06月03日 05:44:34 +00:00Commented Jun 3, 2017 at 5:44
-
@Delta_G, I tried to add duplicate code inside 'loop' for different pins, but it didn't work. I was doing it virtually in circuits.ioShijil T– Shijil T2017年06月03日 07:21:26 +00:00Commented Jun 3, 2017 at 7:21
-
Please post your update (solution) as an answer, then accept it after a while, so we keep the usual question/answer(s) split of the site.jfpoilpret– jfpoilpret2017年06月03日 08:00:19 +00:00Commented Jun 3, 2017 at 8:00
-
@jfpoilpret, OKShijil T– Shijil T2017年06月03日 08:36:13 +00:00Commented Jun 3, 2017 at 8:36
1 Answer 1
Well, after playing with the code for some time, I just did it!! pasting the updated code here, may be useful for someone else like me..
int ledPin = 13; // choose the pin for the LED
int ledPinB = 12; // choose the pin for the LED 2
int inputPin = 2; // choose the input pin (for PIR sensor)
int inputPinB = 4; // choose the input pin (for PIR sensor 2)
int pirState = LOW; // we start, assuming no motion detected
int val1 = 0; // variable for reading the pin status
int val2 = 0; // variable for reading the pin status
int pinSpeaker = 10; //Set up a speaker on a PWM pin (digital 9, 10, or 11)
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(ledPinB, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(inputPinB, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
Serial.begin(9600);
}
void loop(){
val1 = digitalRead(inputPin); // read input value
val2 = digitalRead(inputPinB); // read input value
if (val1 == 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;
}
}
else if (val2 == HIGH) { // check if the input is HIGH
digitalWrite(ledPinB, 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;
}
}
else {
digitalWrite(ledPin, LOW); // turn LED OFF
digitalWrite(ledPinB, 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;
}
}
}
// 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);
}
}
-
Good job. Thank you for sharing so we have this in the database.SDsolar– SDsolar2017年06月03日 11:12:53 +00:00Commented Jun 3, 2017 at 11:12