Help new library problems

Hi guys, I am writing down a library that helps who want to build cheap distance sensors.
We use the IR LED capacitance to determine the distance of the object, with two readings: ambient light check and ir light emission check

I try to write down a library to help people to use free this system but I get some errors:
ERER.cpp:64: error: no ‘void ERER::init(int)’ member function declared in class ‘ERER’

here it is the cpp:

/*
  ---------------------------------------------------
  ERER.cpp - Library for ERER sensors.
  Use 2 infrared LEDs to build a distance sensor
  After that think about to matrix it with this - _-
  ---------------------------------------------------
  Created by Giovanni Blu Mitolo, March 11, 2011.
  www.gioblu.com - gioscarab@gmail.com
  Released with Creative Commons Attribution licence
  ---------------------------------------------------
*/

#include "WProgram.h"
#include "ERER.h"

void ERER::emitterN(int emitterNegative){
 pinMode(emitterN, OUTPUT);
 _irEmitterP = emitterNegative;
}

void ERER::receiverN(int receiverNegative){
 _irReceiverN = receiverNegative;
}

void ERER::receiverP(int receiverPositive){
 _irReceiverP = receiverPositive;
}

void ERER::gain(int setGain){
 _gain = setGain;
}

void ERER::maxRangeValue(int setMaxRangeValue){
 _maxRangeValue = setMaxRangeValue;
}

void ERER::maxRange(int setMaxRange){
 _maxRange = setMaxRange;
}
/////////////////////////////////////////////////////////////////////////////


void ERER::distanceCheck() { 
  if(currentInit == 0){if(digitalRead(_irReceiverN) == LOW) ERER::ambientCheck();}
  if(currentInit == 1){if(digitalRead(_irReceiverN) == LOW) ERER::emissionCheck();}
}

void ERER::ambientCheck() {
  ambient = micros() - lightTime;
  ERER::init(1);
}

void ERER::emissionCheck(){
  rawData = micros() - lightTime;
  distance = rawData * (ambient / (ambient - rawData));
  distance = map(distance, 0, _maxRangeValue, 0, _maxRange);
  if(distance <= 0) distance = 0;
  if(distance > 15) distance = sqrt(distance) * _gain;
  rawData = 0; 
  ambient = 0;
  ERER::init(0);
}

void ERER::init(int emitter) {
  if(emitter == 0)digitalWrite(_irEmitterP, LOW);   
  if(emitter == 1)digitalWrite(_irEmitterP, HIGH); 
  lightTime = micros();
  pinMode(_irReceiverN, OUTPUT);
  digitalWrite(_irReceiverN, HIGH); //carico ricevitore di induttanza
  pinMode(_irReceiverN, INPUT);
  digitalWrite(_irReceiverN, LOW);
  if(emitter == 1) currentInit = 1;
  if(emitter == 0) currentInit = 0;
}

here it is the .h:

/*
  ERER.cpp - Library for ERER sensors.
  (use 2 infrared LEDs to build a distance sensor)
  Created by Giovanni Blu Mitolo, March 11, 2011.
  Released with Creative Commons Attribution licence
*/
#ifndef ERER_h
#define ERER_h

class ERER {
  public:
    long ambient; 
    void ambientCheck();
    long currentInit;
    long distance;
    void distanceCheck();
    void emissionCheck();
    void gain(int setGain)
    void init(int emitter);
    long lightTime;
    void maxRangeValue(int setMaxRangeValue);
    void maxRange(int setMaxRange);
    long rawData;
    void emitterN(int emitterNegative);
    void receiverN(int receiverNegative);
    void receiverP(int receiverPositive);
  private:
    int _irEmitterP;
    int _irReceiverP;
    int _irReceiverN;
    int _gain; 
    long _maxRange;
    long _maxRangeValue; 
};

#endif

pleease help :grin:

You are using the scope resolution operator on your call to init(), in ambientCheck(), but init() is not declared a static function, so this call won't work.

The ambientCheck function will be called for an instance of the class. That same instance should be used to invoke the init() function, shouldn't it?

Hi Pauls! How are you? Do you remember me?
I worked a lot on this but i can't figure how come out of this problem

shoul'd I write in ERER.h this?
  static void init(int emitter);
instead of
void init(int emitter);
to fix it?

here it is the ide code that I use:

#include <ERER.h>
ERER sensore1;

sensore1.receiverN(2);
sensore1.receiverP(5);
sensore1.emitterP(13);
sensore1.gain(4);
sensore1.maxRangeValue(666666);
sensore1.maxRange(2000);

void setup(){
  Serial.begin(9600);
}

void loop(){
 sensore1.distanceCheck();
}

I don't think that you want init() to be static. As defined, it is accessing non-static fields. I think you should just drop the ERER:: from in front of the ERER::init() call.

I edit the code following your advise.
This is the actual code:

ERER.h

