安安~

今天呢~要來做超音波模組啦!!

首先,根據之前查詢的結果,發現到中文的網站寫3 pin超音波模組的好像不多

因此今天就來玩一下啦~

那麼,來看看接線圖(其實這些官網都有喔~)

參考:https://www.arduino.cc/en/Tutorial/Ping

OK,然後來coding了~

其實我也是都看官網的啦(笑)

不過小弟在這邊幫大家把主要的一些變數抓出來,幫助大家修改ㄜ

const int pingPin = 7;//自由修改

void setup() 
{
    Serial.begin(9600);
}

void loop() 
{
  
  long duration, inches, cm;//自由修改

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds)
 {
     return microseconds / 74 / 2;
  }

long microsecondsToCentimeters(long microseconds) 
{
   return microseconds / 29 / 2;
 }

首先,預設腳位是第七腳,程式第一行就是了,自由修改

再來,cm那個變數,它會自動回傳一個數值,而這個數值,就是物體跟模組之間的距離囉!


剛好最近,有人問我如果要量身高怎麼辦

雖然我是第一次聽說有人用這東西量身高拉==""但是如果不考慮雜七雜八的問題,那我想其實只要

身高=地面總高-測量距離 就可以了

因此,我幫他寫的程式就如下(這也是一種應用型ㄜ)

const int pingPin = 7;

int height;

void setup() {
    Serial.begin(9600);
}

void loop() {

  long duration, inches, cm;
  height=(305-cm);//身高測量

  
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

 
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm, ");
  Serial.print("height=");
  Serial.print(height);
  
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds) {
 
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  
}

OK,搞定囉!

這樣數值出來就會像這樣

OK,那關於他的介紹就到此ㄜ

Bye!

arrow
arrow
    文章標籤
    超音波模組 arduino HC-SR03
    全站熱搜

    碼農日常 發表在 痞客邦 留言(1) 人氣()