Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- April 14, 2019
- Reddit electronic dice sketch
- This is a electronic dice program for an Uno with a generic 16x2 LCD + keypad shield.
- (the generic shield has resistive buttons, this is not for the Adafruit shield)
- */
- // void Catan() { ------------------------------------------ this was wrong. Everything that was inside this, is not supposed to be inside a function.
- // I am assuming here that you are using a generic 16x2 LCD + keypad shield, since you are using analogRead() to obtain the button values.
- // It is always a good idea to state in the sketch what parts you used.
- // Also it is good to give web links to where you obtained libraries, if they are not standard ones included in the Arduino IDE.
- // 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).
- #include <LiquidCrystal.h> // <---------------- standard Arduino IDE library
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
- int randNumber = 0;
- // define some values used by the panel and buttons
- int lcd_key = 0;
- int adc_key_in = 0;
- #define btnRIGHT 0
- #define btnUP 1
- #define btnDOWN 2
- #define btnLEFT 3
- #define btnSELECT 4
- #define btnNONE 5
- // read the buttons
- int read_LCD_buttons() {
- adc_key_in = analogRead(0); // read the value from the sensor
- // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
- // we add approx 50 to those values and check to see if we are close
- //if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
- // For V1.1 us this threshold
- if (adc_key_in > 1000) {
- return btnNONE;
- }
- // For V1.0 comment the other threshold and use the one below:
- if (adc_key_in < 50) {
- return btnRIGHT;
- }
- if (adc_key_in < 195) {
- return btnUP;
- }
- if (adc_key_in < 380) {
- return btnDOWN;
- }
- if (adc_key_in < 555) {
- return btnLEFT;
- }
- if (adc_key_in < 790) {
- return btnSELECT;
- }
- return btnNONE; // when all others fail, return this...
- }
- // setup() cannot be placed inside any other function.
- void setup() {
- lcd.begin(16, 2); // start the library
- lcd.setCursor(0, 0);
- lcd.print("press select to dice"); // print a simple message
- randomSeed(analogRead(1));
- }
- // loop() cannot be placed inside any other function.
- void loop() {
- long diceOne = random(1, 7);
- long diceTwo = random(1, 7);
- int total = diceOne + diceTwo;
- lcd_key = read_LCD_buttons(); // read the buttons
- if (lcd_key == btnSELECT) {
- lcd.clear();
- lcd.setCursor(7, 0);
- lcd.print(diceOne);
- lcd.setCursor(7, 1);
- lcd.print(diceTwo);
- analogRead(lcd_key == btnSELECT);
- delay(100);
- analogRead(lcd_key == btnSELECT);
- delay(100);
- }
- // The first if() statement below is not a valid way to compare multiple values.
- // if (total == 2, 3, 4, 5, 6, 8, 9, 10, 11, 12) {
- // 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).
- // The Arduino IDE does not have any built-in way to do that.
- // Each separate value would need its own condition, like so:
- // if (total == 2 || total == 3 || total = 4 || total == 5 || total == 6 || total == 8 || total == 9 || total == 10 || total == 11 || total == 12) {
- // But even that isn't necessary, since you are checking for every possible value of total except for 7.
- // It would be easier to just use an if()--else() statement, and check for the value of 7 first.
- // See further below:
- /*
- (this is the original code)
- if (total == 2, 3, 4, 5, 6, 8, 9, 10, 11, 12) {
- lcd.clear();
- lcd.setCursor(7, 0);
- lcd.print(total);
- }
- if (total == 7) {
- lcd.clear();
- lcd.setCursor(7, 0);
- lcd.print("7");
- lcd.setCursor(3, 1);
- lcd.print("The robber");
- }
- */
- // Below is the way to check for value==7 first:
- if (total == 7) {
- // If value is 7, this gets done.
- lcd.clear();
- lcd.setCursor(7, 0);
- lcd.print("7");
- lcd.setCursor(3, 1);
- lcd.print("The robber");
- }
- else {
- // If value is anything other than 7, this gets done.
- lcd.clear();
- lcd.setCursor(7, 0);
- lcd.print(total);
- }
- // I don't know what the part below is supposed to do, but the conditions in the if() statement are not valid.
- // And in a 16x2 LCD, you would not have enough room to print all the given text anyway.
- /*
- if (total == 7 > 1) { <---------------------------------- This is not a valid comparison expression. What is this supposed to be comparing total to?
- lcd.clear();
- lcd.setCursor(7, 0);
- 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.
- }
- */
- }
- // } ---------------------------------------- this was wrong.
- // This was the closing brace of the void Catan() function, but
- // all that stuff should not have been inside any function anyway.
- // (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