---
# System prepended metadata

title: 第十週紀錄

---



## Arduino學習紀錄
超音波測距
```java =
float ultrasonic_distance_cm(int trigPin, int echoPin) {
  digitalWrite(trigPin, LOW);
  digitalWrite(echoPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  unsigned long sonic_duration = pulseIn(echoPin, HIGH);
  float distance_cm = (sonic_duration / 2.0) / 29.1;

  return distance_cm;
}

void setup() {
  Serial.begin(9600);
  pinMode(12, OUTPUT); // trigPin
  pinMode(13, INPUT);  // echoPin
}

void loop() {
  Serial.print("distance = ");
  Serial.println(ultrasonic_distance_cm(12, 13));
  delay(1000); // 1秒的延遲
}

```


---
**伺服馬達控制**
（似乎是因為電流不夠？旋轉的時候會震動）
```java = 
#include <Servo.h>

Servo servo_2;

void setup() {

  servo_2.attach(5);
  servo_2.write(90);
  delayMicroseconds(3000);

}

void loop() {
  servo_2.write(10);
  delayMicroseconds(1000);
  servo_2.write(90);
  delayMicroseconds(1000);
  servo_2.write(170);
  delayMicroseconds(1000);
  servo_2.write(90);
  delayMicroseconds(1000);

}
```
---
**LCD** 
(待整理)
```java = 
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2); // 設置LCD地址和顯示尺寸

void setup() {
  Wire.begin();
  lcd.init();                      // 初始化LCD
  lcd.backlight();                 // 打開LCD背光
  Serial.begin(9600);
  Serial.println("LCD Display Example");
  
  // Display your text on the LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Hello, World!");
}

void loop() {
  // Your loop code here, if needed
  
  delay(5000); // Delay for 5 seconds (or adjust as needed)
}
```
* 要記得下載一些沒有的library

## 小挑戰
待更新


## 專案進度 
幫我們專案的作品進行了一些角色設定！


### **想要交朋友的社恐小怪獸**

* 環境太安靜的時候會碎碎念
* 環境太大聲的時候會震動大叫
* 會根據情境改變表情

