ผมทำระบบเตือนภัยภายในบ้านควบคุมโดยบอร์ด adrino และโทรเข้ามือถือเวลาเซ็นเซอร์ตรวจจับวัถตุ

ช่วยเช็คให้หน่อยครับ ว่ามีโค้ดตรงไหนผิดหรือเปล่าครับ

#include <Password.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "inetGSM.h"
#include "sms.h"
#include "call.h"

Password password = Password( "1234" ); // Set the Password

LiquidCrystal_I2C lcd(0x27, 16, 2);

const byte ROWS = 4;
const byte COLS = 4;
const int status_lock = A0;
const int status_unlock = A1;
const int led_bed = 11;
const int led_bath = 12;
int pir = A2;            
int flame = A3;          
int Buzzer = 10;         
int alarmValue = 0;
int fl = 0, state = 0, state1 = 0, state2 = 0, Alarm_fl = 0; // Flag
int dir = 8, dir1 = 0;
String test = "                    ";

// Define the Keymap
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = { 9,8,7,6, };
byte colPins[COLS] = { 5,4,3,2 };

CallGSM call;
SMSGSM sms;
char number[20];
byte stat=0;
int value=0;
int pin=9;
char value_str[5];
int sensorVal;
char testnumber[]="0924144922";

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  Serial.begin(9600); // Define Serial for Debug Sensor & Keyped
  lcd.begin();
  lcd.backlight();
  pinMode(status_lock, OUTPUT);    // Sets the digital pin as output
  pinMode(status_unlock, OUTPUT);  // Sets the digital pin as output
  pinMode(led_bed, OUTPUT);        // Sets the digital pin as output
  pinMode(led_bath, OUTPUT);       // Sets the digital pin as output
  keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
  
  lcd.print("* Hello Welcome To *");
  lcd.setCursor(0, 1);
  lcd.print(" Home Alarm Systems ");
  lcd.setCursor(0, 2);
  lcd.print("  ** Home Lock **  ");
  pinMode(pin,INPUT);
  //Serial connection.
  Serial.println("GSM Shield testing.");
  if (gsm.begin(2400))
  Serial.println("\nstatus=READY");
  else Serial.println("\nstatus=IDLE");
};
void loop()
{
  char key = keypad.getKey();
  motion_on();  
  flame_on();         
  close_all_power();
{
  sensorVal = analogRead(1);
}while(sensorVal==0);
call.Call(testnumber);
delay(15000);
call.HangUp();
delay(5000);
  
  if (key != NO_KEY){
    Serial.println(key);
  }
}


void keypadEvent(KeypadEvent eKey) {
  switch (keypad.getState()) {
    case PRESSED:
      
      lcd.setCursor(dir1, 3);
      lcd.print(test);
      test = "*";
      lcd.setCursor(dir, 3);
      lcd.print(test);
      dir1 = dir;
      switch (eKey)
      {
        case 'A':
          if (fl == 1)
          {
            if (state == 0)
            {
              digitalWrite(led_bed, HIGH); // Open the light 2
              state = 1;
            }
            else if (state == 1)
            {
              digitalWrite(led_bed, LOW); // Close the light 2
              state = 0;
            }
          }
          break;
        case 'B':
          if (fl == 1)
          {
            if (state1 == 0)
            {
              digitalWrite(led_bath, HIGH); // Open the light 1
              state1 = 1;
            }
            else if (state1 == 1)
            {
              digitalWrite(led_bath, LOW); // Close the light 2
              state1 = 0;
            }
          }
          break;
        case 'C':
          if (fl == 1)
          {
            state2 = 1;
            lcd.setCursor(0, 3);
            lcd.print("  ** Home Lock **  ");
            dir  = 8;
            dir1 = 0;
            test = "                    ";
          }
          break;
        case '#':
          state2 = 0;
          checkPassword(); // Call the function CheckPassword
          password.reset(); //Reset Key
          dir  = 8;
          dir1 = 0;
          test = "                    ";
          lcd.setCursor(0, 3);
          lcd.print(test);
          break;
        default: password.append(eKey);
          dir++;
      }
  }
}

void checkPassword()
{
  if (password.evaluate()) {
    
    while (state2 == 0)
    {
      
      fl = 1;
      digitalWrite(status_lock, LOW);
      digitalWrite(status_unlock, HIGH);
      lcd.setCursor(0, 3);
      lcd.print(" ** Home Un Lock ** ");
      keypad.getKey();
      Alarm_off();
      password.reset();
    }
  }
  else {
    Serial.println("Wrong");
    //add code to run if it did not work
    lcd.setCursor(0, 3);
    lcd.print("**Password Wrong **");
    delay(1000);
  }
}

void close_all_power() // Function close the light all.
{
  digitalWrite(status_lock, HIGH);
  digitalWrite(status_unlock, LOW);
  digitalWrite(led_bed, LOW);
  digitalWrite(led_bath, LOW);
}

void Alarm() // Function Alarm.
{
  tone(Buzzer, 10, 100); // Alarm at 10 Hz 100 milliseconds.
  delay(100); // delay 100 milliseconds
  tone(Buzzer, 300, 100);// Alarm at 300 Hz 100 milliseconds.
  delay(100); // delay 100 milliseconds
}

void Alarm_off() // Function Alarm off.
{
  digitalWrite(Buzzer, HIGH); //Set buzzer pin is HIGH for stop alarm
}

void motion_on() // Function Open PIR Motion Sensor.
{
  alarmValue = analogRead(pir); // Read analog sensor 0-1023.
  //Serial.println (alarmValue); // Debug Sensor value.
  if (alarmValue >= 1000) // Check the value
  {
    Alarm_off();
  }
  if (alarmValue < 1000)
  {
    Alarm();
  }
}
void flame_on() // Function Open Flame Detector Sensor.
{
  alarmValue = analogRead(flame); // Read analog sensor 0-1023.
  //Serial.println (alarmValue);  // Debug Sensor value.
  if (alarmValue <= 700)  // Check the value
  {
    Alarm();
  }
  if (alarmValue > 700)
  {
    Alarm_off();
  }
}

ปล.ทำวิจัยครับ ขอบคุณมากครับ
แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่