### 5.6 スマート給餌システム #### 5.6.1 給餌室のドア Arduino IDEで**5.6.1Servo**コードを開きます。 ```c #include //Import the library of servo Servo myservo; // create servo object to control a servo // 16 servo objects can be created on the ESP32 int pos = 0; // variable to store the servo position // Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33 int servoPin = 26; void setup() { Serial.begin(9600); myservo.attach(servoPin); // attaches the servo on pin 26 to the servo object myservo.write(180); delay(2000); } void loop() { for (pos = 80; pos <= 179; pos += 1) { // goes from 0 degrees to 80 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 180; pos >= 81; pos -= 1) { // goes from 80 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } ``` **ESP32 Dev Module**ボードと**COM**ポートを選択し、コードをアップロードします。 ![5458448](../media/5458448.png) **テスト結果:** 給餌室のドアがゆっくりと開き、その後閉じます。 **注記:** SG90サーボは180°回転できます。給餌ボックスが小さいため、100°の回転でボックスを完全に閉じるのに十分です。 80°: 全開 120°: 半開 180°: 閉 ![cou63](../media/cou63.gif) **注意** 指を箱に入れないでください。挟まれる危険があります! サーボを損傷する可能性があるため、ドアを物で塞がないでください! ドアはサーボによって制御されます。 **内部構造:** ![cou61](../media/cou61.png) ① 信号(S): マイクロコントローラからの制御信号を受信します。 ② ポテンショメータ: サーボのフィードバック部分。出力軸の位置を測定します。 ③ 組み込みボード(内部コントローラ): サーボのコア。外部制御信号と位置のフィードバック信号を処理し、サーボを駆動します。 ④ DCモーター: 実行部分。速度、トルク、位置を出力します。 ⑤ ギアシステム: モーターからの出力を、特定の伝達比に従って最終的な出力角度に調整します。 **サーボの駆動:** 信号(S)はPWMを受信してサーボの出力を制御し、出力軸の位置はPWMのデューティサイクルに直接依存します。 **例:** A. パルス幅1.5msの信号をサーボに送ると、その軸(ホーン)は中央位置(90°)に回転します。 B. パルス幅 = 0.5msの場合、軸は最小(0°)に回転します。 C. パルス幅 = 2.5msの場合、軸は最大(180°)に回転します。 **注記: 最大角度はサーボの種類によって異なります。170°のものもあれば、90°のものもあります。それにもかかわらず、サーボは通常、パルス幅1.5msの信号を受信すると、(最大角度の)半分動きます。** #### 5.6.2 超音波センサー ![cou65](../media/cou65.png) ![couy61](../media/couy61.png) Arduino IDEで**5.6.2 Ultrasonic-Sensor**コードを開きます。 ```c #define Trigpin 12 //connect trig to io12 #define Echopin 13 //connect echo to io13 int duration,distance; void setup(){ Serial.begin(9600); //Set the baud rate to 9600 pinMode(Trigpin,OUTPUT); //set trig pin to output mode pinMode(Echopin,INPUT); //set echo pin to input mode } void loop(){ digitalWrite(Trigpin,LOW); delayMicroseconds(2); digitalWrite(Trigpin,HIGH); delayMicroseconds(10); //Trigger the trig pin via a high level lasting at least 10us digitalWrite(Trigpin,LOW); duration = pulseIn(Echopin,HIGH); //the time of high level at echo pin distance = duration/58; //convert into distance(cm) delay(50); Serial.print("distance:"); //Serial monitor prints the value Serial.print(distance); Serial.println("cm"); } ``` **ESP32 Dev Module**ボードと**COM**ポートを選択し、コードをアップロードします。 ![5458448](../media/5458448.png) **テスト結果:** このキットでは、検出範囲は3〜8cm以内です。 シリアルモニターを開き、ボーレートを9600に設定すると、シリアルモニターに超音波モジュールと前方の障害物との距離が表示されます。 ![image-20250417140529545](../media/image-20250417140529545.png) #### 5.6.3 インテリジェント給餌システム Arduino IDEで**5.6.3Intelligent-Feeding-System**コードを開きます。 ```c #include //Import the library of servo on ESP32 board Servo myservo; // create servo object to control a servo // 16 servo objects can be created on the ESP32 #define TrigPin 12 //connect trig to D12 #define EchoPin 13 //connect echo to D13 #define ServoPin 26 int duration,distance; void setup(){ Serial.begin(9600); //Set the baud rate to 9600 pinMode(TrigPin,OUTPUT); //set trig pin to output mode pinMode(EchoPin,INPUT); //Set echo pin to input mode myservo.attach(ServoPin); // attaches the servo on pin 26 to the servo object } void loop(){ Serial.println(getDistance()); //When the distance is detected within 2~7cm, open the feeding box. Or else, close. if (getDistance() >= 2 && 7 >= getDistance()) { //Servo rotates to 80° to open the box myservo.write(80); delay(500); } else{ myservo.write(180); delay(500); } } //Put the gotten distance in a function float getDistance() { digitalWrite(TrigPin,LOW); delayMicroseconds(2); digitalWrite(TrigPin,HIGH); delayMicroseconds(10); //Trigger the trig pin via a high level lasting at least 10us digitalWrite(TrigPin,LOW); duration = pulseIn(EchoPin,HIGH); //the time of high level at echo pin distance = duration/58; //convert into distance(cm) delay(50); return distance; } ``` **ESP32 Dev Module**ボードと**COM**ポートを選択し、コードをアップロードします。 ![5458448](../media/5458448.png) **テスト結果:** スマート給餌システムは、超音波モジュールとサーボを介して家禽にインテリジェントに給餌します。前者は動物までの距離を検出し、後者は給餌ボックスの開閉を制御します。ペットがボックスに近づいていることを検出すると、サーボがボックスを開いて給餌します。 **注意** 指を箱に入れないでください。挟まれる危険があります! サーボを損傷する可能性があるため、ドアを物で塞がないでください! ![flo6](../media/flo6.png)