Go Down

Topic: First attempts at Arduino OOP (Read 677 times) previous topic - next topic

markCaz

Jul 03, 2012, 12:42 am Last Edit: Jul 03, 2012, 12:53 am by markCaz Reason: 1
Hey guys,

I'm pretty new to Arduino, but have been doing okay so far. I am having a quite a bit of trouble writing a library function that reads and writes to the EEPROM. The project is an RFID door opener, I want the ability to add cards to the non-volatile memory if one's detected while the button is pressed, or check the card against the memory to allow entrance if the button is not depressed. The DataBase lib I'm writing should roughly do this, but I keep getting an error saying "'EEPROM' was not declared in this scope" even though it's in the header.

Everythings work in progress but I can't get past this bug! thanks for taking the time to read though it and any help you can give.. I'm also confused that despite updating to 1.01 I still need to include 'Arduino.h' for it to work. in the library tutorial it said it should run off 'WProgram.h' instead. only mention as maybe I fluffed the update.

My code structure is as follows: folder 'database' contains:

'database.ino' (with nothing in it)

'DataBase.h'
Code: [Select]

#ifndef DataBase_h
#define DataBase_h

#include <EEPROM.h> // DEFINITION RIGHT HERE :(
#include <Arduino.h>

class DataBase {
   public:
           DataBase();
           ~DataBase();
           boolean existsAt(int i);        // checks EEPROM to see if position is taken.
           int existsValue(int value[5]);  // checks EEPROM
           int getAt(int i);               // returns array
           void setAt(int i, int value[5]);// set in EEPROM
           void clearAt(int i);            // deletes in EEPROM
   };
   
#endif

'DataBase.cpp'
Code: [Select]

#include "DataBase.h"
DataBase::DataBase(){
 Serial.begin(9600);
}
   
DataBase::~DataBase(){
}
   
boolean DataBase::existsAt(int i){
 i*=8;
 boolean t = false;
 for (int j=i; j<i+8; j++){
   if(EEPROM.read(j)!=255){  // BUG HERE :(
     t=true;
   }
 }
 return t;
}

int DataBase::existsValue(int value[5]){
 for ( int i=0; i<1024/8; i+=1){
   if(existsAt(i)&&getAt(i)==value){
     return i;
   }
 }
 return -1;
}
   
int* DataBase::getAt(int i){
 int ans[5];
 i*=8;
 for (int j=i; j<i+8; j+=2){
   int[(j-i)/2] = int(EEPROM.read(j)) + int(EEPROM.read(j+1));
 }
 return ans;
}

void DataBase::setAt(int i, int value[5]){
 i*=8;
 for (int j=i; j<i+8; j+=2){
   byte a = 0;
   byte b = 0;        
   if(value>255){
     a = 255;
     b = value-255;
   }
  EEPROM.write(j  , a );
  EEPROM.write(j+1, b );
 }
}
   
void DataBase::clearAt(int i){
 i*=8;
 for (int j=i; j<i+8; j+=2){
   EEPROM.write(j  , a );
   EEPROM.write(j+1, b );
 }
}

AndyCC

#1
Jul 03, 2012, 12:52 am Last Edit: Jul 03, 2012, 12:56 am by AndyCC Reason: 1
Is EEPROM.h being included, or is it not being found? I've always found it useful in cases like this to put #error at the top of the header file you're including (temporarily, of course):

(EEPROM.h)
Code: [Select]
#error "Yes I just included EEPROM.h"

If you don't get the error, something is wrong with your include path or location of the header file. You could put it right before the line that defines EEPROM if you want to narrow things down.

Nick Gammon

Put this line:

Code: [Select]
#include <EEPROM.h>

at the start of the .ino file.

The IDE copies all your files to a temporary folder, based on the includes in the .ino file.

markCaz

#3
Jul 03, 2012, 01:05 am Last Edit: Jul 03, 2012, 01:09 am by markCaz Reason: 1
Hey Andy and Nick,

Cheers for the quick reply.. It appears not to be being included.

tried adding it at the top of the .cpp too, but no joy!

No idea why thought because it's one of the stock libraries.. I should note this project isn't in the library folder yet, but I wouldn't have expected that to stop it working.

Thanks for helping to bail me out..!

markCaz

oh! in the .ino? but I dont have control of the .ino from the library.. it doesn't make it v portable if I do have to. bit of a shame.

Tom Carpenter

Once it is in the libraries folder it should work without it included in the .ino file. But until then, the compiler needs to know where to look.
~Tom~

markCaz

Awesome that worked! Thanks..! good to know I wont need it in the end :) now to iron out all the other bugs.. heheheh..

Tom Carpenter

On second thoughts, you will...

Unless you copy the EEPROM.cpp and EEPROM.h files into your library folder.

e.g.

libraries
|-->DataBase
|---->DataBase.cpp
|---->DataBase.h
|---->EEPROM.h
|---->EEPROM.cpp

I knew there was a way I had gotten around that problem. Its only an extra 2kB to include in your library folder, and the "#ifndef EEPROM_h" statements in the EEPROM.h will prevent the compiler accidentally including it twice.
~Tom~

Go Up