/**
  Basic sketch that reads commands with data from the Serial connection.
  For the BLE-Nano, this could be the Bluetooth connection also.

  How to setup the BLE-Nano for communication with another BLE-Nano (Master-Slave comm):
  See the paragraph "Bluetooth communication between two Nano BLE devices" @
  https://home.et.utwente.nl/slootenvanf/2020/04/10/arduino-nano-ble
*/

// variables used as text buffers for input received:
String line;

// game variables
int players = 0;


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


void loop() {
  // receive data from Serial connection:
  while (Serial.available() > 0) {
    char readchar = Serial.read();
    if (readchar >= 32 && readchar < 127) line += readchar; // if valid character add it to line (ignore/drop invalids chars)
    delay(2);
  }
  if (line.length() > 1) { // did we receive anything?
    if (line.startsWith("players")) { // is it the 'players' command?
      String playersString = getPart2(line); // fetch the second part of the line
      players = playersString.toInt(); // turn that into a number
      Serial.print("Players set to "); Serial.println(players);
    }
    //else if (line.startsWith("xxxx")) { // check for other command...
    //}


    // end of part that handles incoming data
    line=""; // reset line to be able to read next line
  }
  
}

/*
   Get first part of String s
*/
String getPart1(String s) {
  int space = s.lastIndexOf(' '); // is there a space? (' ')
  if (space > 0) return s.substring(0, space); // return part until the space
  return s; // if there was no space, just return the whole String
}

/*
   Get second part of String s
*/
String getPart2(String s) {
  int space = s.lastIndexOf(' '); // is there a space? (' ')
  if (space > 0) return s.substring(space + 1); // return part after the space
  return ""; // if there was no space, return empty String
}
$abcdeabcde151015202530354045505560fghijfghij