Home > Software engineering >  Trouble merging 3 programs into one for a Arduino robot project
Trouble merging 3 programs into one for a Arduino robot project

Time:04-21

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

CodePudding user response:

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.
  • Related