ใช้ PIR เป็นตรวจจับความเคลื่อนไหวภายในบ้าน
แสดงผลมีการ alarm และส่งข้อความค่ะ
แต่ไม่ทราบวิธีใช้ PIR หลายๆตัวช่วยดูโค้ดให้หน่อยค่ะ
#include "SIM900.h"
#include <SoftwareSerial.h>
//If not used, is better to exclude the HTTP library,
//for RAM saving.
//If your sketch reboots itself proprably you have finished,
//your memory available.
//#include "inetGSM.h"
//If you want to use the Arduino functions to manage SMS, uncomment the lines below.
#include "sms.h"
SMSGSM sms;
//To change pins for Software Serial, use the two lines in GSM.cpp.
//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.
//Simple sketch to send and receive SMS.
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
/********************************************************************************/
int swPin = 7; // pin for switch
int x = LOW; // reading for switch status
int ledPin = 12; // choose the pin for the LED
int inputPin = 11; // 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)
int P = 0 ;
void setup()
{
//Serial connection.
Serial.begin(9600);
Serial.println("GSM Shield testing.");
//Start configuration of shield with baudrate.
//For http uses is raccomanded to use 4800 or slower.
if (gsm.begin(2400)) {
Serial.println("\nstatus=READY");
started=true;
} else Serial.println("\nstatus=IDLE");
pinMode(swPin, INPUT);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
};
void loop(){
x = digitalRead(swPin); // read switch
val = digitalRead(inputPin); // read input value
if(x == HIGH ){
if (val == HIGH) { // check if the input is HIGH
if (pirState == LOW) {
// we have just turned on
// We only want to print on the output change, not state
pirState = HIGH;
if(started) {
//Enable this two lines if you want to send an SMS.
if (sms.SendSMS("0982649823", "Arduino SMS Alarm"))
Serial.println("\nSMS sent OK");
digitalWrite(ledPin, HIGH);
for( P = 0 ; P <= 15 ; P++){playTone(98, 300); delay(100);}
}
}
digitalWrite(ledPin, HIGH); // turn LED ON
//playTone(300, 160);
//delay(150);
delay(150);
} 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);
}
}
คือเขียนเวิร์คแค่ใช้ตัวเดียวค่ะ ขอคำแนะนำ PIR หลายๆตัวหน่อยค่ะ
ขอคำแนะนำการเขียนโค้ด arduino ค่ะ
แสดงผลมีการ alarm และส่งข้อความค่ะ
แต่ไม่ทราบวิธีใช้ PIR หลายๆตัวช่วยดูโค้ดให้หน่อยค่ะ
#include "SIM900.h"
#include <SoftwareSerial.h>
//If not used, is better to exclude the HTTP library,
//for RAM saving.
//If your sketch reboots itself proprably you have finished,
//your memory available.
//#include "inetGSM.h"
//If you want to use the Arduino functions to manage SMS, uncomment the lines below.
#include "sms.h"
SMSGSM sms;
//To change pins for Software Serial, use the two lines in GSM.cpp.
//GSM Shield for Arduino
//www.open-electronics.org
//this code is based on the example of Arduino Labs.
//Simple sketch to send and receive SMS.
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
/********************************************************************************/
int swPin = 7; // pin for switch
int x = LOW; // reading for switch status
int ledPin = 12; // choose the pin for the LED
int inputPin = 11; // 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)
int P = 0 ;
void setup()
{
//Serial connection.
Serial.begin(9600);
Serial.println("GSM Shield testing.");
//Start configuration of shield with baudrate.
//For http uses is raccomanded to use 4800 or slower.
if (gsm.begin(2400)) {
Serial.println("\nstatus=READY");
started=true;
} else Serial.println("\nstatus=IDLE");
pinMode(swPin, INPUT);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(pinSpeaker, OUTPUT);
};
void loop(){
x = digitalRead(swPin); // read switch
val = digitalRead(inputPin); // read input value
if(x == HIGH ){
if (val == HIGH) { // check if the input is HIGH
if (pirState == LOW) {
// we have just turned on
// We only want to print on the output change, not state
pirState = HIGH;
if(started) {
//Enable this two lines if you want to send an SMS.
if (sms.SendSMS("0982649823", "Arduino SMS Alarm"))
Serial.println("\nSMS sent OK");
digitalWrite(ledPin, HIGH);
for( P = 0 ; P <= 15 ; P++){playTone(98, 300); delay(100);}
}
}
digitalWrite(ledPin, HIGH); // turn LED ON
//playTone(300, 160);
//delay(150);
delay(150);
} 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);
}
}
คือเขียนเวิร์คแค่ใช้ตัวเดียวค่ะ ขอคำแนะนำ PIR หลายๆตัวหน่อยค่ะ