skizziks_53

Reddit electronic dice sketch v1.0

Apr 14th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. /*
  2.    April 14, 2019
  3.    Reddit electronic dice sketch
  4.    This is a electronic dice program for an Uno with a generic 16x2 LCD + keypad shield.
  5.    (the generic shield has resistive buttons, this is not for the Adafruit shield)
  6. */
  7.  
  8.  
  9. // void Catan() { ------------------------------------------ this was wrong. Everything that was inside this, is not supposed to be inside a function.
  10.  
  11.  
  12. // I am assuming here that you are using a generic 16x2 LCD + keypad shield, since you are using analogRead() to obtain the button values.
  13. // It is always a good idea to state in the sketch what parts you used.
  14. // Also it is good to give web links to where you obtained libraries, if they are not standard ones included in the Arduino IDE.
  15.  
  16.  
  17. // All the statements below--library include statements, hardware declaration statements, global variable declarations and define statements--must be declared at the root level (not inside any functions).
  18.  
  19. #include <LiquidCrystal.h> // <---------------- standard Arduino IDE library
  20. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  21. int randNumber = 0;
  22. // define some values used by the panel and buttons
  23. int lcd_key     = 0;
  24. int adc_key_in  = 0;
  25. #define btnRIGHT  0
  26. #define btnUP     1
  27. #define btnDOWN   2
  28. #define btnLEFT   3
  29. #define btnSELECT 4
  30. #define btnNONE   5
  31.  
  32.  
  33.  
  34.  
  35. // read the buttons
  36. int read_LCD_buttons() {
  37.   adc_key_in = analogRead(0);      // read the value from the sensor
  38.   // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  39.   // we add approx 50 to those values and check to see if we are close
  40.   //if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  41.   // For V1.1 us this threshold
  42.  
  43.   if (adc_key_in > 1000) {
  44.     return btnNONE;
  45.   }
  46.  
  47.   // For V1.0 comment the other threshold and use the one below:
  48.  
  49.   if (adc_key_in < 50)   {
  50.     return btnRIGHT;
  51.   }
  52.  
  53.   if (adc_key_in < 195)  {
  54.     return btnUP;
  55.   }
  56.  
  57.   if (adc_key_in < 380)  {
  58.     return btnDOWN;
  59.   }
  60.  
  61.   if (adc_key_in < 555)  {
  62.     return btnLEFT;
  63.   }
  64.  
  65.   if (adc_key_in < 790)  {
  66.     return btnSELECT;
  67.   }
  68.  
  69.   return btnNONE;  // when all others fail, return this...
  70. }
  71.  
  72.  
  73.  
  74.  
  75. // setup() cannot be placed inside any other function.
  76. void setup() {
  77.   lcd.begin(16, 2);              // start the library
  78.   lcd.setCursor(0, 0);
  79.   lcd.print("press select to dice"); // print a simple message
  80.   randomSeed(analogRead(1));
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. // loop() cannot be placed inside any other function.
  88. void loop() {
  89.   long diceOne = random(1, 7);
  90.   long diceTwo = random(1, 7);
  91.   int total = diceOne + diceTwo;
  92.  
  93.   lcd_key = read_LCD_buttons();  // read the buttons
  94.  
  95.   if (lcd_key == btnSELECT) {
  96.     lcd.clear();
  97.     lcd.setCursor(7, 0);
  98.     lcd.print(diceOne);
  99.     lcd.setCursor(7, 1);
  100.     lcd.print(diceTwo);
  101.  
  102.     analogRead(lcd_key == btnSELECT);
  103.     delay(100);
  104.     analogRead(lcd_key == btnSELECT);
  105.     delay(100);
  106.   }
  107.  
  108.  
  109.  
  110.   // The first if() statement below is not a valid way to compare multiple values.
  111.   // if (total == 2, 3, 4, 5, 6, 8, 9, 10, 11, 12) {
  112.   // What you are attempting to do here is check if a given value is in a set (not a range, since you want to exclude #7).
  113.   // The Arduino IDE does not have any built-in way to do that.
  114.  
  115.   // Each separate value would need its own condition, like so:
  116.   // if (total == 2 || total == 3 || total = 4 || total == 5 || total == 6 || total == 8 || total == 9 || total == 10 || total == 11 || total == 12) {
  117.  
  118.   // But even that isn't necessary, since you are checking for every possible value of total except for 7.
  119.   // It would be easier to just use an if()--else() statement, and check for the value of 7 first.
  120.  
  121.   // See further below:
  122.   /*
  123.     (this is the original code)
  124.     if (total == 2, 3, 4, 5, 6, 8, 9, 10, 11, 12) {
  125.       lcd.clear();
  126.       lcd.setCursor(7, 0);
  127.       lcd.print(total);
  128.     }
  129.  
  130.     if (total == 7) {
  131.       lcd.clear();
  132.       lcd.setCursor(7, 0);
  133.       lcd.print("7");
  134.       lcd.setCursor(3, 1);
  135.       lcd.print("The robber");
  136.     }
  137.   */
  138.  
  139.   // Below is the way to check for value==7 first:
  140.   if (total == 7) {
  141.     // If value is 7, this gets done.
  142.     lcd.clear();
  143.     lcd.setCursor(7, 0);
  144.     lcd.print("7");
  145.     lcd.setCursor(3, 1);
  146.     lcd.print("The robber");
  147.   }
  148.   else {
  149.     // If value is anything other than 7, this gets done.
  150.     lcd.clear();
  151.     lcd.setCursor(7, 0);
  152.     lcd.print(total);
  153.   }
  154.  
  155.  
  156.  
  157.   // I don't know what the part below is supposed to do, but the conditions in the if() statement are not valid.
  158.   // And in a 16x2 LCD, you would not have enough room to print all the given text anyway.
  159.   /*
  160.     if (total == 7 > 1) { <---------------------------------- This is not a valid comparison expression. What is this supposed to be comparing total to?
  161.       lcd.clear();
  162.       lcd.setCursor(7, 0);
  163.       lcd.print("2,3,4,5,6,8,9,10,11,12"); // <----------------------------------- This line was missing the ending semicolon. And it is too long to show on the LCD anyway.
  164.     }
  165.   */
  166. }
  167.  
  168.  
  169.  
  170. // } ---------------------------------------- this was wrong.
  171. //                                            This was the closing brace of the void Catan() function, but
  172. //                                            all that stuff should not have been inside any function anyway.
  173. //                                            (anything declared inside a function only has scope inside that function; it gets 'erased' once the function is exited)
Add Comment
Please, Sign In to add comment