// Program: 1B Ex5 Q1 Chan Tai Ming.ino
// Program: Ex5_Array.ino
/*
Exercise 5 Array, string, and function
Echo with some modifications
*/
char buf[100] = {0}; // this is an array
char inChar;
bool stringComplete = false;
int i = 0;
void setup()
{
// Initialize serial and wait for port to open
Serial.begin(115200);
while (!Serial)
{
// Wait for serial port to connect, needed for native USB port only
}
}
// ** IMPORTANT **
// MUST use buf[]
// CAN'T use functions like Serial.parseInt, Serial.readString
// Handle the UI (User Interface) like user prompt and error message
// User prompt: "Input a number"
// Error message: "Incorrect input"
// Add space between each set of input for better appearance (sort of UI)
// Refer to the example on the worksheet
void loop()
{
int num;
while (Serial.available())
{
inChar = (char)Serial.read();
buf[i++] = inChar;
if (inChar == '\n') // change '\n' to '~' when using Tinkercad
{
// buf[i++] = inChar; // last character is newline
buf[i] = 0; // string array should be terminated with a zero
stringComplete = true;
}
else
{
// some actions after received
// inChar = ConvertString(inChar);
// buf[i++] = inChar;
}
}
if (stringComplete)
{
Serial.print(buf); // the printing of string will be stopped when zero is reached
// Serial.println(i);
if ((i == 2) && (isDigit(buf[0]))) {
Serial.println("Good number");
num = buf[0] - '0';
PrintStars(num);
} else {
Serial.println("Not a good number");
}
// Write code to handle 2 digits
// if ((i == 3)) && ...
// Get the "num"ber
// Print stars
// PrintStars(24);
stringComplete = false;
i = 0;
}
}
void PrintStars(int n) {
// This loop can be implemented using while or do-while
// How about j was initialized to 1?
// for (int j = 1; j <= n; j++) {
for (int j = 0; j < n; j++) {
Serial.print("*");
if ((j % 10) == 9) {
Serial.println();
}
}
}