科技執法測速機_用兩組超音波模組設計arduino測速機 用8X8LED顯示速度

用兩組超音波模組設計arduino測速機 用8X8LED顯示速度
                                                                60x   ----   ----




創作理念
        本作品以「科技融入生活」為出發點,結合兩組超音波模組與 Arduino 微控制器,透過時間差計算物體移動速度,並以 8×8 LED 矩陣即時顯示結果。設計強調互動性與教育性,讓抽象的物理概念以可視化方式呈現,提升學習興趣與理解深度。作品不僅展現電子與程式整合的應用能力,也象徵創客精神——以簡單元件創造具功能與美感的科技作品。透過光影與數據的交融,讓速度不再只是公式,而是能被「看見」的動態體驗。


使用模組 :



程式碼:


 #include <LedControl.h>


// LED Matrix (DIN=2, CS=3, CLK=4)

LedControl lc = LedControl(2, 4, 3, 1);


const int trigA = 7, echoA = 5;

const int trigB = 6, echoB = 4;


float distanceAB = 20.0; // cm,兩模組間距


void setup() {

  Serial.begin(9600);

  lc.shutdown(0,false);

  lc.setIntensity(0,8);

  lc.clearDisplay(0);


  pinMode(trigA, OUTPUT);

  pinMode(echoA, INPUT);

  pinMode(trigB, OUTPUT);

  pinMode(echoB, INPUT);

}


float getDistance(int trigPin, int echoPin) {

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);

  return duration * 0.034 / 2; // cm

}


void loop() {

  // 偵測物體通過 A

  float dA = getDistance(trigA, echoA);

  if (dA < 10) { // 物體靠近

    unsigned long t1 = millis();


    // 等待物體通過 B

    while (true) {

      float dB = getDistance(trigB, echoB);

      if (dB < 10) {

        unsigned long t2 = millis();

        float speed = distanceAB / ((t2 - t1) / 1000.0); // cm/s

        Serial.print("Speed: ");

        Serial.print(speed);

        Serial.println(" cm/s");


        // 顯示速度 (整數部分)

        int spdInt = (int)speed;

        lc.clearDisplay(0);

        lc.setDigit(0, 0, spdInt % 10, false);

        lc.setDigit(0, 1, (spdInt / 10) % 10, false);

        break;

      }

    }

  }

}


接線說明:

🔌 HC-SR04 超音波模組 A

  • VCC → 5V

  • GND → GND

  • Trig → D7

  • Echo → D5

🔌 HC-SR04 超音波模組 B

  • VCC → 5V

  • GND → GND

  • Trig → D6

  • Echo → D4

💡 MAX7219 8x8 LED 矩陣

  • VCC → 5V

  • GND → GND

  • DIN → D2

  • CS → D3

  • CLK → D8


留言