Created
April 5, 2022 08:40
-
-
Save knowblesse/f3964ca677f193cbc01491ebe66e551d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TRPG Die simulation program | |
// @2021 Knowblesse | |
#include <TM1637.h> | |
#define CLK 6 | |
#define DIO 5 | |
#define L_button 3 | |
#define R_button 2 | |
#define Top_button 4 | |
TM1637 tm1637(CLK, DIO); | |
int currentDiceIndex; | |
int Dice[] = {2, 3, 4, 5, 6, 8, 10, 12, 20, 100}; // total 10 | |
int8_t data[] = {0x7f, 0x7f, 0x7f, 0x7f}; | |
int timer[] = {300, 200, 150, 120, 90, 30}; | |
bool button_pressed = false; | |
void setup() { | |
// put your setup code here, to run once: | |
tm1637.set(7); | |
tm1637.init(); | |
pinMode(2, INPUT); | |
pinMode(3, INPUT); | |
pinMode(4, INPUT); | |
randomSeed(analogRead(0)); | |
currentDiceIndex = 4; | |
showOption(Dice[currentDiceIndex]); | |
} | |
void loop() { | |
if(digitalRead(Top_button)) | |
{ | |
delay(100); | |
if(digitalRead(Top_button)) | |
{ | |
if(!button_pressed) | |
{ | |
button_pressed = true; | |
for(int t=0; t<6; t++) | |
{ | |
if(!digitalRead(Top_button)) break; // stop flickering | |
tm1637.clearDisplay(); | |
delay(timer[t]); | |
showOption(Dice[currentDiceIndex]); | |
delay(timer[t]); | |
} | |
displayNumber(random(1, Dice[currentDiceIndex]+1)); | |
} | |
} | |
} | |
else | |
{ | |
if(button_pressed) | |
{ | |
button_pressed = false; | |
tm1637.clearDisplay(); | |
delay(100); | |
showOption(Dice[currentDiceIndex]); | |
} | |
if(digitalRead(L_button)) | |
{ | |
if(currentDiceIndex == 0) currentDiceIndex=9; | |
else currentDiceIndex--; | |
showOption(Dice[currentDiceIndex]); | |
delay(200); | |
} | |
if(digitalRead(R_button)) | |
{ | |
if(currentDiceIndex == 9) currentDiceIndex=0; | |
else currentDiceIndex++; | |
showOption(Dice[currentDiceIndex]); | |
delay(200); | |
} | |
} | |
} | |
void showOption(int i) | |
{ | |
tm1637.clearDisplay(); | |
int8_t _data[] = {13, 0x7f, 0x7f, 0x7f}; | |
if (i < 10) | |
{ | |
_data[3] = i; | |
} | |
else if (i < 100) | |
{ | |
_data[2] = int(i/10); | |
_data[3] = i % 10; | |
} | |
else | |
{ | |
_data[1] = 1; | |
_data[2] = 0; | |
_data[3] = 0; | |
} | |
tm1637.display(_data); | |
} | |
void displayNumber(int i) | |
{ | |
tm1637.clearDisplay(); | |
int8_t _data[] = {0x7f, 0x7f, 0x7f, 0x7f}; | |
if (i < 10) | |
{ | |
_data[3] = i; | |
} | |
else if (i < 100) | |
{ | |
_data[2] = int(i/10); | |
_data[3] = i % 10; | |
} | |
else | |
{ | |
_data[1] = int(i/100); | |
_data[2] = int((i % 100)/10); | |
_data[3] = i % 10; | |
} | |
tm1637.display(_data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment