I have created an Arduino sketch that I am using to send and receive data via serial. It works pretty well and doesn't seem go wrong as long as the correct formats are received. Is there anything I could do to improve this/shrink it to clear up some code smells?
String dataString; void setup() { Serial.begin(9600); //setup digital pins as inputs pin 3 - 13 for (int i = 2; i <= 7; i++) { pinMode(i, INPUT); } //setup digital pins as OutPuts pin 22 - 53 for (int i = 7; i <= 13; i++) { pinMode(i, OUTPUT); } } void loop() { serialDataOutput(); serialDataInput(); delay(100); } void serialDataOutput() { dataString = ""; for (int i = 0; i <= 13; i++) { dataString.concat(i); dataString.concat("," + String(digitalRead(i)) + "/"); } for (int i = 14; i <= 18; i++) { dataString.concat(i); dataString.concat("," + String(analogRead(i - 14)) + "/"); } Serial.println(dataString); delay(0); } void serialDataInput() { String Input; if (Serial.available() > 0) { // If data is available to read, Input = Serial.readStringUntil('\n'); // read it and store it in val } // Input = "13,1"; int commaIndex = Input.indexOf(','); // Search for the next comma just after the first int secondCommaIndex = Input.indexOf(',', commaIndex + 1); String firstValue = Input.substring(0, commaIndex); String secondValue = Input.substring(commaIndex + 1, secondCommaIndex); digitalWrite(firstValue.toInt(), secondValue.toInt()); }