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