15275285755 发表于 2014-12-15 17:25:17

蓝牙控制舵机

要做一个项目,用一个蓝牙模块控制3个舵机做不同的运转,求大神帮忙
#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;

int MotorRight1=2;
int MotorRight2=4;
int MotorLeft1=7;
int MotorLeft2=8;

char val0;
char val1;
char val2;
char val3;

void setup()
{
Serial.begin(9600);
pinMode(MotorRight1, OUTPUT);// 腳位2 (PWM)
pinMode(MotorRight2, OUTPUT);// 腳位 4 (PWM)
pinMode(MotorLeft1,OUTPUT);// 腳位 7 (PWM)
pinMode(MotorLeft2,OUTPUT);// 腳位 8 (PWM)
myservo1.attach(3);
myservo2.attach(5);
myservo3.attach(6);


}

void go()// 前進
{
      digitalWrite(MotorRight1,LOW);
      digitalWrite(MotorRight2,HIGH);
      digitalWrite(MotorLeft1,LOW);
      digitalWrite(MotorLeft2,HIGH);
      
}

void left() //右轉
{
      digitalWrite(MotorRight1,HIGH);
      digitalWrite(MotorRight2,LOW);
      digitalWrite(MotorLeft1,LOW);
      digitalWrite(MotorLeft2,HIGH);

}
void right() //左轉
{
      digitalWrite(MotorRight1,LOW);
      digitalWrite(MotorRight2,HIGH);
      digitalWrite(MotorLeft1,HIGH);
      digitalWrite(MotorLeft2,LOW);
   
}
void stop() //停止
{
   digitalWrite(MotorRight1,LOW);
   digitalWrite(MotorRight2,LOW);
   digitalWrite(MotorLeft1,LOW);
   digitalWrite(MotorLeft2,LOW);
   
}
void back() //後退
{
      digitalWrite(MotorRight1,HIGH);
      digitalWrite(MotorRight2,LOW);
      digitalWrite(MotorLeft1,HIGH);
      digitalWrite(MotorLeft2,LOW);;
      
}
      
void loop()
{
char val0 = Serial.read();
Serial.write(val0);
if (-1 != val0) {
    if ('W' == val0)
    go();
    else if ('A' ==val0)
    left();
    else if ('D' == val0)
    right();
    else if ('S' == val0)
    back();
    else if ('Q' == val0)
      stop();
    delay(500);
    }
else
{
    //stop();
    delay(500);
}
{                                                   // 每次步进一度
    myservo1.write(val1);      // 指定舵机转向的角度
    delay(15);                     // 等待15ms让舵机到达指定位置
}
{                                                   // 每次步进一度
    myservo2.write(val2);      // 指定舵机转向的角度
    delay(15);                     // 等待15ms让舵机到达指定位置
}
{                                                   // 每次步进一度
    myservo3.write(val3);      // 指定舵机转向的角度
    delay(15);                     // 等待15ms让舵机到达指定位置
}

}

绿林网页 发表于 2014-12-16 20:17:40

这小舵机的机械臂,能支撑自身不,能拿东西么

15275285755 发表于 2014-12-17 22:39:22

绿林网页 发表于 2014-12-16 20:17 static/image/common/back.gif
这小舵机的机械臂,能支撑自身不,能拿东西么

只是用来做个模型,不是真正做什么实验

天天向上/tp 发表于 2015-1-7 18:27:02

哈哈。这个是个不错的主意!!!!!
页: [1]
查看完整版本: 蓝牙控制舵机