Can this done easier?

Berfore including something into a large sketch I am testing it in a simple sketch. The same here, the data that comes from a serial.read is here simulated by a charracter arry. The purpose of the if is to test if the received character contains the nu,ber 0-9 and two other charcters being the comma and dot, eg "qwerty,0123456789.\0" => ",0123456789."
The question is, can this be done easier?

void setup() {
   Serial.begin(9600);
   char teststring[20] = "qwerty,0123456789.\0";
  char saByte;
  bool num_keypad = false;
  for (int i; i <= 20; i++) {
    saByte = teststring[i];
    if(!num_keypad && saByte == ',' || saByte == '.'|| saByte >= '0'&& saByte <= '9'){
    Serial.print(saByte);
    }
  }
}

void loop() {
 }

Perhaps this might help:

Use strchr

You don't need to include the trailing nul char, the compiler will do that for you

char teststring[20] = "qwerty,0123456789.";

consider using readByteUntil()

if the received string has a space between the cmd and value, sscanf() can be used to read the cmd into a char array and an int. i don't believe the arduino scanf handles floats

Not only you gave 2 terminating 0 at index 18 and 19 you are also reading past the end of array

Then array will be 19 char long, and element [19] will be what?

just define it without a size and all will be taken care of, including the terminating null
char teststring [] = "qwerty,0123456789.";

1 Like

Hi
try this way.

String myString ;
String myResult;
char teststring[20] = "qwerty,0123456789.\0";
//-------------------------------------------------
void setup() {
  Serial.begin(9600);
  myString = teststring;
  int myStart = myString.indexOf(",");
  int myEnd = myString.indexOf(".");
  myResult = myString.substring(myStart, myEnd + 1);
  Serial.println(myResult);
}
//-------------------------------------------------
void loop() {
}

What are you trying to do?

See reply #3

see this post:
https://forum.arduino.cc/t/if-on-boolean-wont-work/956326

Set the "Compiler warnings" preference to All. Then you would get some helpful warnings about mistakes:

warning: suggest parentheses around '&&' within '||' [-Wparentheses]
     if(!num_keypad && saByte == ',' || saByte == '.'|| saByte >= '0'&& saByte <= '9'){
        ~~~~~~~~~~~~^~~~~~~~~~~~~~~~
/Users/john/Documents/Arduino/sketch_jan25a/sketch_jan25a.ino:8:69: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
     if(!num_keypad && saByte == ',' || saByte == '.'|| saByte >= '0'&& saByte <= '9'){
                                                        ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~

That means you should put parentheses in your expression to disambiguate the order of operation:
if(!num_keypad && (saByte == ',' || saByte == '.'|| (saByte >= '0'&& saByte <= '9'))) {

warning: 'i' is used uninitialized in this function [-Wuninitialized]
   for (int i; i <= 20; i++) {
            ^

You didn't assign an initial value to 'i' and it is a local variable so it doesn't automatically get set to zero. You have to explicitly initialize it:
' for (int i=0; i <= 20; i++) {`

You can save a little code by not using a variable that never changes ('num_keypad').

Here is a version of your sketch that fixes the errors and warnings and removes the unneeded variable.

void setup()
{
  Serial.begin(115200);
  delay(200);
  
  const char teststring[] = "qwerty,0123456789.";

  for (size_t i = 0; i < strlen(teststring); i++)
  {
    char saByte = teststring[i];
    if (saByte == ',' || saByte == '.' || (saByte >= '0' && saByte <= '9'))
    {
      Serial.print(saByte);
    }
  }
}

void loop() {}

...or you could just use strchr

As in: if (strchr(",.012356789", sabyte))?

  1. I think that would average more comparisons since it has to check 1, 2, 3, 4, 5, 6, 7, and 8 as well as comparing to 0 and 9.
  2. It will produce a false positive if 'saByte' contains zero.
  1. Meh. It's serial
  2. Good point.

How do you feel about bsearch? :smiley:

undefined, but beyond the end of the current string but available if you wanted to add additional characters. If you actually did print them out, they most likely would be '\0'

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