Diorama Realism: Automated Rail Crossing
ระบบทางรถไฟอัตโนมัติ (ที่นักสร้างอย่าง Sheekar Banerjee เคยทำไว้) จะเปลี่ยนโมเดลรถไฟที่อยู่นิ่งๆ ให้กลายเป็นโลกจำลองที่ตอบสนองได้สมจริงสุดๆ มันผสานการควบคุมเซอร์โวเข้ากับตรรกะการกะพริบไฟ LED แบบไม่บล็อกการทำงาน เพื่อเลียนแบบสัญญาณเตือนตามสถานีจริงได้อย่างเนี๊ยบ!
วงจรเบื้องต้น:
Arduino ต่อกับเซ็นเซอร์อัลตราโซนิก (HC-SR04) Arduino -----> HC-SR04 Vin -----> Vcc GND -----> GND ขา 2 -----> trigPin ขา 3 -----> echoPin
Arduino ต่อกับ Servo Motor Arduino -----> Servo Motor Vin -----> สายแดง (+) GND -----> สายดำหรือน้ำตาล (-) ขา Digital pin 9 -----> สายเหลือง/ส้ม (data)
ลงลึกเรื่องเทคนิค
การกะพริบไฟแบบ Non-Blocking (ไม่บล็อก)
ส่วนที่ยากที่สุดของโปรเจคนี้คือ ไฟสัญญาณเตือน นี่แหละ
- ทางรถไฟจริงจะมีไฟสีแดงสองดวงที่กะพริบสลับกัน: ซ้าย, ขวา, ซ้าย, ขวา
- ถ้าน้องเขียนโค้ดด้วย
delay(500);ตัว Arduino จะหยุดทำงานทั้งหมด มันจะฟังค่าจากเซ็นเซอร์ไม่ได้ว่ารถไฟผ่านไปหรือยัง! - น้องต้องใช้โครงสร้างตรรกะของ
millis()เทคนิค "Blink Without Delay" นั่นแหละ if (currentMillis - previousMillis >= 500) { toggleLights(); }- วิธีนี้ทำให้ Arduino กะพริบไฟ LED สัญญาณอันใหญ่โตได้เนียนๆ พร้อมกับคำนวณความเร็วรถไฟที่กำลังเข้ามาได้อีกด้วย
การควบคุมบาร์เกต (Boom Gates)
- ตอนรถไฟมา: เราเอาเซ็นเซอร์ TCRT5000 IR Sensor ฝังไว้ใต้รางรถไฟ ห่างจากทางข้ามประมาณ 3 ฟุต
- Arduino ตรวจจับรถไฟได้ มันจะเริ่มลำดับการกะพริบไฟ LED แบบไม่บล็อกทันที
- มันจะสั่งให้ SG90 Micro Servos (ซ่อนไว้ใต้ถนน) 2 ตัว ค่อยๆ หมุน 90 องศา เพื่อปล่อยบาร์เกตลงมา
- ตอนรถไฟออก: เราเอาเซ็นเซอร์ตัวที่สองฝังไว้ห่างจากทางข้าม 3 ฟุต ด้านหลัง พอรถไฟส่วนท้ายผ่านเซ็นเซอร์ตัวที่สอง Arduino จะยกบาร์เกตขึ้นและปิดไฟสัญญาณเตือน!
รายการอุปกรณ์สำหรับจัดฉาก
- Arduino Uno/Nano: ศูนย์บัญชาการใหญ่
- IR Obstacle Sensors (x2) หรือ Light Dependent Resistors.
- Micro Servos (SG90 x2).
- LED สีแดงและตัวต้านทาน 220 โอห์ม (x4) สำหรับติดตั้งในเสาไฟสัญญาณที่ปริ้นจาก 3D
- MP3 Sound Module (ตัวเลือกเสริม: เอาไว้เปิดเสียงกระดิ่ง "ติ๊ง ติ๊ง ติ๊ง" ดังๆ จาก micro-SD card!)
โค้ด:
//the code is entirely created by: SHEEKAR BANERJEE (at December 2017)
//Dept. of CSE, IUBAT
//AI-ML-IOT Solution Engineer and Researcher
#include<Servo.h> //This calls the Header of the repository containing the library files of Servo Motor Driver Modules
#define trigPin 2 //This initializes the Trig pin of HC-05 Ultrasonic Sensor to the pin no. 2 of Arduino Microcontroller
#define echoPin 3 //This initializes the Echo pin of HC-05 Ultrasonic Sensor to the pin no. 3 of Arduino Microcontroller
Servo sm; //Servo is a Class which is providing an independent data type // sm is a variable which is representing the servo motor
int sound=250;
void setup() {
Serial.begin(9600); //Streaming 9600 bits of sensor data per second
pinMode(trigPin,OUTPUT); //Here, Trig pin triggers the electrical impulses into the sensor as an output
pinMode(echoPin,INPUT); //Here, Echo pin inputs the reflective signals into the sensor
sm.attach(9); //Servo motor data pin is attached to Arduino Digital Pin 3
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration=pulseIn(echoPin, HIGH);
distance=(duration/2)/29.1; //Calculation of distance
if(distance<10)
{
sm.write(80);
}
else {
sm.write(0);
}
}
ผลลัพธ์:
ระบบทางรถไฟอัตโนมัติ | Sheekar Banerjee