Parse value from USB serial input

Hello! I’ve been trying to program my Arduino UNO to receive the command ‘write x y HEXCOLOR’ over USB serial. So far I have this:

void writePixel(int x,int y, uint16_t color) {
 String getValue(String data, char separator, int index)
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;

  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;

  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
//Correct format for pixel set it this:
//write x y hex
//write 100 200 0xFF3366
void loop()
  // send data only when you receive data:
        String content = "";
  char character;

  while(Serial.available()) {
      character =;

  if (content != "") {
    if(content.indexOf("write") >=0) {
      Serial.println("GOT WRITE");

The problem is that

getValue(content,' ', 1)

(Returns the X value)
Returns a string instead of an int. I also need to convert a string (“0xFF3366”) to uint16_t.

So my questions is this: How do I convert a String to an int and convert a String to an uint16_t?

Take a look on the Serial - command and/or SoftwaSerial :slight_smile:
The Serial - command reads and writes datas over the USB-cable you have connected the UNO to the pc.


Take a look at strtol!

I have extracted the values with strtoi/strtol and the command check with String.startsWith.

I hope I could have helped you.

Neither worked. My incoming data is formatted like this:

String stringMessage = "MESSAGE"

My incoming data is formatted like this:

And what value do you expect to get when you convert "MESSAGE" to an unsigned long? What value do you actually get?