กลับไปหน้ารวมไฟล์
arduino-pedestrian-traffic-lights-millis-fsm.md

Arduino Pedestrian Crossing Traffic Lights

Chronological Sequencing: The Traffic Light FSM

การสร้างลำดับไฟจราจรโดยใช้ delay(1000) เป็นการออกแบบเมืองที่ผิดพลาดอย่างร้ายแรง! หากคนเดินเท้ากดปุ่ม "Walk" อย่างรุนแรงในขณะที่ Arduino กำลังติดอยู่ใน delay() ขนาดใหญ่ 15 วินาทีที่ค้างไฟ Green ไว้, โปรเซสเซอร์จะละเลยพวกเขาโดยสิ้นเชิง และพวกเขาจะถูกบังคับให้รอจนกว่า loop จะรีเซ็ต! โปรเจกต์ Pedestrian Crossing Traffic Lights นี้ได้กำจัด delay() ออกจากการทำงานหลักอย่างสิ้นเชิง ด้วยการใช้ millis() ภายใน Finite State Machine (FSM) ที่เข้มงวด, Arduino จะตรวจสอบทั้ง Traffic Light Clock และสถานะของ Pedestrian Button ทางกายภาพในทันทีพร้อมกัน 100%!

potentiometer_led_rgb_color_theory_1772681525046.png

The Non-Blocking millis() Execution Core

ฟังก์ชัน delay() ทำให้การทำงานหลักของ silicon เป็นอัมพาตอย่างรุนแรง millis() ทำหน้าที่เป็นนาฬิกาจับเวลาที่เดินเงียบๆ อยู่เบื้องหลัง

  1. Arduino ตรวจสอบนาฬิกาจับเวลามากกว่าหนึ่งล้านครั้งต่อวินาทีโดยไม่หยุด!
  2. if (currentTime - previousTime >= 5000) { Yellow Light! }
  3. แต่ในขณะเดียวกัน ภายใน loop เดียวกัน มันจะตรวจสอบ if (digitalRead(BUTTON) == LOW) อย่างรวดเร็ว!
unsigned long previousMillis = 0;
const long greenTime = 10000; // 10 seconds of traffic flow natively.

boolean pedestrianWaiting = false;

void loop() {
  unsigned long currentMillis = millis(); // Check the watch!

  // The brutal BUTTON INTERCEPTOR! Never blocking!
  if (digitalRead(pedButton) == LOW) {
    pedestrianWaiting = true; // Flag the system!
  }

  // If the Green time is fully organically up... OR a pedestrian forced an early interrupt!
  if (currentMillis - previousMillis >= greenTime || (pedestrianWaiting && currentMillis - previousMillis >= 3000)) {
    // If they walk-button was pressed, immediately force Yellow light after 3 baseline seconds!
    triggerYellowProtocol();
    triggerPedestrianWalk(); // Safely halt traffic entirely!

    pedestrianWaiting = false; // Reset the flag cleanly!
    previousMillis = currentMillis; // Reset the stopwatch!
  }
}

Simulating Real-World Hardware Interfaces

ระบบไฟจราจรจริงทำงานด้วย 240VAC โดยใช้ contactors ขนาดใหญ่

  • เพื่อจำลอง array ขนาดเล็กนี้ให้ถูกต้อง, Arduino จะขับเคลื่อน 5mm LEDs โดยตรง
  • Resistor Mandatory Matrix: LED ทุกตัว (Red, Yellow, Green, Ped-Red, Ped-Green) จะต้องมี 220-Ohm resistor ของตัวเองต่ออนุกรมกันอย่างเคร่งครัด!
  • ห้ามต่อขา negative ของ LED ทั้งหมดเข้ากับ resistor ตัวเดียวกันเด็ดขาด เนื่องจาก Red ต้องการประมาณ 2V และ Green ต้องการประมาณ 3.2V การต่อรวมกันจะทำให้เกิดปัญหาทางไฟฟ้าอย่างรุนแรง ลดความสว่างลงอย่างมาก และอาจทำให้ semiconductor doping layer ที่ละเอียดอ่อนของ Green LED เสียหายอย่างคาดไม่ถึง!

Civil Infrastructure Modeling Toolset

  • Arduino Uno/Nano (จัดการ FSM แบบ multitasking ความเร็วสูงได้โดยตรง!)
  • Large Tactile Arcade Button (เพื่อจำลองคนเดินเท้าที่ไม่อดทนซึ่งต้องการ Walk symbol ของพวกเขาอย่างชัดเจน)
  • LED Array (Red, Yellow, Green) x2 (ชุดหนึ่งสำหรับ Car Traffic โดยเฉพาะ อีกชุดหนึ่ง [Red/Green] สำหรับ Pedestrians โดยเฉพาะ)
  • Multiple 220-Ohm / 330-Ohm Resistors (แยกแรงดันตกคร่อมของสีแต่ละสีได้อย่างสมบูรณ์แบบ)
  • A Deep Understanding of FSM Logic Matrices (โปรเจกต์นี้สอนแนวคิดการเขียนโปรแกรมฝังตัวด้วย C++ ที่สำคัญที่สุด นั่นคือ "State Machines" โดยไม่มีฟังก์ชัน blocking!)

ข้อมูล Frontmatter ดั้งเดิม

title: "Arduino Pedestrian Crossing Traffic Lights"
description: "Urban traffic sequencing! Synthesize complex chronological state machines perfectly mimicking municipal junction algorithms, utilizing non-blocking hardware `millis()` arrays to prioritize pedestrian tactile interruptions across heavy vehicular LED flows."
category: "Educational"
difficulty: "Beginner"