Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arduino error: does not name a type?

Tags:

arduino

I have written a library, but have problem with error does not name a type. I've tryed everything, searched for couple of hours and no luck. Library is placed in the "libraries" folder of the arduino sketch folder. Please help!!! I am using OSX, but the same problem occurs on Windows also.

This is header file of the library:

#ifndef OpticalSensor_h
#define OpticalSensor_h

#include <Arduino.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
#include <String.h>

class OpticalSensor
{
    public:
        OpticalSensor(int analogPort);
        void LCDInit(int columns, int rows);
        void SerialInit(int bitRate);
        void SDInit();
        double& ReadFromAnalogPort();
        void SDCreateFile(String fileName);
        void SDDeleteFile(String fileName);
        void SDWriteToFile(String fileName);
        void SDStreamToFile(String Text);
        void SDOpenFileToStream(String fileName);
    private:
        int _analogPort;
        bool _displayFlag;
        Adafruit_RGBLCDShield _lcd;
        File _MainRecFile;
        double _voltage;
        void _LCDClearAll();
        void _LCDWriteInTwoRows(String row1, String row2);
        void _DelayAndClearLCD(bool returnStatus);
};

#endif

This is .cpp file of the library:

#include <OpticalSensor.h>

Adafruit_RGBLCDShield _lcd;
File _MainRecFile;
double _voltage;

OpticalSensor::OpticalSensor(int analogPort)
{
    _analogPort = analogPort;
}

void OpticalSensor::LCDInit(int columns, int rows)
{

    _lcd = Adafruit_RGBLCDShield();
    _lcd.begin(columns,rows);
}

void OpticalSensor::SerialInit(int bitRate)
{
    Serial.begin(bitRate);
    _bitRate = bitRate;
    while(!Serial) {
        //wait until serial is not open
    }
}

void OpticalSensor::SDInit()
{
    // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
    // Note that even if it's not used as the CS pin, the hardware SS pin
    // (10 on most Arduino boards, 53 on the Mega) must be left as an output
    // or the SD library functions will not work.
    pinMode(10, OUTPUT);

    //check if SD can be found and initialized. Print also message to
    //Serial if initialized and to _lcd if initialized.
   if(!SD.begin(4)) {
     if(Serial){
         Serial.println("Initialization failed!");
     }
     if(_lcd){
         _lcd.print("Init failed!");
     }
     _DelayAndClearLCD(true);
   }
   else {
       if(Serial) {
           Serial.println("Initialization done!");
       }
       if(_lcd) {
           lcd.print("Init done!");
       }
       _DelayAndClearLCD(false);
   }
}

void OpticalSensor::SDCreateFile(String fileName)
{
    //check if file allready exists, if not it creates one
    //and writes apropriate response to
    //lcd and Serial if they are initialized.
    if(SD.exists(fileName)) {
        if(Serial) {
            Serial.println(fileName + " already exists!");
        }
        if(_lcd) {
            _LCDWriteInTwoLines(fileName,"already exists!");
        }
        _DelayAndClearLCD(false);
    }
    else
    {
        if(Serial) {
            Serial.println(fileName + "Creating file " + fileName + "...");
        }
        if(_lcd) {
            _LCDWriteInTwoLines("Creating file", fileName);
        }
        _MainRecFile = SD.open(fileName + ".txt", FILE_WRITE);
        _MainRecFile.close();
        _DelayAndClearLCD(false);


        //check if file was created successffully and print apropriate response
        //to lcd and Serial if they are initialized
        if(SD.exists(fileName + ".txt")) {
            if(Serial) {
                Serial.println(fileName + ".txt" + " created successffully!");
            }
            if(_lcd) {
                _LCDWriteInTwoLines(fileName + ".txt", "created!");
            }
            _DelayAndClearLCD(false);
        }
        else {
            if(Serial) {
                Serial.println("error: failed to create file!");
            }
            if(_lcd) {
                _LCDWriteInTwoLines("error: failed to","create file!");
            }
            _DelayAndClearLCD(false);
        }
    }
}

