修改原來的code, 找到驅動舵機的地方, 拿掉搖桿觸發的判斷, 作 MIN[] ~ MAX[] 角度之間的運動
舵機訊號接 5, Vcc 接 5V
#include <Servo.h>
const int SERVOS = 4;
int PIN[SERVOS],value[SERVOS],idle[SERVOS],currentAngle[SERVOS],MIN[SERVOS],MAX[SERVOS],INITANGLE[SERVOS];
Servo myservo[SERVOS];
int afrom[] = {90, 35, 135},ato[] = {20, 140, 110},amiddle[] = {20, 35, 135},afinal[] = {179,75,95},aafterfinal[] = {179,35,135};
int anaPins[]={A0,A1,A2,A3};
int DELAYTIME = 200;
boolean counterClock = false;
void setup() {
Serial.begin(9600);
PIN[0] = 5;MIN[0] = 5;MAX[0] = 175;INITANGLE[0] = 90;
PIN[1] = 6;MIN[1] = 35;MAX[1] = 179;INITANGLE[1] = 90;
PIN[2] = 7;MIN[2] = 90;MAX[2] = 179;INITANGLE[2] = 155;
PIN[3] = 8;MIN[3] = 0;MAX[3] = 179;INITANGLE[3] = 25;
for(int i = 0; i < SERVOS; i++){
pinMode(anaPins[i], INPUT);
myservo[i].attach(PIN[i]);
myservo[i].write(INITANGLE[i]);
value[i] = 0;
idle[i] = 0;
}
}
void doCruise(){
int thisServoId = 0;
currentAngle[thisServoId] = myservo[thisServoId].read();
if(currentAngle[thisServoId]>45)
counterClock = true;
while(true){
delay(40);
if(!counterClock){
if(currentAngle[thisServoId] < MAX[thisServoId]){
++currentAngle[thisServoId];
}
if(!myservo[thisServoId].attached()){
myservo[thisServoId].attach(PIN[thisServoId]);
}
myservo[thisServoId].write(currentAngle[thisServoId]);
if(currentAngle[thisServoId] == MAX[thisServoId])
counterClock = true;
}else{
if(currentAngle[thisServoId] > MIN[thisServoId])
--currentAngle[thisServoId];
if(!myservo[thisServoId].attached()){
myservo[thisServoId].attach(PIN[thisServoId]);
}
myservo[thisServoId].write(currentAngle[thisServoId]);
if(currentAngle[thisServoId] == MIN[thisServoId])
counterClock = false;
}
}
}
void loop(){
doCruise();
}