Advertisement
belrey10

Random Inputs

Nov 2nd, 2024 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void setup() {
  2.   Serial.begin(9600);
  3.   Serial.println("Hello, what is your name?");
  4.  
  5.   while (Serial.available() == 0) { // stops program from continuing until Serial.available is 0, or the user inputs something.
  6.      //wait for user to input data
  7.   }
  8.   String usersName = Serial.readString();
  9.   Serial.println("Nice to meet you, " + usersName);
  10.  
  11.   //ageFunFact(usersName);
  12.   coinFlip(usersName);  
  13.  
  14.   Serial.println("Thanks for playing!");
  15. }
  16.  
  17. void loop() {
  18.  
  19. }
  20.  
  21.  
  22. void coinFlip(String yourName){
  23.   Serial.print("Lets play a game, " + yourName);
  24.   Serial.println("I'm going to flip a coin. Input '1' for heads, or '0' for tails.");
  25.   randomSeed(analogRead(0));
  26.   int coinflip = random(2); // 0 for tails, 1 for heads.
  27.  
  28.   String result;
  29.   if (coinflip == 0) {
  30.     result = "Tails";
  31.   }
  32.   else {
  33.       result = "Heads";
  34.   }
  35.  
  36.   while (Serial.available() == 0) {
  37.   //Serial.available is 0, or the user inputs something.
  38.      //wait for user to input data
  39.      //Serial.println("DEGBUG GOT HERE");
  40.   }
  41.   int guess = Serial.parseInt();
  42.  
  43.   if (guess == coinflip) {
  44.     Serial.println("Nice Job! Your guess of " + result + " was correct!");
  45.   }
  46.   else {
  47.     Serial.println("Darn! Your guess was incorrect. The correct answer was "+ result + ".");
  48.   }
  49. }
  50.  
  51. void ageFunFact(String yourName){
  52.   Serial.print("Time for a fun fact, " + yourName);
  53.   Serial.println("How old are you right now?");
  54.  
  55.     while (Serial.available() == 0) {
  56.      //  Serial.available is 0, or the user inputs something.
  57.      //  wait for user to input data
  58.   }
  59.   int usersAge = Serial.parseInt();
  60.   int usersAgePlusTen = usersAge+10;
  61.   Serial.println("Wow! Did you know that in 10 years, you will be exactly " + String(usersAgePlusTen) + " years old?");
  62.   Serial.println("");
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement