2022年6月25日 星期六

ArduinoESP32藍芽舵機安裝Arduino可愛的舵機

舵機安裝比步進馬達好裝很多,主要是無需驅動板,可以利用開發板直供電。 

程式碼也很好懂。 所以又裝上藍芽模組,也不會太難。 

 

 



============================================================== 

#include <Servo.h>//宣告
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
Servo servo;//命名
char receivedChar;// received value will be stored as CHAR in this variable
int pos = 0;

void setup() {
  SerialBT.begin("ESP32_Technical_Tamizha"); //You can change your Bluetooth
    Serial.begin(115200);//序列阜監控視窗
    servo.attach(16); //橘色的線接主要是訊號線,接到 p16
}

void loop() {
receivedChar =(char)SerialBT.read();
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
   if (SerialBT.available()) {
        Serial.print ("Received:");//print on serial monitor
    Serial.println(receivedChar);//print on serial monitor
switch(receivedChar) {                            //   
case 'L':
  //1~9
for(pos = 0; pos < 180; pos += 1) // 一度一度由 0 度旋轉到 180 度

servo.write(pos);
delay(15);
break;

case 'R': // 1~9
for(pos = 180; pos>=1; pos-=1) // 一度一度由 180 度旋轉到 0 度

servo.write(pos);
delay(15);
break;
}
}
}