//delete file from SD card
void OpticalSensor::SDDeleteFile(String fileName)
{

}

//open file, write data to it, and close file after.
void OpticalSensor::SDWriteToFile(String fileName, String Text)
{
    _MainRecFile = SD.open(fileName + ".txt", FILE_WRITE);
    _MainRecFile.println(Text);
    _MainRecFile.close();
}

//Open file to stream data to it.
void OpticalSensor::SDOpenFileToStream(String fileName)
{
    _MainRecFile = SD.open(fileName + ".txt", FILE_WRITE);
}

//Write data to file while file is open.
//Notice that you can stream data only to one file at a time!!!
//For instance, if you have two sensors that you want to
//write data to two different files, you have to use SDWriteToFile
//function!!!
void OpticalSensor::SDStreamToFile(String Text)
{
    if(_MainRecFile) {
        _MainRecFile.println(Text);
    }
}

//close file that you streamed data too.
void OpticalSensor::SDCloseStreaming(String fileName)
{
    _MainRecFile.close();
}

//clear entire LCD
void OpticalSensor::_LCDClearAll()
{
    _lcd.clear();
    _lcd.setCursor(0,0);
}

void OpticalSensor::_LCDWriteInTwoRows(String row1, String row2)
{
    //write first String in row1
    _lcd.print(row1);
    //set cursor to the beginning of row 2
    _lcd.setCursor(0,1);
    //write second String to row 2
    _lcd.print(row2);
}

void OpticalSensor::_DelayAndClearLCD(bool returnStatus)
{
    //if Serial or _lcd are initialized, delay for 2 seconds
    //and clear LCD
    if(Serial || _lcd) {
        delay(2000);
        if(_lcd)
            _LCDClearAll();
    }
    //terminate
    if(bool == true) {
        return;
    }
}

double& ReadFromAnalogPort()
{
    _voltage = analogRead(_analogPort);
    return _voltage;
}

And this is the .ino file where library is included:

#include <OpticalSensor.h>

OpticalSensor sensor(0);

void setup() {
  sensor.LCDInit(16,2);
  sensor.SerialInit(9600);
  sensor.SDInit();
  sensor.SDCreateFile("test1");
  sensor.SDOpenFileToStream("test1");
}

void loop() {

}

this is the error:

In file included from Test_OpticalSensorLib.ino:1: /Users/gaspersladic/Documents/Arduino/libraries/OpticalSensor/OpticalSensor.h:34: error: 'Adafruit_RGBLCDShield' does not name a type /Users/gaspersladic/Documents/Arduino/libraries/OpticalSensor/OpticalSensor.h:35: error: 'File' does not name a type

like image 312
Gašper Sladič Avatar asked Dec 16 '22 06:12

Gašper Sladič


2 Answers

The two includes you mention in your comment are essential. 'does not name a type' just means there is no definition for that identifier visible to the compiler. If there are errors in the LCD library you mention, then those need to be addressed - omitting the #include will definitely not fix it!

Two notes from experience which might be helpful:

  1. You need to add all #include's to the main sketch - irrespective of whether they are included via another #include.

  2. If you add files to the library folder, the Arduino IDE must be restarted before those new files will be visible.

like image 197
Peter B Avatar answered Mar 04 '23 19:03

Peter B


I got the does not name a type error when installing the NeoMatrix library.

Solution: the .cpp and .h files need to be in the top folder when you copy it, e.g:

myArduinoFolder/libraries/Adafruit_NeoMatrix/Adafruit_NeoMatrix.cpp

When I used the default Windows unzip program, it nested the contents inside another folder:

myArduinoFolder/libraries/Adafruit_NeoMatrix/Adafruit_NeoMatrix/Adafruit_NeoMatrix.cpp

I moved the files up, so it was:

myArduinoFolder/libraries/Adafruit_NeoMatrix/Adafruit_NeoMatrix.cpp

This fixed the does not name a type problem.

like image 25
asdf Avatar answered Mar 04 '23 19:03

asdf