Hey yall- first time posting here. I'm working on a small project that is a dnd dice roller with two buttons- One to select the dice and one to roll it. Not i'm pretty new to arduino programming so theres likely a simple solution to this- but i'll start by explaining the issue.
I can press the button to navigate from one menu to another, but when i go to roll the dice in that menu, it rolls something random.
My setup and init look like this:
(forgive my shitty commenting)
// libraries
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TrueRandom.h>
//variables
#define menu 3
// menu size
const int menuSize = 7;
String menuItems[menuSize];
int currMenu = 0;
String temp;
bool Next = HIGH;
char currentPrintOut[10];
Adafruit_SSD1306 display(4);
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // setup the OLED
pinMode(2, INPUT); // setup button 3
pinMode(3, INPUT_PULLUP); // setup button 2
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.drawRect(0, 0, 128, 8, WHITE);
display.display(); // write to display
Serial.begin(115200);
// the one array
menuItems[0] = "D2";
menuItems[1] = "D4";
menuItems[2] = "D6";
menuItems[3] = "D8";
menuItems[4] = "D10";
menuItems[5] = "D12";
menuItems[6] = "D20";
MenuChanged();
}
The function it calls at the end is:
void MenuChanged() {
display.clearDisplay();
display.drawRect(0, 0, 128, 8, WHITE);
if (currMenu < 4) {
display.setCursor(53, 15);
}
else {
display.setCursor(48, 15);
}
display.setTextColor(WHITE);
display.setTextSize(2);
temp = String (menuItems[currMenu]);
temp.toCharArray (currentPrintOut, 10);
display.println(currentPrintOut); // write the roll
Serial.println(currentPrintOut); // write the roll
display.display(); // write to display
delay(100);
}
So here is where I think its breaking- ALSO i realize there is likely an easier way to frite this, but i'm new so a bunch of if statements is what i had for now.
Main loop(likely broken):
void loop() {
if (digitalRead(3) != Next) {
Next = !Next;
if (!Next) {
//pressed
if (currMenu > 0) {
currMenu--;
}
else {
currMenu = menuSize - 1;
}
MenuChanged();
Serial.print (currMenu);
}
}
if (digitalRead(2) == HIGH) {
delay(15);
if (digitalRead(2) == HIGH) {
if (currMenu = 0) {
d2Roll();
}
}
}
if (digitalRead(2) == HIGH) {
delay(15);
if (digitalRead(2) == HIGH) {
if (currMenu = 1) {
d4Roll();
}
}
}
if (digitalRead(2) == HIGH) {
delay(15);
if (digitalRead(2) == HIGH) {
if (currMenu = 2) {
d6Roll();
}
}
}
if (digitalRead(2) == HIGH) {
delay(15);
if (digitalRead(2) == HIGH) {
if (currMenu = 3) {
d8Roll();
}
}
}
if (digitalRead(2) == HIGH) {
delay(15);
if (digitalRead(2) == HIGH) {
if (currMenu = 4) {
d10Roll();
}
}
}
if (digitalRead(2) == HIGH) {
delay(15);
if (digitalRead(2) == HIGH) {
if (currMenu = 5) {
d12Roll();
}
}
}
if (digitalRead(2) == HIGH) {
delay(15);
if (digitalRead(2) == HIGH) {
if (currMenu = 6) {
d20Roll();
}
}
}
}
I know this looks like a lot, but basically after i check if a button is pressed, the same if statement repeats itsself:
if (digitalRead(2) == HIGH) {
delay(15);
if (digitalRead(2) == HIGH) {
if (currMenu = 6) {
d20Roll();
}
}
}
it just changes per dice.
Speaking of the logic behind rolling the dice looks like this:
void d20Roll() {
display.fillScreen(BLACK); // erase all
display.drawRect(0, 0, 128, 8, WHITE);
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
int roll = TrueRandom.random(1, 21); // store the number
if (roll == 20) {
drawStar();
display.setCursor(77, 14);
display.println("20"); // daaamn yuss
}
else if (roll == 1) {
display.setCursor(87, 14);
display.println("1"); //get rekt
drawSkull();
}
else if (roll < 10) {
// single character number
d20(); // draw the outline
display.setTextColor(BLACK);
display.setCursor(60, 15);
display.println(roll); // write the roll
}
else {
// dual character number
d20(); // draw the outline
display.setTextColor(BLACK);
display.setCursor(52, 15);
display.println(roll); // write the roll
}
display.display(); // write to display
delay(100);
}
The problem is in my mind- while not perfect, the dice should roll ok- ish. But somethign is terribly wrong...