Compare a 1 digit number to a 3 digit number

Hello I try to compare values with different digits.
Here is an example:

if("10" >= "5") {
Serial.print("OK");
}

That doesn't work but

if("9" >= "5") {
Serial.print("OK");
}

That works. Also

If("15" >= "10") {
Serial.print("OK");
}

That works too.

How can I use different digits values?

Remove the quotes. You are comparing ASCII strings of different sizes

2 Likes

Those are C-strings (zero terminated character arrays), and logical tests like ">=" do not provide a useful comparison.

You could convert those C-strings to integers using the function atoi(), then compare the integers, or compare them as C-strings using strcmp().

If I remove the quotes I get this error:

error: conversion from 'int' to 'const String' is ambiguous
if(input > 5){
^
note: candidate: String::String(const __FlashStringHelper*)
String(const __FlashStringHelper *str);
^~~~~~
note: conversion of argument 1 would be ill-formed:

note: candidate: String::String(const char*)
String(const char *cstr = "");
^~~~~~
note: conversion of argument 1 would be ill-formed:

note: initializing argument 1 of 'unsigned char String::operator>(const String&) const'
unsigned char operator > (const String &rhs) const;
^~~~~~~~

exit status 1

Compilation error: conversion from 'int' to 'const String' is ambiguous

Please post ALL the code, using code tags. What type of variable is "input"?

Avoid using String objects on AVR Arduinos, like the Uno, Mega, etc. as they cause memory problems and program crashes.

To compare any two integer numbers, use something like this:

int a=123;
int b=7;
if (a < b) {
     do_something();
     }
//HC-12 messenger send/receive
//autor Tom Heylen tomtomheylen.com

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); //RX, TX

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() {
  
  
  if(Serial.available() > 0){//Read from serial monitor and send over HC-12
    String input = Serial.readString();
    mySerial.println(input);    
  }
 
  if(mySerial.available() > 1){//Read from HC-12 and send to serial monitor
    String input = mySerial.readString();
    Serial.println(input);
    if(input > 5){
      mySerial.println("OK");
    }
    else {}
  }
  delay(20);
}

Replace this

    String input = mySerial.readString();
    Serial.println(input);
    if(input > 5){

with

    int input = mySerial.parseInt();
    Serial.println(input);
    if(input > 5){
1 Like

Thank you I will try tomorrow. Can you explain why I need to replace that part?

Because you are interested in the integral value of the input. readString() merely returns a string of characters, whereas parseInt() returns the numerical value of the input.

You can't compare a String (or a C-string) with an integer. They are fundamentally different entities and are used for very different purposes.

You need to learn more about the different types of variables and how they are used. It is an extremely important aspect of the programming language.

Do you recommend a place or video to learn about them or am l on my own?

It is a very basic concepts of C/C++ language, you can see it in any textbook or manual

Okay I will look it up

Okay I tried it and it worked thank you.

The only weird thing now it when the transmitter gets a message it serial prints the message, sends a OK response back followed by a 0.

Where does the zero suddenly appears from?

It's reading extra characters from the serial monitor.

Change the line ending setting to "none" (forget exactly, but not carriages rteturn or line feed.

Then no extra characters get sent.

Alternatively, learn about how to handle line ending characters. This will come in handy when you can't just go and turn them off, but have to deal with them when someone else is sending to you.

HTH

a7

@henrik9979
Side note - do not show the code as screenshots, always insert the code as text using code tags, according the forum rules.

Okay then next time I will remember to drag a black marker across the entire code so only the serial monitor will be visible, which was purposely to demonstrate what happened in the serial monitor when running the code.

No, it is better to insert contents of serial monitor to the forum as text too

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