//#include <SoftwareSerial.h>
#define PIR_PIN_1 3 //signal PIN from PIR sensor
#define LIGHT_1 50 // PIN for activating relay
//#define rxPin 12 //pin na kojem Arduino PRIMA data od bluetooth modula
//#define txPin 11 //pin na koji Arduino ŠALJE data na bluetooth modul
int delay_time = 0; //default delay time for light bulb
int movement_1 = 0; //0 for movement not detected, 1 for movement detected
byte PIR_sensor_status = 0; //0 for PIR sensor not avtivated, 1 for activated
//SoftwareSerial bluetooth_serial = SoftwareSerial(rxPin, txPin);
void setup() {
// put your setup code here, to run once:
pinMode(LIGHT_1, OUTPUT); //on this PIN sending signal from Arduino to relay
//pinMode(rxPin, INPUT);
//pinMode(txPin, OUTPUT);
pinMode(PIR_PIN_1, INPUT_PULLUP); //on this PIN receiving signal from PIR sensor
attachInterrupt(digitalPinToInterrupt(PIR_PIN_1), pir_interrupt, CHANGE);
/*interrupt programm when receiving signal from PIR sensor on CHANGE PIN status*/
Serial.begin(9600);
//bluetooth_serial.begin(9600);
delay(1500);
/*
while (!bluetooth_serial) {
Serial.println("Bluetooth not connected");
}
*/
digitalWrite(LIGHT_1, LOW); //disable light on the beginning of the programm
PIR_sensor_status = 1; //turn on PIR sensor
}
void loop() {
// put your main code here, to run repeatedly:
read_from_keyboard();
delay(20);
}
void PIR_sensor() {
noInterrupts();
movement_1 = digitalRead(PIR_PIN_1);
Serial.print("Movemment: ");
Serial.print(movement_1);
Serial.println();
Serial.print("PIR sensor status: ");
Serial.print(PIR_sensor_status);
Serial.println();
if (movement_1 == 1 && PIR_sensor_status == 1) {
digitalWrite(LIGHT_1, HIGH); //if PIR sensor activated turn ON the Relay
delay(delay_time);
//digitalWrite(LIGHT_1, HIGH);
}
else if (movement_1 == 1 && PIR_sensor_status == 0){
digitalWrite(LIGHT_1, LOW);
delay(delay_time);
}
else if (movement_1 == 0 && PIR_sensor_status == 1){
digitalWrite(LIGHT_1, LOW);
delay(delay_time);
}
else if (movement_1 == 0 && PIR_sensor_status == 0){
digitalWrite(LIGHT_1, LOW);
delay(delay_time);
}
interrupts();
}
void pir_interrupt() {
PIR_sensor();
}
void read_from_keyboard() {
if (Serial.available() > 0){
int stroke = Serial.read(); //read from keyboard input
switch (stroke) {
case 49: //keyboard button "1"
Serial.println("Light on");
digitalWrite(LIGHT_1, HIGH);
break;
case 48: //keyboard button "0"
Serial.println("Light off");
digitalWrite(LIGHT_1, LOW);
break;
case 50: //keyboard button "2"
Serial.println("PIR Sensor on");
PIR_sensor_status = 1;
break;
case 51: //keyboard button "3"
Serial.println("PIR Sensor off");
PIR_sensor_status = 0;
break;
default:
break;
}
}
}