Hi, I am trying to print letters in serial mon and save in a global array.
For example, I would use Serial.print("How do you spell one?") and I want the user to be able to type in the answer ("one") and save the answer.
Can anyone guide me in the right direction as I am new to programming?
Actually, if the answers are all words, I would use a state machine to recognise the responses.
In this example, the state is defined by the question number and the answer character expected next. The specification is a little vague, but I'm sure this code can be modified to suit your assignment.
char questions[][40] = {"Spell 1?", "What chases cats?", "How many 'i's in Mississipi?", "Type a dollar sign:"}; //questions must be less than 40 chars long
char answers[][10] = {"one", "dog", "4", "$"}; //answers should all be lowercase and up to 9 chars long
const int numQuestions = sizeof(questions) / sizeof(questions[1]);
const int Special_Value_Ask_The_Question = -1; //this is assigned to the charBeingRecognised to force the next step to print the question
int questionNum = 0; //start at question zero
int charBeingRecognised = Special_Value_Ask_The_Question;
void setup() {
Serial.begin(9600);
while (!Serial && millis() < 10000); //wait for Serial Monitor to connect (Native-USB Arduinos only)
Serial.println("Question machine version 1.0 starting...");
}
void loop() {
if (charBeingRecognised == Special_Value_Ask_The_Question) {
//ask the question
Serial.print(questions[questionNum]);
//now we wait for the first character
charBeingRecognised = 0;
} else {
//we're in the middle of recognising an answer
if (Serial.available()) {
//a new character has arrived
char c = Serial.read();
Serial.write(c); //echo the character to the screen
if (lowerCase(c) == answers[questionNum][charBeingRecognised]) {
//sucessfully matched, move along to look for the next character
charBeingRecognised++;
if ('\0' == answers[questionNum][charBeingRecognised]) {
//we reached the null char on the end of the string - the answer was correct
Serial.println();
Serial.println("Correct!");
//ignore any extra characters following the correct answer. If the answer was "dog" and the user typed "dogs" then we accept that.
waitForSerialInputToClear();
//move along to the next question
questionNum++;
charBeingRecognised = Special_Value_Ask_The_Question;
//have we finished all the questions yet?
if (questionNum >= numQuestions) {
Serial.println("You got all questions correct. Well done!");
Serial.println();
//what do we do next? The specification doesn't say. Let's go back to asking the first question again
questionNum = 0;
}
}
} else {
//char didn't match.
//Depending on the Serial Monitor settings, the computer may be sending newline characters which we wish to ignore
if (c == '\r' || c == '\n') {
//ignore this character
} else {
//wrong character and not on the ignore list. The answer is wrong.
Serial.println();
Serial.println("Wrong answer. Try again.");
charBeingRecognised = Special_Value_Ask_The_Question;
//If using the Arduino Serial Monitor, there may be a number of characters queued up to send.
//If we immediately ask the question again, those characters will generate more "wrong!" messages.
waitForSerialInputToClear();
}
}
} //else nothing available on Serial. Just wait.
}
//additional processing can go here - check inputs, light the lights, whatever
}
void waitForSerialInputToClear() {
//At 9600, we expect a new character every 10 milliseconds or so.
//If there's a gap longer than that, then the outgoing buffer from the Serial Monitor is empty: the user stopped typing
const unsigned long CharacterDelayTime = 12;
unsigned long startOfWait = millis();
while (millis() - startOfWait < CharacterDelayTime) {
if (Serial.available()) {
Serial.read(); //discard the character
startOfWait = millis(); //re-start the time
}
}
}
char lowerCase(char c) {
//return the lowercase version of any letter
//leave all other characters untouched
if(c>='A' && c<='Z') c = c-'A'+'a';
return c;
}