Advertisement
learnelectronics

Arduino Magic 8 Ball

Jun 27th, 2018
1,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. /*
  2.  Magic 8 Ball
  3.  
  4.  learnelectronics
  5.  25 Jun 2018
  6.  
  7.  www.youtube.com/c/learnelectronics
  8.  
  9.  
  10.  */
  11.  
  12.  
  13. #include <LiquidCrystal.h>                                      // include the library code:
  14.  
  15.                                                                 // initialize the library with the numbers of the interface pins
  16. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  17.  
  18. bool myswitch = 1;                                              // create Boolian varibale for switch
  19.  
  20. void setup() {
  21.  
  22.   lcd.begin(16, 2);                                             // set up the LCD's number of columns and rows:
  23.  
  24.  
  25. pinMode(8,INPUT_PULLUP);                                        //set pin 8 as input with internal pullup
  26.  
  27. }
  28.  
  29. void doit(){                                                    //declare function for when switch is activated
  30.   randomSeed(analogRead(0));                                    //seed random num generator
  31.   int randNumber = random(5);                                   //get random number between 0 and 5
  32.  
  33.   lcd.setCursor(0, 1);                                          //reset cursor
  34.   lcd.print("                ");                                //clear display
  35.   lcd.setCursor(0, 1);                                          //reset cursor
  36.   switch (randNumber) {                                         //switch case on random number
  37.   case 0:                                                       //send answer to lcd
  38.     lcd.print("YES!");
  39.     break;
  40.   case 1:
  41.     lcd.print("No");
  42.     break;
  43.     case 2:
  44.     lcd.print("Maybe");
  45.     break;
  46.     case 3:
  47.     lcd.print("Ask me later");
  48.     break;
  49.     case 4:
  50.     lcd.print("Answer Unclear");
  51.     break;
  52.     case 5:
  53.     lcd.print("Just order pizza");
  54.     break;
  55.  
  56. }
  57. }
  58.  
  59. void loop() {
  60.   myswitch = digitalRead(8);                                      //read pin 8 and send result to variable
  61.   if (myswitch == 0){                                             //if the pin is low
  62.     doit();                                                       //call function doit
  63.   }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement