Home > Software design >  Arduino, check chars inside a byte array
Arduino, check chars inside a byte array

Time:05-19

I am coding for a project where I update the time shown in an LCD 16x2 screen using commands from the serial port. The command I am using to update the time is {I,A, H,H,M,M,F}, where it means that its initiating, A is for Add, HH and MM is hours and minutes to update to and F is for finish.

I created a global byte array to hold this input:

byte bufferEntrada[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

The problem: I need to check for possible letters inside the command and i don't know how.

Example: IA1245F is a valid command, but IA12C5F is a invalid one.

Any help is much appreciated.

CodePudding user response:

If I understand your problem correctly, you are sending characters, not bytes. Although this is only a subtle difference, to your project it will make some difference.

You are sending ASCII characters. This means the character '0' can be represented as the decimal value 48. If you look at an ASCII table you can see that the characters '0' to '9' have values 48 to 57, inclusive. These are the values you need to accept as integer digits.

if (aChar >= 48 && aChar <= 57) { ... } else { /* not a number */ }

In C, this can be made more readable:

if (aChar >= '0' && aChar <= '9') { ... } else { /* not a number */ }

or, by using the isdigit() standard function

if (isdigit(aChar)) { ... } else { /* not a number */ }

Note: On Arduino, the corresponding function is isDigit()

CodePudding user response:

Well, it turns out that im stupid, each character reads out as a decimal number referring to it in the ASCII table, so to check if there is any character there i only need to run a for loop checking if there is any value inside the byte array that is > 10.

  • Related