//TangYatHo230087110
// Program: Q1_Extend_Ex5_Array.ino
/*
Q1 Extend the program Ex5_Array to read an integer (with at most 2 digits) from
the Serial Monitor and print out a group of stars that is equal to the integer
value. The stars should be arranged in rows of ten.
For example:
This is a test
Incorrect input
5234
Incorrect input
72
**********
**********
**********
**********
**********
**********
**********
**
0
8
********
*/
char buf[100] = {0}; // this is an array
char inChar; // this is a variable to store the current character read from the Serial Monitor
bool stringComplete = false;// this is a flag to indicate whether the input string is complete
int i = 0;//// this is a counter to keep track of the array index
bool goodString;//// this is a flag to indicate whether the input string is a valid integer
int Num;// this is a variable to store the converted integer value
// declare the function
//char ConvertString(char);
void PrintStars(int);// this is a function to print stars according to the integer value
void setup()//runs once when the program starts and is used to initialize the Serial communication.
{
// Initialize serial and wait for port to open
Serial.begin(115200);// this is a function to set the data rate in bits per second for serial data transmission
while (!Serial)// this is a loop to wait until the serial port is ready
{
}
}
void loop()//runs repeatedly and is used to read the input from the Serial Monitor
{
while (Serial.available()) // this is a loop to check if there is any data available on the serial port
{
inChar = (char)Serial.read(); // this is a function to read the incoming serial data as a character and assign it to the variable inChar
if (inChar == '\n') // this is a condition to check if the character is a newline character, which indicates the end of the input string
{
buf[i++] = inChar; // this is a statement to append the newline character to the array
buf[i] = 0; // this is a statement to add a null terminator to the array, which indicates the end of the string
stringComplete = true; // this is a statement to set the flag to true, which indicates the input string is complete
}
else // this is a condition to handle other characters
{
buf[i++] = inChar; // this is a statement to append the character to the array
}
}
if (stringComplete)// this is a condition to check if the input string is complete
{
goodString = true;// this is a statement to assume the input string is a valid integer
if (!(isdigit(buf[0]) && ((i == 2) || ((i == 3) && (isdigit(buf[1])))))) {// this is a condition to check if the input string is not a valid integer, which means it is not a single digit or a two-digit number
goodString = false;// this is a statement to set the flag to false, which indicates the input string is not a valid integer
}
if (goodString) // this is a condition to check if the input string is a valid integer
{
if (i == 2)// this is a condition to check if the input string is a single digit
{
Num = buf[0] - '0';// this is a statement to convert the character to an integer by subtracting the ASCII value of '0'
}
else // this is a condition to handle a two-digit number
{
Num = (buf[0] - '0') * 10 + (buf[1] - '0');// this is a statement to convert the two characters to an integer by multiplying the first one by 10 and adding the second one, after subtracting the ASCII value of '0'
}
Serial.println(Num);// this is a function to print the integer value to the Serial Monitor, followed by a newline
PrintStars(Num);// this is a function call to print stars according to the integer value
}
else // this is a condition to handle an invalid input string
{
Serial.print(buf);// this is a function to print the input string to the Serial Monitor
Serial.println("Incorrect input");// this is a function to print an error message to the Serial Monitor, followed by a newline
}
stringComplete = false;// this is a statement to reset the flag to false, which indicates the input string is not complete
i = 0; // this is a statement to reset the counter to zero, which indicates the array index is at the beginning
//Serial.println();
}
}
// body of the function
/*
char ConvertString(char x)
{
if (isUpperCase(x))
return (toLowerCase(x));
else if (isLowerCase(x))
return (toUpperCase(x));
else
return (x);
}
*/
void PrintStars(int n)// this is a function definition to print stars according to the integer value
{
for (int i = 1; i <= n; i++) // this is a loop to iterate from 1 to n, where n is the integer value
{
Serial.print("*");// this is a function to print a star to the Serial Monitor
if (!(i % 10))// this is a condition to check if i is divisible by 10, which means a row of 10 stars is completed
Serial.println(); // this is a function to print a newline to the Serial Monitor
}
if (n % 10)// this is a condition to check if n is not divisible by 10, which means the last row of stars is incomplete
Serial.println(); // this is a function to print a newline to the Serial Monitor return; // this is a statement to return from the function
return;
}