Problem with code reading the serial monitor

I'm having a problem figuring out how to fix my code. I'm pretty new to Arduino and thought of a starter project of having a ultrasonic sensor beep before i get too close to something so i can walk around my house blindfolded.

#include <SR04.h>
String distance = "0";

#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

void setup() {
   Serial.begin(9600);
   delay(100);
}

void loop() {
   a=sr04.Distance();
   Serial.print(a);
   Serial.println("cm");
   delay(100);

   //contacting

    while (Serial.read() == 0)
  { //Wait for user input

  }
  distance = Serial.readString();

  if (distance > "30")
  {
    Serial.println("beep");
  }
  else
  {
    Serial.println("no beep");
  }

}

(i am also using a library for the sensor)

this is the code im using and this doesn't include the code for the beep sound so thats what beep and no beep means.

The problem is when i run the code in the serial monitor i want anything below 30 to come up as beep but its all no beep. ex.

is there any way i can fix this?

Is this your module ?

yes

Means, if the value of 'distance' is greater than the text string "30".

You are comparing two completely different data types. It's not even possible to compare text strings this way...

it should be

  if (distance > 30)

Go through this discussion.

https://create.arduino.cc/projecthub/abdularbi17/ultrasonic-sensor-hc-sr04-with-arduino-tutorial-327ff6

when i use that it comes up with this error: conversion from 'int' to 'const String' is ambiguous

Oh, 'distance' is a String! I didn't expect that because it's a very weird way to store integer values. I don't think the String class overload of '>' handles integers anyway, it's probably checking the string length.

I think you have just obscured everything with the library, you have no idea what is happening "under the hood". Please consult the library documentation and examples.

okay thank you

Always read the documentation:
https://www.arduino.cc/reference/en/language/variables/data-types/string/operators/greaterthan/

"Caution: String comparison operators can be confusing when you’re comparing numeric Strings, because the numbers are treated as Strings and not as numbers. If you need to compare numbers numerically, compare them as ints, floats, or longs, and not as Strings."

I didn't know exactly what '>' does in the String class because I never use the String class for anything. What it is, is a lexical comparison (in other words, a dictionary style ordering). Like,

"cat" > "car"

@anon57585045 maybe in your dictionary…

a7

It's just a lexical comparison, that fails. Or doesn't.
I did hear a whooshing sound over my head.

>

image

1 Like

You can't read back the value you wrote to Serial Monitor. Just use the value you stored in the variable 'a'.

#include <SR04.h>

#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;

void setup() {
   Serial.begin(9600);
   delay(100);
}

void loop() {
   a = sr04.Distance();
   Serial.print(a);
   Serial.println("cm");

  if (a > 30)
  {
    Serial.println("beep");
  }
  else
  {
    Serial.println("no beep");
  }
  delay(100);
}

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