|
以下是小弟我操控藍牙遙控車與伺服馬達控制鏡頭的程式
一開始程式碼我只有寫藍芽控制車子移動,可以很順暢;可是加上了伺服馬達的程式碼之後整個遙控出了問題,伺服馬達反過來可以正常作用,遙控車卻無法移動。跪求大大們協助
#include<SoftwareSerial.h>
SoftwareSerial BT(0,1);
#include <Servo.h>
Servo myservo;
Servo myservo1;
char command;
const byte IN1 = 10;
const byte IN2 = 9;
const byte IN3 = 8;
const byte IN4 = 7;
const byte ENA = 6;
const byte ENB = 5;
int speeds = 0;
void loop() {
if(BT.available()>0){
command = BT.read();
switch(command){
case 'q':
lighton();
break;
case 'p':
lightdown();
break;
case 'w':
forward();
break;
case'x':
backward();
break;
case'a':
turnLeft();
break;
case'd':
turnRight();
break;
case's':
stop();
break;
case 'b':
Speedup();
break;
case 'c':
Speeddown();
break;
case 'r':
cameraright();
break;
case 't':
cameraleft();
break;
case'u':
cameraup();
break;
case'o':
cameradown();
break;
case'z':
camerarlreturn();
break;
case'y':
cameraudreturn();
break;
case'h':
camerareturn();
break;
}
}
}
void lighton(){
analogWrite(13,255);
}
void lightdown(){
analogWrite(13,0);
}
void CarAction(){
analogWrite(ENA,speeds);
analogWrite(ENB,speeds);
Serial.print("speed=");
Serial.println(speeds);
}
void stop() {
speeds=0;
analogWrite(IN1,0);
analogWrite(IN2,0);
analogWrite(IN3,0);
analogWrite(IN4,0);
}
void Speedup(){
speeds+=20;
if(speeds>250){speeds=255;}
CarAction();
}
void Speeddown(){
speeds-=20;
if(speeds<=100){speeds=100;}
CarAction();
}
void forward() {
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
}
void backward() {
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
}
void turnLeft() {
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
}
void turnRight() {
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
}
void cameraleft(){
for(int i = 500; i <= 2400; i+=100)
myservo.writeMicroseconds(i);
}
void cameraright() {
for(int i = 2400; i >= 500; i-=100)
myservo.writeMicroseconds(i);
}
void cameraup(){
for(int i = 500; i <= 2400; i+=100)
myservo1.writeMicroseconds(i);
}
void cameradown(){
for(int i = 2400; i >= 500; i-=100)
myservo1.writeMicroseconds(i);
}
void camerarlreturn(){
myservo.write(90);
}
void cameraudreturn(){
myservo1.write(90);
}
void camerareturn(){
myservo.write(90);
myservo1.write(90);
}
void setup() {
myservo.attach(11, 500, 2400);
myservo1.attach(12, 500, 2400);
myservo.write(90);
myservo1.write(90);
BT.begin(9600);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
pinMode(13,OUTPUT);
stop();
} |
|