Last active
October 30, 2020 11:45
-
-
Save fxprime/80bf3e56ab2ba0defdea365de4b0e0ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Arduino.h> | |
| const int wait_time = 5000; //เวลารอ 5 วินาที | |
| const int sensor_foot = 8; //ขาสัญญาณเซนเซอร์ PIR ที่เท้า | |
| const int sensor_glass = 9; //ขาสัญญาณเซนเซอร์ PIR ที่กระจกหลัง | |
| const int sensor_steer = 10; //ขาสัญญาณเซนเซอร์ PIR ที่พวงมาลัย | |
| const int buzzer = 13; //ขาสัญญาณขาออกไปยัง Buzzer | |
| unsigned long foot_ts = 0; //ตัวแปรเก็บค่าเวลาเมื่อเซนเซอร์เท้าจับสัญญาณความเคลื่อนไหวได้ | |
| unsigned long glass_ts = 0; //ตัวแปรเก็บค่าเวลาเมื่อเซนเซอร์กระจกมองหลังจับสัญญาณความเคลื่อนไหวได้ | |
| unsigned long steer_ts = 0; //ตัวแปรเก็บค่าเวลาเมื่อเซนเซอร์พวงมาลัยจับสัญญาณความเคลื่อนไหวได้ | |
| void setup() { | |
| pinMode(sensor_foot, INPUT); //ตั้งค่าเซนเซอร์เป็นสัญญาณขาเข้า | |
| pinMode(sensor_glass, INPUT); //ตั้งค่าเซนเซอร์เป็นสัญญาณขาเข้า | |
| pinMode(sensor_steer, INPUT); //ตั้งค่าเซนเซอร์เป็นสัญญาณขาเข้า | |
| pinMode(buzzer, OUTPUT); //ตั้งค่าเซนเซอร์เป็นสัญญาณขาออก | |
| Serial.begin(115200); //ตั้งค่าความถี่ในการพิมพ์ตัวอักษรออกหน้าจอ | |
| Serial.println("System inited."); | |
| } | |
| void loop() { | |
| unsigned long time_now = millis(); //เก็บค่าเวลา ณ ปัจจุบัน | |
| int foot_detect =digitalRead(sensor_foot); //อ่านค่าจากเซนเซอร์เท้า | |
| int glass_detect =digitalRead(sensor_glass);//อ่านค่าจากเซนเซอร์กระจกมองหลัง | |
| int steer_detect =digitalRead(sensor_steer);//อ่านค่าจากเซนเซอร์พวงมาลัย | |
| Serial.print(foot_detect); Serial.print(", "); //พิมพ์ค่าที่อ่านได้ คั่นด้วย , | |
| Serial.print(glass_detect); Serial.print(", "); | |
| Serial.print(steer_detect); Serial.print("\n"); | |
| //ถ้ามีการเจอสัญญาณเซนเซอร์ไหน ให้บันทึกเวลาไว้ | |
| if(foot_detect) { | |
| foot_ts = time_now; | |
| } | |
| if(glass_detect) { | |
| glass_ts = time_now; | |
| } | |
| if(steer_detect) { | |
| steer_ts = time_now; | |
| } | |
| //เช็คว่าเซนเซอร์ไหนที่ไม่เจอการเคลื่อนไหว และ มีระยะเวลานานกว่าเวลาที่บันทึกไว้เกิน 5 วินาที ให้ถือเป็นการไม่ขยับ | |
| //3000 คือระยะเวลาที่เซนเซอร์ให้สัญญาณ 1 ค้างไว้หลังจากเจอการเคลื่อนไหว จึงต้องถูกลบออกจากระยะเวลาที่บันทึกไว้ล่าสุด | |
| bool foot_not_move = foot_detect==0 && ((time_now-(foot_ts -3000)) >wait_time); | |
| bool glass_not_move = glass_detect==0 && ((time_now-(glass_ts-3000)) >wait_time); | |
| bool steer_not_move = steer_detect==0 && ((time_now-(steer_ts-3000)) >wait_time); | |
| //นำทั้ง 3 เซนเซอร์มาตรวจสอบว่า ถ้าไม่ขยับทั้ง 3 เซนเซอร์ ตัวแปร no_movement_detect จะเป็น 1 (true) | |
| bool no_movement_detect = foot_not_move && glass_not_move && steer_not_move; | |
| //เมื่อตัวแปร no_movement_detect เป็น 1 ให้ส่งสัญญาณ high ไปยัง buzzerเพื่อเปิดเสียง | |
| //ถ้า no_movement_detect เป็น 0 ให้ปิดเสียง | |
| if(no_movement_detect) { | |
| Serial.println("All not move."); | |
| digitalWrite(buzzer, HIGH); | |
| } else{ | |
| digitalWrite(buzzer, LOW); | |
| } | |
| //หน่วงเวลานิดหน่อยเพราะไม่จำเป็นต้องคำนวณเร็วเกินไป (100 ms) | |
| delay(100); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment