// 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; //initializes it with the value 'false'
bool invalidInput = false; //initializes it with the value 'false'
int inputNumber1 = 0;
int inputNumber0 = 0;
int i = 0;
char CheckString(char); // declare the function
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
}
}
void loop()
{
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
buf[i++] = inChar;
if (!isDigit(inChar))
{
invalidInput = true;
}
}
}
if (stringComplete)
{
// check \n location
if ( i > 3 ) //When the value exceeds ten digits, will show incorrect input
{
invalidInput = true;
}
if (invalidInput){
Serial.print(buf); // the printing of string will be stopped when zero is reached
Serial.print("Incorrect input");
Serial.print("\n");
invalidInput = false;
}
else{
//When the value is close to or equal to ten digits
if (i == 2)
{
inputNumber0 = (int) buf[0] - 0x30;
Serial.print(buf);
}
else {
inputNumber1 = (int) buf[0] - 0x30;
inputNumber0 = (int) buf[1] - 0x30;
Serial.print(buf);
}
if (inputNumber1 > 0)
//When the tens digit is greater than zero
{
for (int j = 0; j <= inputNumber1 - 1; j++) {
Serial.print("**********\n");
}
}
if (inputNumber0 > 0)
//When the single digit is greater than zero
{
for (int j = 0; j <= inputNumber0 - 1; j++) {
Serial.print("*");
}
Serial.print("\n");
}
}
stringComplete = false;
i = 0;
inputNumber0 = 0;
inputNumber1 = 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);
}