/*
  ERER.cpp - Library for ERER sensors.
  (use 2 infrared LEDs to build a distance sensor)
  Created by Giovanni Blu Mitolo, March 11, 2011.
  Released with Creative Commons Attribution licence
*/
#ifndef ERER_h
#define ERER_h

class ERER {
  public:
    long ambient; 
    void ambientCheck();
    long currentInit;
    long distance;
    void distanceCheck();
    void emissionCheck();
    void gain(int setGain)
    void init(int emitter);
    long lightTime;
    void maxRangeValue(int setMaxRangeValue);
    void maxRange(int setMaxRange);
    long rawData;
    void emitterN(int emitterNegative);
    void receiverN(int receiverNegative);
    void receiverP(int receiverPositive);
  private:
    int _irEmitterP;
    int _irReceiverP;
    int _irReceiverN;
    int _gain; 
    long _maxRange;
    long _maxRangeValue; 
};

#endif

ERER.cpp

/*
  ---------------------------------------------------
  ERER.cpp - Library for ERER sensors.
  Use 2 infrared LEDs to build a distance sensor
  After that think about to matrix it with this - _-
  ---------------------------------------------------
  Created by Giovanni Blu Mitolo, March 11, 2011.
  www.gioblu.com - gioscarab@gmail.com
  Released with Creative Commons Attribution licence
  ---------------------------------------------------
*/

#include "WProgram.h"
#include "ERER.h"

void ERER::emitterN(int emitterNegative){
 pinMode(emitterN, OUTPUT);
 _irEmitterP = emitterNegative;
}

void ERER::receiverN(int receiverNegative){
 _irReceiverN = receiverNegative;
}

void ERER::receiverP(int receiverPositive){
 _irReceiverP = receiverPositive;
}

void ERER::gain(int setGain){
 _gain = setGain;
}

void ERER::maxRangeValue(int setMaxRangeValue){
 _maxRangeValue = setMaxRangeValue;
}

void ERER::maxRange(int setMaxRange){
 _maxRange = setMaxRange;
}
/////////////////////////////////////////////////////////////////////////////


void ERER::distanceCheck() { 
  if(currentInit == 0){if(digitalRead(_irReceiverN) == LOW) ERER::ambientCheck();}
  if(currentInit == 1){if(digitalRead(_irReceiverN) == LOW) ERER::emissionCheck();}
}

void ERER::ambientCheck() {
  ambient = micros() - lightTime;
  init(1);
}

void ERER::emissionCheck(){
  rawData = micros() - lightTime;
  distance = rawData * (ambient / (ambient - rawData));
  distance = map(distance, 0, _maxRangeValue, 0, _maxRange);
  if(distance <= 0) distance = 0;
  if(distance > 15) distance = sqrt(distance) * _gain;
  rawData = 0; 
  ambient = 0;
  init(0);
}

void init(int emitter) {
  if(emitter == 0)digitalWrite(_irEmitterP, LOW);   
  if(emitter == 1)digitalWrite(_irEmitterP, HIGH); 
  lightTime = micros();
  pinMode(_irReceiverN, OUTPUT);
  digitalWrite(_irReceiverN, HIGH); //carico ricevitore di induttanza
  pinMode(_irReceiverN, INPUT);
  digitalWrite(_irReceiverN, LOW);
  if(emitter == 1) currentInit = 1;
  if(emitter == 0) currentInit = 0;
}

IDE code

#include <ERER.h>
ERER sensore1;

sensore1.receiverN(2);
sensore1.receiverP(5);
sensore1.emitterP(13);
sensore1.gain(4);
sensore1.maxRangeValue(666666);
sensore1.maxRange(2000);

void setup(){
  Serial.begin(9600);
}

void loop(){
 sensore1.distanceCheck();
}

With this i get:
ERER.cpp:73: error: ‘currentInit’ was not declared in this scope

arrrrrggggg I am going crazy, seems very simple but i can't figure out why I get this error and where is it!! :astonished:

You forget to declare currentInit inside ERER.cpp.

I copied your .h, .cpp, and .pde files into the 0022 version of the IDE.

You have more errors than you listed.

    void gain(int setGain)

Needs a ; at the end;

void ERER::emitterN(int emitterNegative){
 pinMode(emitterN, OUTPUT);
 _irEmitterP = emitterNegative;
}

In the pinMode statement, emitterN is the name of the function, not the pin to be set.

void init(int emitter) {

What happened to the ERER::?

Your class is missing a constructor.

void ERER::distanceCheck() { 
  if(currentInit == 0){if(digitalRead(_irReceiverN) == LOW) ERER::ambientCheck();}
  if(currentInit == 1){if(digitalRead(_irReceiverN) == LOW) ERER::emissionCheck();}
}

Get rid of the scope resolution operator. Again.

sensore1.receiverN(2);
sensore1.receiverP(5);
sensore1.emitterP(13);
sensore1.gain(4);
sensore1.maxRangeValue(666666);
sensore1.maxRange(2000);

Executable code needs to be inside a function.