0

I have a requirement where an event has char *newvalue.

void event(char *newValue) {
//Code here
}

newValue is 2 digits. If it's 1-9 it is " 1" " 2" and 10+ is "10" "11" etc. It also can contain alpha characters as well not just numbers.

I am passing this to a function void function(uint8_t ascii) to display on a single digit display. If I pass newValue it doesn't register less than 10 as it picks up the space.

So what I am trying to achieve is if it's a single character, strip the space somehow into a uint8_t and if it's 2 digits strip just the right digit as a uint_8 and have a bool where I will turn on the decimal point.

I've googled like crazy and can't seem to get something workable. If somebody can help it would be appreciated. If the above is not clear please let me know what more info you need.

The code below, 'value here' is where I want to put the rightmost value from *newvalue.

void onUfcComm2DisplayChange(char *newValue) {
  comdisp.setBrightness(9);
  comdisp.writeDigitAscii(0, 'valuehere');
  comdisp.writeDisplay();
}

void Adafruit_AlphaNum4::writeDigitAscii(uint8_t n, uint8_t a,  boolean d, boolean w) {
  uint16_t font;
  if (w == false) {
    font = pgm_read_word(alphafonttable + a);
  } else {
    font = pgm_read_word(alphafonttableWD + a);
  }
  displaybuffer[n] = font;
  if (d) displaybuffer[n] |= (1 << 14);
}
  • Please show us the sketch you're using. – VE7JRO Mar 29 at 4:40
  • 1
    C++ function strtok() can be used for spliting a str into a token, as long as there is a delimiter in the str. cplusplus.com/reference/cstring/strtok. – hcheung Mar 29 at 5:22
  • Thats where I have been unstuck. there is no delimiter. – Tekadept Mar 29 at 6:12
  • I amended the basic code in original post – Tekadept Mar 29 at 6:14
0

I'm not real clear on exactly what you want to have there, but here are a few ideas that may help:

If you know what character you want, then just get it. Like if you always want the second character (I think this is what you need):

void someFunc(char *p)
{
  char secondChar = p[1];
  Serial.print(secondChar);
}

If you just want the last character in the string no matter how long, get the length of it and then pull that character:

void someFunc(char *p)
{
  int len = strlen(p);
  char lastChar = p[len-1];
  Serial.print(lastChar);
}

If you want the last digit in the string no matter how long and no matter how many random characters that aren't digits after it:

void someFunc(char *p)
{
  int len = strlen(p);
  char lastDigitChar = 0;
  // Cycle backwards til we get the first digit
  for(int i=len-1; i>=0; i--){
    if(isDigit(p[i]){
      lastDigitChar = p[i];
      break;
    }
  }
  Serial.print(lastDigitChar);
}
| improve this answer | |
  • Thanks that is exactly what I was after..... I think I was just trying to overengineer the whole thing and that is just so simple it works perfectly thankyou! – Tekadept Mar 29 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.