Skip to content

Instantly share code, notes, and snippets.

@penpencool
Created November 2, 2023 13:05
Show Gist options
  • Select an option

  • Save penpencool/46ad79e7ace2705da1376f8244703009 to your computer and use it in GitHub Desktop.

Select an option

Save penpencool/46ad79e7ace2705da1376f8244703009 to your computer and use it in GitHub Desktop.
const int sensorPin = 2; // ต่อเข้ากับขาของ Infrared Barrier Tracking Module
int sensorStatus = 0; // ตัวแปรเก็บสถานะของเซนเซอร์
void setup() {
pinMode(sensorPin, INPUT); // กำหนดขา sensorPin เป็น INPUT
Serial.begin(9600); // เริ่มการสื่อสารผ่าน Serial ที่ความเร็ว 9600 bps
}
void loop() {
sensorStatus = digitalRead(sensorPin); // อ่านค่าจากเซนเซอร์
if (sensorStatus == LOW) { // บางครั้งเซนเซอร์จะให้ค่าเป็น LOW เมื่อตรวจจับวัตถุ
Serial.println("Object Detected!"); // แสดงข้อความเมื่อตรวจจับวัตถุ
} else {
Serial.println("No Object Detected.");
}
delay(500); // หน่วงเวลา 0.5 วินาที
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment