|
|
要做一个项目,用一个蓝牙模块控制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让舵机到达指定位置
- }
-
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|