```c
#include<Servo.h>
Servo base,fArm,rArm,claw;/*creat the subject*/
const int baseMin = 0;/*set the maximum and minimum values*/
const int baseMax = 180;
const int rArmMin = 20;
const int rArmMax = 180;
const int fArmMin = 40;
const int fArmMax = 150;
const int clawMin = 120;
const int clawMax = 160;
int DSD = 15;/*Default Servo Delay*/
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("*****MeArm*****");
base.attach(10);/*initialization*/
rArm.attach(11);
fArm.attach(9);
claw.attach(5);
base.write(90);/*the initial state*/
rArm.write(100);
fArm.write(100);
claw.write(160);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0){
char serialCmd = Serial.read();
armDataCmd(serialCmd);
}
}
void armDataCmd(char servoCmd){
int degree = Serial.parseInt();
if(servoCmd == 'b'||servoCmd == 'c'||servoCmd == 'r'||servoCmd == 'f'){
moveCmd(servoCmd,degree,DSD);
}else{
switch(servoCmd){
case'o':
reportStatus();
break;
default:
Serial.println("unknown instructions");
return;
}
}
}/*creat a function which can control the servo*/
void moveCmd(char servoName,int toPos,int servoDelay){
Servo sub;
switch(servoName){
case'b':
if(baseMin<=toPos&&toPos<=baseMax){
sub = base;
break;
}else{
Serial.println("***OUT OF THE RANGE***");
return;
}
case'r':
if(rArmMin<=toPos&&toPos<=rArmMax){
sub = rArm;
break;
}else{
Serial.println("***OUT OF THE RANGE***");
return;
}
case'f':
if(fArmMin<=toPos&&toPos<=fArmMax){
sub = fArm;
break;
}else{
Serial.println("***OUT OF THE RANGE***");
return;
}
case'c':
if(clawMin<=toPos&&toPos<=clawMax){
sub = claw;
break;
}else{
Serial.println("***OUT OF THE RANGE***");
return;
int fromPos = sub.read();
if(fromPos >= toPos){
for(int i = fromPos;i>=toPos;i--){
sub.write(i);
delay(servoDelay);
}
}else{
for(int i = fromPos; i <=toPos;i++){
sub.write(i);
delay(servoDelay);
}
}
}
}
void reportStatus(){
Serial.println("");
Serial.println("");
Serial.println("+ Robot-Arm Status Report +");
Serial.print("Claw Position: "); Serial.println(claw.read());
Serial.print("Base Position: "); Serial.println(base.read());
Serial.print("Rear Arm Position:"); Serial.println(rArm.read());
Serial.print("Front Arm Position:"); Serial.println(fArm.read());
Serial.println("++++++++++++++++++++++++++");
Serial.println("");
}
为什么上面的reportStatus()函数无法正常调用?