"Not declared in this scope" Error with Libraries

Hi guys,

I’m working on a library to control a robot car. I have a class called “awesomeRobot”. I create an instance of it called “bot.” In my loop, I attempt to call functions from my library, e.g. bot.forward(), and I get an error message saying that “bot was not declared in this scope.” I can’t seem to make this work. Can anybody tell me what I’m missing here?

Header:

//
//  awesomeRobot.hpp
//  
//
//
//

#ifndef awesomeRobot_h
#define awesomeRobot_h

#include "Arduino.h"

class awesomeRobot
{
public:
    
    awesomeRobot();
    
    void startAwesomeRobot();
    void forward(int duration, int realSpeed);
    void reverse(int duration, int realSpeed);
    void curveRight(int radius, int duration, int realSpeed);
    void curveLeft(int radius, int duration, int realSpeed);
    void pivotRight(int duration, int realSpeed);
    void pivotLeft(int duration, int realSpeed);
    void stopBot();
    
    int motor1 = 6;
    int motor2 = 7;
    int motor3 = 8;
    int motor4 = 9;
    
    int ENA = 5;
    int ENB = 11;
    
};

#endif /* awesomeRobot_h */

CPP:

//
//  awesomeRobot.cpp
//  
//
//
//

#include "awesomeRobot.h"
#include "Arduino.h"

awesomeRobot::awesomeRobot(){
}

void awesomeRobot::startAwesomeRobot(){
    
    pinMode(motor1, OUTPUT);
    pinMode(motor2, OUTPUT);
    pinMode(motor3, OUTPUT);
    pinMode(motor4, OUTPUT);
    
    pinMode(ENA, OUTPUT);
    pinMode(ENB, OUTPUT);
}

void awesomeRobot::forward(int duration, int realSpeed){
    
    analogWrite(ENA, realSpeed * 2.55);
    analogWrite(ENB, realSpeed * 2.55);
    
    digitalWrite(motor1, HIGH);
    digitalWrite(motor2, LOW);
    digitalWrite(motor3, LOW);
    digitalWrite(motor4, HIGH);
    
    delay(duration);
    
}

void awesomeRobot::reverse(int duration, int realSpeed){
    
    analogWrite(ENA, realSpeed * 2.55);
    analogWrite(ENB, realSpeed * 2.55);
    
    digitalWrite(motor1, LOW);
    digitalWrite(motor2, HIGH);
    digitalWrite(motor3, HIGH);
    digitalWrite(motor4, LOW);
    
    delay(duration);
    
}

void awesomeRobot::curveRight(int radius, int duration, int realSpeed){
    
    analogWrite(ENA, realSpeed * 2.55 / radius);
    analogWrite(ENB, realSpeed);
    
    digitalWrite(motor1, HIGH);
    digitalWrite(motor2, LOW);
    digitalWrite(motor3, LOW);
    digitalWrite(motor4, HIGH);
    
    delay(duration);
}

void awesomeRobot::curveLeft(int radius, int duration, int realSpeed){
    
    analogWrite(ENA, realSpeed * 2.55);
    analogWrite(ENB, realSpeed * 2.55 / radius);
    
    digitalWrite(motor1, HIGH);
    digitalWrite(motor2, LOW);
    digitalWrite(motor3, LOW);
    digitalWrite(motor4, HIGH);
    
    delay(duration);
    
}

void awesomeRobot::pivotRight(int duration, int realSpeed){
    
    analogWrite(ENA, realSpeed);
    analogWrite(ENB, realSpeed);
    
    digitalWrite(motor1, LOW);
    digitalWrite(motor2, HIGH);
    digitalWrite(motor3, LOW);
    digitalWrite(motor4, HIGH);
    
    delay(duration);
}

void awesomeRobot::pivotLeft(int duration, int realSpeed){
    
    analogWrite(ENA, realSpeed);
    analogWrite(ENB, realSpeed);
    
    digitalWrite(motor1, HIGH);
    digitalWrite(motor2, LOW);
    digitalWrite(motor3, HIGH);
    digitalWrite(motor4, LOW);
    
    delay(duration);
    
}

void awesomeRobot::stopBot(){
    
    digitalWrite(ENA, LOW);
    digitalWrite(ENB, LOW);
    
}

Main Program:

#include <awesomeRobot.h>

void setup() {

  awesomeRobot bot;
  bot.startAwesomeRobot();
}

void loop() {
  
  bot.forward(1000, 90);
  bot.stopBot();
  delay(1000);
  bot.reverse(1000, 90);
  bot.stopBot();
  delay(1000);

}

Error Message:

Arduino: 1.6.11 (Mac OS X), Board: "Arduino/Genuino Uno"

/Users/evanjohnson/Desktop/Robot_Library_Test/Robot_Library_Test.ino: In function 'void loop()':
Robot_Library_Test:11: error: 'bot' was not declared in this scope
   bot.forward(1000, 90);
   ^
exit status 1
'bot' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

When you deal with Libraries, you (usually) have to create an instance of that class.

You have to crerate that instance before the setup part of your sketch.

The way you made it, it is only visible inside the setup() part of the sketch... Not in the loop

It might look something likeawesomeRobot bot;
Read the documentation and/or the examples

Jacques

void setup() {
  awesomeRobot bot;
  bot.startAwesomeRobot();
}

A variable declared in setup() goes out of scope as soon as setup() is finished. Bot is a variable of the type awesomeRobot. Declare bot in global scope to fix the error.

The code is now compiling and running properly! Thanks for the help guys! :slight_smile:

/*
Motor Test

Just see if the robot can move and turn.

Circuit:

  • Arduino Robot

created 1 May 2013
by X. Yang
modified 12 May 2013
by D. Cuartielles

This example is in the public domain
*/

void setup() {
// initialize the robot
Robot.begin();
}

void loop() {
Robot.motorsWrite(255, 255); // move forward --On this line
delay(2000);
Robot.motorsStop(); // fast stop
delay(1000);
Robot.motorsWrite(-255, -255); // backward
delay(1000);
Robot.motorsWrite(0, 0); // slow stop
delay(1000);
Robot.motorsWrite(-255, 255); // turn left
delay(2000);
Robot.motorsStop(); // fast stop
delay(1000);
Robot.motorsWrite(255, -255); // turn right
delay(2000);
Robot.motorsStop(); // fast stop
delay(1000);
}

can anyone see if they can figure out why it is saying this

Are you forgetting to include a library? Maybe look closely at the Robot library examples.