Convert a String to int array using Arduino Nano328

Hi to all,
I’ve this problem.

My scenario is:
I receive a message bye Serial channel, this message is very long, for example is a String of 308 chars.

This message is composed by several integer, example “123,500,4500,2340…”
I need to convert this string in array of int.

In my example I declared the message valued (I simulate that I reveied it by serial)

void loop()
{
  prog_uchar message[] PROGMEM = "350,165,50,35,50,120,50,35,50,40,50,35,50,35,50,35,50,35,50,35,50,40,45,40,50,35,50,35,50,120,50,35,50,40,50,35,50,35,50,35,50,35,50,35,50,40,45,40,50,120,50,35,50,35,50,35,50,40,50,35,50,35,50,35,50,35,55,30,50,40,45,125,50,35,50,120,50,125,45,40,50,35,50,35,50,35,50,120,50,40,50,120,50,120,50,35,55,120,50";

  int len = 308;
  
  unsigned int messageRaw[99];
  int i = 0;
  char c;
  while(i < len)
  {
    
    char currentStr;
    byte single = pgm_read_byte_near(message + i);
    
    currentStr = single;
    
    Serial.print("Current char: ");
    Serial.println(currentStr);
    
    if(currentStr != ',')
    {
      count += 1;
      current.concat(currentStr);
    }
    else
    {
      Serial.print("Number to convert: ");
      Serial.println(current);
      
      char currentValue[] = "";
      current.toCharArray(currentValue,count+1);
      messageRaw[index] = atoi(currentValue);
      
      Serial.print("Converted value: ");
      Serial.println(messageRaw[index]);
      
      index += 1;
      count = 0;
      current = "";
    }
    
    i++;
  }
}

My problem is that into monitor of ide, I read wrong character! It appears alla wrong characters.
I’m sure that I wrong somethings but I don’t konw what!
Can you help me?

Your code does not compile:

sketch_oct04e.ino: In function ‘void loop()’:
sketch_oct04e:23: error: ‘count’ was not declared in this scope
sketch_oct04e:24: error: ‘current’ was not declared in this scope
sketch_oct04e:29: error: ‘current’ was not declared in this scope
sketch_oct04e:32: error: ‘count’ was not declared in this scope
sketch_oct04e:33: error: ‘index’ was not declared in this scope

http://snippets-r-us.com/

Sorry!

int count = 0;
String current = "";
int index = 0;
boolean first = true;
unsigned int messageRaw[99];

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

But I finally solved!

I changed this row byte single = pgm_read_byte_near(message + i); in byte single = message;

But now I need more help! I need to another step before this row prog_uchar message[] PROGMEM = "...."

I need to assing a String to this prog_uchar message[]

Howe can I do it?

I need to assing a String to this prog_uchar message[]

You most certainly do NOT. The String class is a waste of resources. Learn to use C-style strings (NULL terminated arrays of chars) instead.

If you wish to use String class, read the reference on String.reserve() function. If you want to do it using the c-string, then best approach is to write a state machine. It will take in one char at a time and processes it according to its state.