Question about functions in libraries

Hi everyone, I am working on a library for an ultrasonic range finder. This one : http://www.seeedstudio.com/wiki/Ultra_Sonic_range_measurement_module

There is my first attempt for the code
Main:

#include <UltraSonicSensor.h>

long AltitudeUltraSonic;
int pin = 2;

void setup()
{
  UltraSonicSensor().setPin(pin);
  Serial.begin(9600);
}

void loop()
{
  AltitudeUltraSonic = UltraSonicSensor().Altitude();
  Serial.print("Altitude (cm): ");
  Serial.println(AltitudeUltraSonic);
  delay(100);
}

CPP

#include "Arduino.h"
#include "UltraSonicSensor.h"

void UltraSonicSensor::setPin(int pin)
{
  _pin = pin;
}

long UltraSonicSensor::Altitude()
{
  pinMode(_pin,OUTPUT);
  digitalWrite(_pin,LOW);
  delayMicroseconds(2);
  digitalWrite(_pin,HIGH);
  delayMicroseconds(10);
  digitalWrite(_pin,LOW);
  pinMode(_pin,INPUT);
  
  pulseDuration = pulseIn(_pin,HIGH);
  return pulseDuration/(2*29);
}

Header

#ifndef UltraSonicSensor_h
#define UltraSonicSensor_h
#include "Arduino.h"

class UltraSonicSensor
{
public:
  void setPin(int pin);
  long Altitude(int _pin);
private:
  long pulseDuration;
  int _pin;
};

#endif

So this code is not working because pulseIn is not getting any signal. It seems that _pin is not correctly set. I would like to know why?

I have found a workaround by setting the pin directly in the Altitude function by having an input argument.
Still I would like to know why the code above is not working.

Thanks

You have a few problems here.

In your setup you have this line

UltraSonicSensor().setPin(pin);

So this will create an instance of UltraSonicSensor and call it’s setPin function. BUT this is a totally pointless exercise as this instance is going to become non existent once the command is done as you don’t save the instance that you’re creating.

Further down in your loop, you have this line

  AltitudeUltraSonic = UltraSonicSensor().Altitude();

This again is going to create an instance of UltraSonicSensor that only exists for this single command. Also this instance, (being quite a seperate instance to the one created in Setup) will not have any pin setup for it.

Furthermore, looking at your .h file, you have declared that an int variable should be passed to UltrasonicSensor.Altitude().

So try these mods to see if it gets you somewhat closer.

In your .h file you don’t need the pin to be passed to the Altitude function (as it gets set as a separate issue). So your class definition should look like this.

class UltraSonicSensor
{
public:
  void setPin(int pin);
  long Altitude(); //no need to pass the pin here
private:
  long pulseDuration;
  int _pin;
};

In your main sketch create a single instance of the UltraSonicSensor and use this instance througout,

voila

#include <UltraSonicSensor.h>

long AltitudeUltraSonic;
int pin = 2;

UltraSonicSensor myUS;

void setup()
{
  myUS.setPin(pin);
  Serial.begin(9600);
}

void loop()
{
  AltitudeUltraSonic = myUS.Altitude();
  Serial.print("Altitude (cm): ");
  Serial.println(AltitudeUltraSonic);
  delay(100);
}