Using AnalogIn in a class

08 Nov 2010

first question, has anyone done this and has a code example? I am struggling to get this to work properly.

 

Second question, anyone have any idea why this isn't working,

.cpp

IRRangeFinder::IRRangeFinder (PinName pin):   _pin(pin) {
    
};
// returns value for range, greater than 4, 5, 6, 7, 8, 10, 12, 14, 20, 25, 30 cm
int read(void) {
     float temp = _pin.read();// complier error, _pin not declared(E20)    
 if ( temp<0.1212) {
        return 30;
    }
.h

class IRRangeFinder {
    
    public:
    
        // @param ain analog in from sensor
        IRRangeFinder (PinName pin);
        
        // returns value for range, greater than 4, 5, 6, 7, 8, 10, 12, 14, 20, 25, 30 cm
        int read(void);
        
        //returns value for floating point given by analog in
        float read_f(void);
        
        //returns value for voltage, given by (1/3.3)*(read_f)
        float read_v(void);
        
        
    protected:
    
        AnalogIn _pin;
        
};
the mbed.h and header files are included.

 

its probably glaringly obvious, but I can't see it.

08 Nov 2010 . Edited: 08 Nov 2010

You are close! .cpp should be:

int IRRangeFinder::read(void) {
   float temp...

Simon

08 Nov 2010 . Edited: 08 Nov 2010

now thats embarrasing.

 

thanks for solving my weekends worth of head scratching in about 13 minutes :)

 

It compiles! YAY!

08 Nov 2010

This is the sort of problem that is only quick to see once you've made the mistake yourself :)

So when the next person gets stuck, pass it on!

Simon