'microsecondsToCentimeters' was not declared in this scope

Hello, I get this error when i run the following code and I still can’t see what’s the problem, I hope you guys can help me to fix it cuz I already waste to much time in it.

#include <LiquidCrystal.h>
const int pingPin = 7; // Trigger pin Ultrasonic Sensor
const int echoPin =6; // Echo pin of Ultrasonic Sensor
const int motorPin =2;
const int puzzer =4;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(13,12,11,10,9,8);

// Then we initialize all the devices used in the project

void setup() {
Serial.begin(9600); //Starting Serial Terminal
delay(2000);
lcd.begin(16,2);
pinMode(puzzer, OUTPUT);

pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, HIGH);
lcd.setCursor(1,1);
lcd.print(“pump on”);

}

void loop() {
long duration, cm, lev;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);

pinMode(echoPin,INPUT);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);
lev = 32 - cm;

lcd.setCursor(1,0);
lcd.print(“level”);
lcd.setCursor(11,0);
lcd.print(“cm”);
lcd.setCursor(10,0);
lcd.print(" ");
lcd.setCursor(8,0);
lcd.print(lev);

/After that we check conditions if water level is LOW or water level is HIGH and take
actions accordingly.
/

if (lev>28 && lev<32)
{

 digitalWrite(motorPin, LOW);
 lcd.setCursor(1,1);
 lcd.print("pump off");


}

if (lev>8 && lev<15)
{
digitalWrite(puzzer, HIGH);
delay(500);
digitalWrite(puzzer, LOW);
delay(500);
digitalWrite(puzzer, HIGH);
delay(500);
digitalWrite(puzzer, LOW);
delay(500);

 digitalWrite(motorPin, HIGH);
 delay(500);
 lcd.setCursor(1,1);
 lcd.print("pump on");
 delay(500);



}

}

This line “cm = microsecondsToCentimeters (duration);” calls function microsecondsToCentimeters (duration); ,
but this function has not been declared.

RV mineirin

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code to stop it being mangled by the forum software as yours has been and to make it easier to copy for examination

As to the error, where is the microsecondsToCentimeters() function declared ? It is not in your program

Try using the find feature of the Arduino IDE to find the microsecondsToCentimete thingy. Did find find more than one entry for the microsecondsToCentimete thingy? If not that’s why the error message is being produced.

Hi, @shesherees
Welcome to the forum.

Can I suggest you use the NewPing library to read your Ultrasonic Unit, it makes it so much easier to get what you need.

What model Arduino are you using?

Thanks… Tom… :grinning: :+1: :coffee: :australia:

The problem is that I’m new to this and the code is not mine but I want to try it and see how it works cuz I’m interested, so I have limit information about all of this. So if you can make changes to the code and fix it, I’ll appreciate you guys. I’m spending too much time on learning this stuff but I don’t think that I reached to the level of writing my own code. Plus if you can suggest to me how to start learning it in a better way will be really great.

How can I declare it then

Where did you get the code from ?

I copy it from here
http://www.uoh.edu.sa/en/Subgates/Faculties/CM/Departments/Electrical/EEstudents/PublishingImages/Pages/SYP/S_171%20EE%20411%20(SDP)%20Reports.pdf

I hope that they did not get too many marks for their programming skills as the function is missing from their code.

As previously suggested, install the NewPing library and have a look at the examples that it comes with

The function that you are looking for is really very simple

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Just copy it into your sketch and try compiling it again

Thanks a lot, will do.