Serial Input Basics

It has occurred to me that there are many cases where a user only wishes to input a single number and the Parse example is unnecessarily complex for that.

The following code is a small extension of the version that receives several characters with an end marker to include code to convert the receivedChars to an int. I have marked all the new lines of code with // new for this version

const byte numChars = 32;
char receivedChars[numChars];	// an array to store the received data

boolean newData = false;

int dataNumber = 0;				// new for this version

void setup() {
	Serial.begin(9600);
	Serial.println("<Arduino is ready>");
}

void loop() {
	recvWithEndMarker();
	showNewData();
}

void recvWithEndMarker() {
	static byte ndx = 0;
	char endMarker = '\n';
	char rc;
	
	if (Serial.available() > 0) {
		rc = Serial.read();

		if (rc != endMarker) {
			receivedChars[ndx] = rc;
			ndx++;
			if (ndx >= numChars) {
				ndx = numChars - 1;
			}
		}
		else {
			receivedChars[ndx] = '\0'; // terminate the string
			ndx = 0;
			newData = true;
		}
	}
}

void showNewData() {
	if (newData == true) {
		dataNumber = 0;				// new for this version
		dataNumber = atoi(receivedChars);	// new for this version
		Serial.print("This just in ... ");
		Serial.println(receivedChars);
		Serial.print("Data as Number ... ");	// new for this version
		Serial.println(dataNumber);		// new for this version
		newData = false;
	}
}

...R