Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Arduino program. The serial monitor keeps rolling and always \"invalid input\".

ID: 3572350 • Letter: A

Question

Arduino program. The serial monitor keeps rolling and always "invalid input". Can't figure out why. Help.

String username;

void setup()

{

Serial.begin(9600);

//Green LED on D3

//Red LED on D4

pinMode(3,OUTPUT);

pinMode(4,OUTPUT);

digitalWrite(3,0);

digitalWrite(4,0);

}

void loop()

{

test_username();

}

//Character array to store data recorded from serial monitor char

charBuf[5];

//Recursive function to keep prompting till right username is displayed

void test_username()

{

Serial.print("Please enter your username or ID:");

delay(2000);

if(Serial.available()>0)

{

username=Serial.readString();

username.toCharArray(charBuf, 5);

}

if(strcmp(charBuf,"Tony")==0)

{

Serial.println("Hello Student");

//Display access granted and toggle LED

digitalWrite(3,1);

digitalWrite(4,0);

delay(500);

}

else

{

Serial.println("Invalid username");

digitalWrite(3,0);

digitalWrite(4,1);

delay(500);

test_username();

}

}

Explanation / Answer

This is because

if(strcmp(charBuf,"Tony")==0) => the entered name wil be compared with Tony. hence if you give Tony as input, the program will display welconme message and stop running.