Me again! Ultrasonic sensor issues

My project is continuing and the hardware is done, but the software for my ultrasonic sensor isn't measuring properly. It compiles, downloads, and does everything right, except the only distance it prints is "Distance in cm: 357". Every time. This isn't my code used inside of also not my code. It uses the ultrasonic library to measure distance.

int buttonValue;
int previous = HIGH;
int pin = 2;
int screenNumber = 0;

#include <LiquidCrystal.h>
#include <Ultrasonic.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 8, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

Ultrasonic ultrasonic(7,6); 

void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  Serial.begin(115200);
  pinMode(13, INPUT_PULLUP);
}

void loop()
{
  buttonValue = digitalRead(13);

  if (buttonValue == LOW && previous == HIGH)  // Has a button just been pressed?
  {
    switch (screenNumber)                      // Select the screen to display.
    {
      case 0:
        lcd.clear();
        lcd.print("Joystick control");  // joystick and rgb led page
        break;
      case 1:
        lcd.clear();
        lcd.print("Humidity/Temp");    // dht11 temp/humidity module page
        break;
      case 2:
        lcd.clear();
        lcd.print("Light level");       // photoresistor page
        break;
      case 3:
        lcd.clear();
        lcd.print("Distance");        // ultrasonic sensor page
        delay(500);
        lcd.clear();                           // Clears the screen in preparation of new measurement    
  lcd.print("Distance in cm:");           // Prints the first line of the display
  lcd.setCursor(0,1);                     // Moves cursor tot he second line of display
  lcd.print(ultrasonic.distanceRead());   // Takes measurement from US and prints it to LCD
        break;
      case 4:
        lcd.clear();
        lcd.print("IR control");          // infrared controller page
        break;
    }

    screenNumber = (screenNumber + 1 ) % 5;    // Calculate the next screen number
  }

  previous = buttonValue;
}

Can I get that in English, please?

Have you tried a simple sketch that just prints the ranges?

1 Like

Mb I fixed it lol

What's that?

It stands for my bad

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