Skip to content

Instantly share code, notes, and snippets.

@ledlogic
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ledlogic/328e6956b795cb88e2f3 to your computer and use it in GitHub Desktop.
Save ledlogic/328e6956b795cb88e2f3 to your computer and use it in GitHub Desktop.
LCDDiceRoller.iso
/*************************************************************************************
Dieroller
Uses the library TrueRandom from https://code.google.com/p/tinkerit/wiki/TrueRandom
(c) 2015 Jeff D. Conrad
**************************************************************************************/
#include <LiquidCrystal.h>
#include <TrueRandom.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // select the pins used on the LCD panel
// 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
int read_LCD_buttons() { // read the 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
// We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in > 1000) return btnNONE;
// For V1.1 us this threshold
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
// 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;
}
void setup() {
lcd.begin(16, 2);
showTitle();
}
void showTitle() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Diceroller");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print("[d100][d10]");
lcd.setCursor(13, 1);
lcd.print(millis() % 1000);
lcd.setCursor(0, 1);
lcd_key = read_LCD_buttons();
switch (lcd_key) {
case btnLEFT: {
doLeft();
break;
}
case btnRIGHT: {
doRight();
break;
}
}
}
void doLeft() {
long d = TrueRandom.random(1, 100);
drawRoll(d, 100);
}
void doRight() {
long d = TrueRandom.random(1, 10);
drawRoll(d, 10);
}
void drawRoll(long d, long r) {
lcd.setCursor(0, 1);
String out = String(d) + "/" + String(r);
while (out.length() < 16) {
out = out + " ";
}
lcd.print(out);
delay(2000);
showTitle();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment