自定义函数的问题

该代码示例展示了如何使用Arduino控制四个伺服电机,分别代表机器人手臂的基座、右臂、左臂和爪子。在`setup()`函数中初始化伺服电机,并设置其最大和最小角度限制。`loop()`函数监听串口输入,根据接收到的字符调用`armDataCmd()`函数来移动相应部位。当接收到特定字符时,`reportStatus()`函数会打印当前所有伺服电机的角度位置。然而,代码中可能存在一个错误,`reportStatus()`函数未能正常调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


```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()函数无法正常调用?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值