'microsecondsToCentimeters' was not declared in this scope

Hello, I tried to run my code in arduino and it keeps telling me that microsecondsToCentimeters’ was not declared in this scope. Though I don’t see any problem with it and I think it should work perfectly. please help me, it already took to much time from me.

This is the code that I tried to run

#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);
    }
}

Hey @sheshe,

This forum is for discussion of Particle devices and services. For future Arduino questions you will be better served by the Arduino forum.


Regarding your code, you are trying to use a microsecondsToCentimeters function without declaring it. You’re probably looking for the definition provided in the Ping example in the Arduino documentation:

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;
}
1 Like

Thank you very much, I’ll check the Arduino forum

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.