Pages: [1]   Go Down
Author Topic: First attempts at Arduino OOP  (Read 655 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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 );
  }
}
« Last Edit: July 02, 2012, 05:53:39 pm by markCaz » Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 75
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
« Last Edit: July 02, 2012, 05:56:19 pm by AndyCC » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 504
Posts: 19095
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Put this line:

Code:
#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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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..!
« Last Edit: July 02, 2012, 06:09:03 pm by markCaz » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1729
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

~Tom~

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Leeds, UK
Offline Offline
Edison Member
*
Karma: 80
Posts: 1729
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

~Tom~

Pages: [1]   Go Up
Jump to: