#include <Servo.h> // 加载舵机库
Servo myservo; // 创建舵机对象

void setup() {
  myservo.attach(9); // 将舵机信号线连接到数字9引脚
  Serial.begin(9600);// 初始化串口，设置波特率为9600
  // myservo.write(0);
}

void loop() {
  for (int pos = 80; pos <= 100; pos += 1) { // 从0°转到180°
    myservo.write(pos); // 设置舵机角度
    delay(15); // 等待15毫秒
  }
  for (int pos = 100; pos >= 80; pos -= 1) { 
    myservo.write(pos); // 设置舵机角度
    delay(15); // 等待15毫秒
  }

  if (Serial.available() > 0) {
    // 读取数据
    char received = Serial.read();
    // 输出数据
    Serial.print("Received: ");
    Serial.println(received);

    if (received == '1'){
      myservo.write(0);
      for ( int i = 0;i <= 100;i += 1){
        delay(10); 
      }
    }
    if (received == '2') {
      myservo.write(180);
      for ( int i = 0;i <= 100;i += 1){
        delay(10); 
      }
    }
  }
}
