// 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;
// declare the function
// char ConvertString(char);
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
}
// for (int j = 9; j > 0; j--) {
// Serial.print('*');
// }
// Serial.println();
// PrintStars(5);
// PrintStars(22);
}
void loop()
{
int num;
while (Serial.available())
{
inChar = (char)Serial.read();
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.print("i = ");
// Serial.println(i);
// Serial.print("buf[0] = ");
// Serial.println(buf[0]);
// Serial.println(buf[0], DEC);
// Serial.print("buf[1] = ");
// Serial.println(buf[1]);
// Serial.println(buf[1], DEC);
// Serial.print("buf[2] = ");
// Serial.println(buf[2]);
// Serial.println(buf[2], DEC);
// Serial.print("buf[3] = ");
// Serial.println(buf[3]);
// Serial.println(buf[3], DEC);
// Serial.print("buf[4] = ");
// Serial.println(buf[4]);
// Serial.println(buf[4], DEC);
// Serial.println("----------");
if ((i == 2) && (isDigit(buf[0]))) {
num = buf[0] - '0';
PrintStars(num);
} else {
Serial.println("Incorrect input");
Serial.println();
}
stringComplete = false;
i = 0;
num = 0;
}
}
// 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) {
// for (int j = n; j > 0; j--) {
// Serial.print('*');
// }
// Serial.println();
// }
void PrintStars(int n) {
for (int j = 0; j < n; j++) {
Serial.print('*');
}
Serial.println();
}