Hi everyone, I am new to Arduino. I am trying to create a library for my ultrasonic range sensor.
I can't compile the code because of an error. I can't find where the problem is.
Here is the code to test the library :
#include <UltraSonicSensor.h>
long AltitudeUltraSonic;
int pin = 2;
void setup()
{
UltraSonicSensor().setPin(pin);
Serial.begin(9600);
}
void loop()
{
AltitudeUltraSonic = UltraSonicSensor().Altitude();
Serial.println("Altitude (cm): ");
Serial.print(AltitudeUltraSonic);
delay(100);
}
The header file :
#ifndef UltraSonicSensor_h
#define UltraSonicSensor_h
#include "Arduino.h"
class UltraSonicSensor
{
public:
setPin(int pin);
long Altitude();
private:
long pulseDuration;
const int _pin;
};
#endif
The cpp file :
#include "Arduino.h"
#include "UltraSonicSensor.h"
UltraSonicSensor::setPin(int pin)
{
_pin = pin;
}
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);
}
The error is :
In file included from testUltraSonicSensor.ino:7:
C:\Documents\Arduino\libraries\UltraSonicSensor/UltraSonicSensor.h:14: error: ISO C++ forbids declaration of 'setPin' with no type
Thanks for the help and happy new year