library questions

Hello all, me again!
Today, I came in to find an HC-SR04 (ultrasonic rangefinder) lying on the
floor. I ordered it from eBay a while ago, and it has finally appeared! I
have been looking for an interesting project to do, and I saw on the playground
about writing a library. How fun I thought!

So today, I set off writing a library for the rangefinder. Although the code
to use it is very simple, and a library isn't really needed, it just sounded fun.

I have got the following code:

Header file

#ifndef Ultrasound_h
#define Ultrasound_h

#include "WProgram.h"

class Ultrasound
{
  public:
    rangeM();
    rangeCM();
    rangeMM();
  private:
    int _pingOut;
    int _pingIn;
};

#endif

source file

#include "WProgram.h"
#include "Ultrasound.h"

Ultrasound::Ultrasound(int pingOut, int pingIn)
{
  pinMode(pingOut, OUTPUT);
  pinMode(pingIn, OUTPUT);
  _pingOut = pingOut;
  _pingIn = pingIn;
}

void Ultrasound::rangeM()
{ 
  digitalWrite(pingOut, LOW);
  delayMicroseconds(2);
  digitalWrite(pingOut, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingOut, LOW);
  
  duration = pulseIn(pingIn, HIGH);
  
  return ((duration * 2938.66996) / 2 );
}

void Ultrasound::rangeCM()
{
  digitalWrite(pingOut, LOW);
  delayMicroseconds(2);
  digitalWrite(pingOut, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingOut, LOW);
  
  duration = pulseIn(pingIn, HIGH);
  
  return ((duration * 29.3866996) / 2 );
}

void Ultrasound::rangeMM()
  digitalWrite(pingOut, LOW);
  delayMicroseconds(2);
  digitalWrite(pingOut, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingOut, LOW);
  
  duration = pulseIn(pingIn, HIGH);
  
  return ((duration * 2.93866996) / 2 );
}

The problem comes when I try to save it. I have made an ultrasonic
library file in the main libraries file. When I go into it and save the header
file as Ultrasound.h it returns the following message:

The sketch name had to be modified. Sketch names can only consist
of ASCII characters and numbers (but cannot start with a number).
They should also be less less than 64 characters long.

It also changes the name to Ultrasound_h. I have 3 questions:

  • How do I get it to save as ultrasound.h and ultrasound.cpp;
  • Was I right to write the library in the arduino IDE;
  • Will my code work?

Thankyou very much, and sorry for the stupid question.

/me :slight_smile:

Was I right to write the library in the arduino IDE;

I allways develop libraries in a separate editor (notepad++ under win7) and have no difficulties with filenames. I don't know maybe the IDE only want to save as .pde ?

What platform are you on? windows linux mac?

be carefull with cases : - Ultrasound.h vs ultrasound.h

be carefull with cases : - Ultrasound.h vs ultrasound.

I will have to look at that.

I am using the windows 7 OS. I will have a go in notepad and let you know how I am going.

Thanks, Onions.

Another question you need to ask and solve is where do you save the .h and .cpp files so you can have the Arduino IDE find them for future inclusion for sketches.

Lefty

Will my code work?

At first sight: I don't think so. Purely based on the class interface you've written, I think you'll have some errors compiling.

First off, you've not declared the constructor. In your header file you should have the Ultrasound(int, int) method declared. Second, you're used two data members ('_pingOut' & '_pingIn'), but in the member functions you've omitted the leading underscore. It won't be able to find the variables that way. And lastly, you should declare the full method headers in the class declaration, not just their name.

Good luck.

  duration = pulseIn(pingIn, HIGH);

And duration is declared where?

The ultrasound sensor only uses one pin. Sometimes it is an output pin. Sometimes it is an input pin. You need to change the mode of the pin, not the pin number that is being used.

Finally, you should have one function, private, that does the pinging, and returns the time to receive an echo. The rangeM, rangeCM, and rangeMM functions should call that function to get the duration, then map the duration to the appropriate unit of measure for distance. Somewhere, you should define/describe the magic numbers that you use, too.

Hello again!
I have managed to save the files in notepad as .h and .cpp, so thankyou!

The ultrasound sensor only uses one pin. Sometimes it is an output pin. Sometimes it is an input pin.

I should have mensioned, I am using the HC-SR04. This has 4 pins - Vcc,
Ground, echo and trigger, so different pins are used. I am aware however,
that most rangefinders only use three pins, so thanks anyway. :slight_smile:

you should define/describe the magic numbers that you use

2938.66996 is the speed of sound in microseconds per meter - the
amount of time the sound wave takes to travel 1 meter, in microseconds.
As pulseIn measures in microseconds, we are using meters per microseconds,
as oppose to meters per second or millisecond.

If we divide this by 100, we get microseconds per CM, or 29.3866996.
By dividing this by 10, we get microseconds per MM, or 2.93866996.

We know speed = distance / time, so we can work out that
distance = speed * time. As the pulse travels out and back, we then divide
that distance by 2 to find out the distance away it is.

I think you'll have some errors compiling.

I do. I will keep you posted, should I manage to get it working.

Thankyou for all your help,
/me

you should define/describe the magic numbers that you use

2938.66996 is the speed of sound in microseconds per meter - the
amount of time the sound wave takes to travel 1 meter, in microseconds.
As pulseIn measures in microseconds, we are using meters per microseconds,
as oppose to meters per second or millisecond.

If we divide this by 100, we get microseconds per CM, or 29.3866996.
By dividing this by 10, we get microseconds per MM, or 2.93866996.

We know speed = distance / time, so we can work out that
distance = speed * time. As the pulse travels out and back, we then divide
that distance by 2 to find out the distance away it is.

Not here. In your code! ;D