将3个程序合并为一个Arduino机器人项目

发布于 2025-01-23 05:25:25 字数 3976 浏览 1 评论 0原文

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

  1. 程序One允许机器人在光面表面上检测黑线并遵循它。
  2. 程序2允许用户按下IR遥控器上的按钮,然后机器人将旋转一个移动ARMS
  3. 程序3的伺服器,允许用户按另一个按钮播放音频文件

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

我将程序1和2合并在一起,没有问题,并在机器人上进行了测试,并且可以正常运行。当我合并第三个程序时,我会遇到一个错误:“ rotatemotor”不在此范围中。在我看来,这肯定在范围内。从我看时。经过一些故障排除后,我确实找到了删除行const const unsigned char voice1 [] progmem = {audio file}和#include< pcm.h>使程序再次功能正常。任何人都可以在这里的墙上提供各种帮助。

#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));    
}

I am currently building a robot that has 3 functions and so far have 3 different programs for the 3 functions:

  1. Program one allows the robot to detect a black line on a light surface and follow it.
  2. Program 2 allows a user to press a button on an IR remote and the robot will then rotate a servo that moves the arms
  3. Program 3 allows the user to press another button to play an audio file

NOTE: ALL THESE PROGRAMS WORK FINE AND TESTED FINE INDIVIDUALLY

I merged program 1 and 2 together no problem and tested it on the robot and it functioned correctly. When I merge the 3rd program I get an error: 'rotateMotor' is not in this scope. It seems to me it most certainly is within scope. from when I looked at it. After some troubleshooting I did find removing the line const unsigned char voice1[] PROGMEM = {audio file here} and #include <PCM.h> makes the program function fine again. Can anyone help at all kind of at a wall here.

#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));    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2025-01-30 05:25:26

您可以:

  • rotatemotorrotatemotor首先使用rotatemotor之前,请将向前声明首先使用。那将在设置函数上方,该功能调用rotatemotor(0,0);。如果编译器到达该行时没有看到rotatemotor的匹配声明,则将失败。正向声明看起来应该像函数的定义一样大好:
      //仅声明,没有定义:
    void rotatemotor(int rightmotorspeed,int左右peed);
     

  • rotatemotor的整个定义移动到设置函数之前。

You can either:

  • Put a forward declaration of rotateMotor before the function where rotateMotor is first used. That would be above the setup function which calls rotateMotor(0,0);. If no matching declaration of rotateMotor has been seen by the compiler when it reaches that line, it will fail. The forward declaration should look exacly like in the definition of the function:
    // only a declaration, no definition:
    void rotateMotor(int rightMotorSpeed, int leftMotorSpeed);
    

or

  • Move the whole definition of rotateMotor before the setup function.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文