Skip to content
Snippets Groups Projects

Update Arduino_SecuritySystem.ino

Merged Kieran Williams requested to merge c1930510-main-patch-70252 into main
1 file
+ 113
0
Compare changes
  • Side-by-side
  • Inline
+ 113
0
#include <SoftwareSerial.h>
// Connect your Bluetooth Module to D8
#define RxD 8
#define TxD 9
#define PIR_MOTION_SENSOR 2
const int ledPin = 3; // the number of the LED pin, D3
const int buttonPin = 4; // the number of the pushbutton pin, D4
int buttonState; // the state of the button
int ledState = LOW; // the state of the LED
bool systemState = false;
SoftwareSerial blueToothSerial(RxD,TxD);
void setup()
{
Serial.begin(9600);
while(!Serial){
;
}
Serial.print("Started\n");
pinMode(PIR_MOTION_SENSOR, INPUT);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
Serial.flush();
blueToothSerial.flush();
pinMode(buttonPin, INPUT);
pinMode(PIR_MOTION_SENSOR, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop()
{
char recvChar;
static unsigned char state = 0;
if(blueToothSerial.available()>0){
char newState = blueToothSerial.read();
Serial.println("State Change: " + String(newState));
if (String(newState) == "0"){
systemState = false;
}
if (String(newState) == "1") {
systemState = true;
}
}
if(digitalRead(PIR_MOTION_SENSOR) && systemState == true){
blueToothSerial.println("Movement");
Serial.println("Movement");
}
buttonState = digitalRead(buttonPin);
// button not clicked
if (buttonState == HIGH){
if (systemState == false) {
analogWrite(ledPin, 1);
Serial.println("off");
blueToothSerial.println("off");
}
else {
analogWrite(ledPin, 255);
Serial.println("on");
blueToothSerial.println("on");
}
}
// button clicked
else {
Serial.println("============== buttonClick");
if (systemState == false) {
Serial.println("============== turn system on");
systemState = true;
} else {
Serial.println("============== turn system off");
systemState = false;
}
}
delay(200);
}
/***************************************************************************
* Function Name: setupBlueToothConnection
* Description: initilizing bluetooth connction
***************************************************************************/
void setupBlueToothConnection()
{
blueToothSerial.begin(9600);
blueToothSerial.print("AT");
delay(2000);
blueToothSerial.print("AT+BAUD4");
delay(2000);
blueToothSerial.print("AT+ROLES");
delay(2000);
blueToothSerial.print("AT+NAMEBluegum"); // set the bluetooth name as "Slave" ,the length of bluetooth name must less than 12 characters.
delay(2000);
blueToothSerial.print("AT+AUTH1");
delay(2000);
blueToothSerial.flush();
Serial.print("Finished\n");
}
Loading