蓝牙模块2

#include <Servo.h>    // 声明调用Servo.h库
Servo myservo;        // 创建一个舵机对象
int pos = 0;          // 变量pos用来存储舵机位置
void setup() { 
    myservo.attach(9);  // 将引脚9上的舵机与声明的舵机对象连接起来
      // 设置波特率为 
  Serial.begin(9600);
} 
void loop() { 
     while(Serial.available())
  {
    char c=Serial.read();
    Serial.println(c);
    if(c=='0')
     {
        Serial.println("BT is ready!");
        Serial.write("Serial--13--high");
        for(pos = 0; pos < 180; pos += 1){    // 舵机从0°转到180°,每次增加1°          
        myservo.write(pos);           // 给舵机写入角度   
        delay(15);                    // 延时15ms让舵机转到指定位置
        }
      }
     if(c=='1')
     {
       Serial.write("Serial--13--low");
       digitalWrite(0, LOW);
       for(pos = 180; pos>=1; pos-=1) {    // 舵机从180°转回到0°,每次减小1°                               
       myservo.write(pos);        // 写角度到舵机     
       delay(15);                 // 延时15ms让舵机转到指定位置
          }
     }
  }
}

猜你喜欢

转载自www.cnblogs.com/Sonny-xby/p/11083254.html