寫程式43.jpg

💽影片名稱:[Arduino寫程式] 第43集 - 超音波控制 距離偵測

🎬集數:43集🎤背景音樂:Janji - Heroes Tonight (feat. Johnning) [NCS Release]🎤背景音樂:Jim Yosef - Firefly [NCS Release]💕若有問題歡迎在下方留言💬讓我知道~😋


🌐(前往YouTube觀看)-[ https://youtu.be/nMRHS6xkI9s?feature=shared ]

🌐(YouTube播放清單)-[ https://youtube.com/playlist?list=PLOrdAySpFIQonYKEWANFiVrICDQY8R0_1&feature=shared ]


🌐程式創造++ 頻道:[ https://www.youtube.com/@CCplus2561 ]🌐Arduino程式語法網頁:[ https://www.86duino.com/?page_id=2255&lang=TW ]


[電路圖]

寫程式43 - 超音波控制 距離探測 電路圖.png

▲ 電路圖@CCplus2561 提供

 


[程式碼]

EX01: 顯示探測距離

#define t 3
#define r 2

void setup(){
   Serial.begin(9600);
   pinMode( t, OUTPUT);
   pinMode( r, INPUT);
}

void loop(){
   int a,b;                      //變數
   digitalWrite(t, HIGH);
   delayMicroseconds(1000);      //微秒
   digitalWrite(t, LOW);
   a = pulseIn(r, HIGH);         //計算出現時間
   b = (a*0.034/2) ;             //計算距離
   Serial.println ("距離為 = ");
   Serial.println (b);
   Serial.println (" cm");
   delay(500);
}


EX02: (不斷行) 顯示探測距離

#define t 3
#define r 2

void setup(){
   Serial.begin(9600);
   pinMode( t, OUTPUT);
   pinMode( r, INPUT);
}

void loop(){
   int a,b;                      //變數
   digitalWrite(t, HIGH);
   delayMicroseconds(1000);      //微秒
   digitalWrite(t, LOW);
   a = pulseIn(r, HIGH);         //計算出現時間
   b = (a*0.034/2) ;             //計算距離
   Serial.print ("距離為 = ");
   Serial.print (b);
   Serial.println (" cm");
   delay(500);
}

創作者介紹
創作者 Code Create++ 的頭像
程式創造++

Code Create++

程式創造++ 發表在 痞客邦 留言(0) 人氣( 0 )