无法将 3 个程序合并为一个用于 Arduino 机器人项目

Trouble merging 3 programs into one for a Arduino robot project

我目前正在构建一个具有 3 种功能的机器人,到目前为止,这 3 种功能有 3 个不同的程序:

  1. 程序一允许机器人检测光表面上的黑线并跟随它。
  2. 程序 2 允许用户按下红外遥控器上的按钮,然后机器人将旋转移动手臂的伺服系统
  3. 程序 3 允许用户按下另一个按钮来播放音频文件

注意:所有这些程序都可以正常工作并且经过单独测试

我将程序 1 和 2 合并在一起没问题,并在机器人上进行了测试,它运行正常。当我合并第三个程序时出现错误:'rotateMotor' 不在此范围内。在我看来,它肯定在范围内。从我看的时候。经过一些故障排除后,我确实发现删除行 const unsigned char voice1[] PROGMEM = {audio file here} 和 #include 使程序再次正常运行。任何人都可以在这里帮忙吗?

#include <IRremote.h> //include IR remote library
#include <Servo.h>
#include <PCM.h>

const unsigned char voice1[] PROGMEM = {
  audio file here
  };
  
#define IR_SENSOR_RIGHT 11
#define IR_SENSOR_LEFT 12
#define MOTOR_SPEED 165

int IRpin = 11;  // pin for the IR sensor
uint32_t Previous;
IRrecv irrecv(IRpin); //IR sensor gets IR pin input to arduino
decode_results results; //results of IR remote button press
Servo armservo; //create servo objects

//Right motor
int enableRightMotor = 6;
int rightMotorPin1 = 7;
int rightMotorPin2 = 8;

//Left motor
int enableLeftMotor = 5;
int leftMotorPin1 = 9;
int leftMotorPin2 = 10;

void setup()
{
  Serial.begin(9600); //begin IR function
 irrecv.enableIRIn(); // Start receiver
 armservo.attach(9);  // attaches the servo to pin on arduino
 pinMode (12,OUTPUT);
 TCCR0B = TCCR0B & B11111000 | B00000011 ;
  
  // put your setup code here, to run once:
  pinMode(enableRightMotor, OUTPUT);
  pinMode(rightMotorPin1, OUTPUT);
  pinMode(rightMotorPin2, OUTPUT);
  
  pinMode(enableLeftMotor, OUTPUT);
  pinMode(leftMotorPin1, OUTPUT);
  pinMode(leftMotorPin2, OUTPUT);

  pinMode(IR_SENSOR_RIGHT, INPUT);
  pinMode(IR_SENSOR_LEFT, INPUT);
  rotateMotor(0,0);   
}


void loop()
{

  int rightIRSensorValue = digitalRead(IR_SENSOR_RIGHT);
  int leftIRSensorValue = digitalRead(IR_SENSOR_LEFT);

  //If none of the sensors detects black line, then go straight
  if (rightIRSensorValue == LOW && leftIRSensorValue == LOW)
  {
    rotateMotor(MOTOR_SPEED, MOTOR_SPEED);
  }
  
  //If right sensor detects black line, then turn right
  else if (rightIRSensorValue == HIGH && leftIRSensorValue == LOW )
  {
      rotateMotor(-MOTOR_SPEED, MOTOR_SPEED); 
  }
  
  //If left sensor detects black line, then turn left  
  else if (rightIRSensorValue == LOW && leftIRSensorValue == HIGH )
  {
      rotateMotor(MOTOR_SPEED, -MOTOR_SPEED); 
  } 
  
  //If both the sensors detect black line, then stop 
  else 
  {
    rotateMotor(0, 0);
  }

  if (irrecv.decode(&results)) //if statement recieving result of button press
   {
     irrecv.resume();   // Receive next value
   }
   
  if (results.value == 16736925)  // if recieve button press 2 function will initialize code specific to my controller change via decoder program
    {
      armservo.write(0); //input 0 position on servo
      delay(1500); //delay next function 1.5 secs

      armservo.write(75); //input 75 degree position on servo
      delay(1500);

      armservo.write(0);
      delay(1500);

      armservo.write(90); //input 90 degree position on servo
      delay(1500);
    } 
}

void rotateMotor(int rightMotorSpeed, int leftMotorSpeed)
{
  if (rightMotorSpeed < 0)
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,HIGH);    
  }
  
  else if (rightMotorSpeed > 0)
  {
    digitalWrite(rightMotorPin1,HIGH);
    digitalWrite(rightMotorPin2,LOW);      
  }
  
  else
  {
    digitalWrite(rightMotorPin1,LOW);
    digitalWrite(rightMotorPin2,LOW);      
  }

  if (leftMotorSpeed < 0)
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,HIGH);    
  }
  
  else if (leftMotorSpeed > 0)
  {
    digitalWrite(leftMotorPin1,HIGH);
    digitalWrite(leftMotorPin2,LOW);      
  }
  
  else 
  {
    digitalWrite(leftMotorPin1,LOW);
    digitalWrite(leftMotorPin2,LOW);      
  }
  
  analogWrite(enableRightMotor, abs(rightMotorSpeed));
  analogWrite(enableLeftMotor, abs(leftMotorSpeed));    
}

您可以:

  • 在首次使用 rotateMotor 的函数之前放置 前向声明 of rotateMotor。那将在调用 rotateMotor(0,0);setup 函数之上。如果编译器在到达该行时没有看到 rotateMotor 的匹配声明,它将失败。前向声明应该与函数定义中的完全一样:
    // only a declaration, no definition:
    void rotateMotor(int rightMotorSpeed, int leftMotorSpeed);
    

  • rotateMotor 的整个定义移到 setup 函数之前。