//Student name: NGAN Hoi Sing
//Class: EG524403-1A
//Student number: 230292338
#define ARRAY_SIZE 1200 //defines a constant ARRAY_SIZE with a value of 1200.
char inputData[ARRAY_SIZE];
int dataIndex = 0; //index or position in the inputData array.
bool inputExceeded = false; //indicate whether the input data has exceeded the capacity of the inputData array.
void setup() {
Serial.begin(9600);
Serial.println("Please input:"); //Set the index what you need to check
}
void loop() {
if (Serial.available()) {
char receivedChar = Serial.read();
// Check if the received character is a newline or carriage return
if (receivedChar == '\n' || receivedChar == '\r') {
// Null-terminate the input string
inputData[dataIndex] = '\0';
// Calculate the length of the input string in bytes
int inputLength = dataIndex + 1;
// Display the input string and its length if it doesn't exceed 1200 bytes
if (!inputExceeded) {
Serial.print("Size of input data = ");
Serial.print(inputLength);
Serial.println(" bytes");
Serial.println(inputData);
} else {
//Serial.println("Input exceeds 1200 bytes. Ignoring input.");
Serial.print("Size of input data = ");
Serial.print(inputLength);
Serial.println(" bytes");
//store only the first 1200 characters of data and ignore all others.
for (int j=0; j < 1200; j++)
{Serial.print(inputData[j]);} Serial.println();
}
// Reset the data index and input exceeded flag to prepare for the next input
dataIndex = 0;
inputExceeded = false;
// Display "Please input again"
Serial.println();
Serial.println();
Serial.println("Please input again:");
} else {
// Store the received character in the inputData array if it doesn't exceed 1200 bytes
if (dataIndex < ARRAY_SIZE - 1 && !inputExceeded) {
inputData[dataIndex] = receivedChar;
dataIndex++;
// Check if the input has exceeded 1200 bytes
if (dataIndex >= ARRAY_SIZE - 1) {
inputExceeded = true;
}
}
}
}
}