Ever had a hard decision, but didn’t know how to go about deciding? Well, fear not! In this project, you’ll be building a program that will give you a yes or no answer to any question that you ask it.
Step 1
Code some chaos
void setup() {
Serial.begin(9600);
Serial.println(" oooo ");
Serial.println(" `888 ");
Serial.println(" .oooo. .oooo.o 888 oooo .oooo. oooo oooo ooo .oooo. oooo ooo ");
Serial.println("`P )88b d88( \"8 888 .8P' `P )88b `88. `88. .8' `P )88b `88. .8' ");
Serial.println(" .oP\"888 `\"Y88b. 888888. .oP\"888 `88..]88..8' .oP\"888 `88..8' ");
Serial.println("d8( 888 o. )88b 888 `88b. d8( 888 `888'`888' d8( 888 `888' ");
Serial.println("`Y888""8o 8""888P' o888o o888o `Y888""8o `8' `8' `Y888""8o .8' ");
Serial.println(" .o..P' ");
Serial.println(" `Y8P' ");
Serial.println("Ask me a question and I will give you a yes/no answer\n"); } void loop() { randomSeed(analogRead(0));
if(Serial.available() > 0) {
Serial.print(Serial.readString());
Serial.println();
delay(1000);
if(random(0, 2) == 0) Serial.println("I say yes."); // The 'YES' message
else Serial.println("I say no."); // The 'NO' message
Serial.println();
delay(1000);
Serial.println("Now ask me something else\n");
}
}
Step 2
Ask away
Open the ‘Serial Monitor’
Click on the button shown in the image to open the Serial Monitor.
Send your message
Type in a message and click ‘Send’. The Decision Maker will respond!
Step 3
Modify the madness
Change the decision messages
Giving a simple yes or no answer to all of your questions is a bit boring, don’t you think? At the moment, when a question is being answered, you get a pretty plain response. If the answer is ‘yes’, then it just says: “I say yes.”, and nothing more.
if(random(0, 2) == 0) Serial.println("I say yes."); // The 'YES' message
To edit this response, all you need to do is change the text inside the quotation marks. For example, if I wanted to respond with, “The stars tell me that the answer to your question is yes.”, I would need to use the following code:
if(random(0, 2) == 0) Serial.println("The stars tell me that the answer to your question is yes."); // The 'YES' message
But that’s only the response when the answer to a question is ‘yes’. We can also edit the response to questions with a ‘no’ answer. The code for this can be found on line 18.
else Serial.println("I say no."); // The 'NO' message
Like with our the previous response, all we have to do to change this one is to change the text inside the quotation marks.