Help with programing arduino

Hello,
I am trying to make a digital dice for my project in school. I am having troubles with the programming, I don't really know how to write code and I am not a programmer and my school isn't really helpful with this.

I took inspiration from a post on the Arduino website (Arduino Digital Dice | Arduino Project Hub) where someone already made the project that I'm trying to make. I connected the LCD and the Arduino UNO the same way that is shown on the schematic (I hope its connected correctly) as well as the button and potentiometer. I just used a smaller breadboard but as far as I know that shouldn't change anything.

The code that I used was the same as the one from the already made project. The problem is that when I upload the code to the Arduino, it doesn't actually roll any numbers, it just says that its rolling and thats it.

In the code I need it to roll or randomly generate numbers from 1 to 12. If possible I would like to make it so that it has different modes where if for example if I pressed the button twice it would change the mode to generate numbers from 1 to 6.

I am now asking for someone to help me out atleast a little bit, if someone could tell me if I connected something incorrectly or if the code isn't good or if there is a better code I could use here.

If its any help to know I am using an Arduino UNO R3, LCD display (Module no: DEM 16216 SYH-PY), a normal pushbutton and a 10k potentiometer.

Here are the schematics and some pictures;



It won't allow me to upload more than 3 pictures so if the pictures don't look too good maybe I can reply with a picture where you can see the connections better if that is an option...

This the code that I used for the programing;

#include <LiquidCrystal.h>
long randNumber;
int Led = 13; //define  LED port
int Shock = 2; //define shock port
int val;//define digital variable  val
// initialize the library with the numbers of the interface pins
LiquidCrystal  lcd(7, 8, 9, 10, 11, 12 );
byte customChar[] = {
  B00000,
  B00000,
  B11111,
  B11001,
  B10101,
  B10011,
  B11111,
  B00000
};
void  setup()
{
  
  lcd.begin(16, 2);
  lcd.createChar(0, customChar);
  lcd.home();
  pinMode(Led, OUTPUT); //define LED as a output port
  randomSeed(analogRead(0));
  pinMode(Shock, INPUT); //define shock sensor as a output port
  lcd.write(byte(  0));
  lcd.print("Digital dice");
  lcd.write(byte( 0));
  delay(1000);
}

void  loop()
{
  
  val = digitalRead(Shock); //read the value of the digital  interface 3 assigned to val
  if (val == LOW) //when the shock sensor have signal  do the following
  {
     lcd.clear();
     lcd.print("Rolling dice...");
     delay(4000);
     lcd.clear();
     lcd.setCursor(0, 0);
     randNumber  = random(1,7);
     lcd.print("Dice 1 = ");
     lcd.print(randNumber);
     
     lcd.setCursor(0, 1);
     randNumber = random(1,7);
     lcd.print("Dice  2 = ");
     lcd.print(randNumber);
     
  }
 
 delay(150);
}

I appreciate anyone that will help in any way!

You might want to look at this How to get the best out of this forum before you proceed any further.

It tells you how to ask a question here.
We need code, which you provided but we also need a schematic which you didn't. That link to redit is totally unacceptable here.

2 Likes

Youll also need this for the report about your project

1 Like

Alright thank for letting me know, Im not really used to posting on forums like this...
I edited the post so I hope now it looks better and I added the schematic

Thank you as well for letting me know how to add it, as you can see i'm not good at writing in forums lol

  • Cannot get my head around the way you have the power rails on the breadboard wired to 0v and 5v.
    Please, the standard is: Red plus 5v, Black/Blue GND.

  • Looks like the 10k resistor on the switch might not be wired properly.

  • Confirm how you have the LCD contrast potentiometer wired.

2 Likes

Well, here's something similar I did for a Hallowe'en Dragon Battle quest game that I abandoned one year since it was going to prove impractical to my overall haunt flow.
In any case, this is some rough (I do mean rough - be nice forum fellows :joy: ) but maybe it can help point you in the right direction. Set your serial monitor to 115200 baud, no components required as I hadn't added in any button presses or such to this, I was just working on a D&D style dragon battle that at this stage, was virtually unwinnable (but that's another story)
I recommend, if you choose to to play around with this code, that you make a second copy and make all your changes to that copy so you can roll back to the one that just runs through a few simulated dice rolls.
Good luck.

int count = 0;

int dieOne; // for build up sequence
int dieTwo; // for build up sequence
int rollingDice; // for build up sequence

int d1;
int d2;
int d3;
int d4;
int d5;
int d6;
int d7;
int d8;
int d9;
int d10;
int d11;
int d12;

int d2Roll;
int d3Roll;
int d4Roll;
int d5Roll;
int d6Roll;
int d7Roll;
int d8Roll;
int d9Roll;
int d10Roll;
int d11Roll;
int d12Roll;

int playerDamage = 0;

int dragonDamage = 0;

int hpPlayer;
int hpDragon;

int dragonCounter = 0;
int playerCounter = 0;


void setup() {
  Serial.begin(115200);
  longLineSpacing();
  delay(100);
  Serial.println("dragonEngine_ver3");
  longLineSpacing();
  hpPlayer = 100;
  hpDragon = 10000;
  displayPlayerHP();
  displayDragonHP();
  delay(1000);
  randomSeed(analogRead(0));
}

void loop() {
  if (hpDragon <= 0) {
    deadDragon();
  }

  if (hpPlayer <= 0) {
    deadPlayer();
  }

  playerDamage = playerRollsDefault();

  // player misses
  if (playerDamage <= 4) {
    shortLineSpacing();
    Serial.println("Player Missed!");
    shortLineSpacing();
    delay(5000);
  }
  // player critical hit
  if (playerDamage == 12) {
    shortLineSpacing();
    Serial.println("Critical Hit on Dragon!");
    shortLineSpacing();
    delay(1000);
    Serial.print("10x Attack Damage: ");
    Serial.println(d2Roll * 10);
    shortLineSpacing();
    delay(5000);
    dragonCounter = hpDragon - (d2Roll * 10);
    Serial.print("Dragon Hit Points: ");
    Serial.println(dragonCounter);
    shortLineSpacing();
    hpDragon = dragonCounter;
  }
  // player regular hit
  else if (playerDamage > 4) {
    shortLineSpacing();
    Serial.print("Player Attack Damage: ");
    Serial.println(d2Roll);
    shortLineSpacing();
    delay(5000);
    dragonCounter = hpDragon - d2Roll;
    Serial.print("Dragon Hit Points: ");
    Serial.println(dragonCounter);
    shortLineSpacing();
    hpDragon = dragonCounter;
  }

  hpDragon = hpDragon - playerDamage;

  dragonDamage = dragonRolls();

  if (dragonDamage <= 16) {
    shortLineSpacing();
    Serial.println("Dragon Missed!");
    shortLineSpacing();
    delay(5000);
  }

  else if (dragonDamage > 16) {
    shortLineSpacing();
    Serial.print("Dragon Queen Attack Damage: ");
    Serial.println(d12Roll);
    shortLineSpacing();
    delay(5000);
    playerCounter = hpPlayer - d12Roll;
    Serial.print("Player Hit Points: ");
    Serial.println(playerCounter);
    shortLineSpacing();
    hpPlayer = playerCounter;
  }
}


int playerRollsDefault() {
  Serial.println("Player Rolls 2d6!");
  Serial.println("");
  buildUp();
  Serial.println("");
  d1 = random(1, 7);
  d2 = random(1, 7);
  d2Roll = (d1 + d2);
  Serial.print("You Rolled: ");
  Serial.print("d1: ");
  Serial.print(d1);
  Serial.print("   d2: ");
  Serial.println(d2);
  Serial.println("");
  delay(1500);
  return (d2Roll);
}

int dragonRolls() {
  Serial.println("Dragon Rolls 12d6!");
  Serial.println("");
  buildUp();
  Serial.println("");
  d1 = random(1, 7);
  d2 = random(1, 7);
  d3 = random(1, 7);
  d4 = random(1, 7);
  d5 = random(1, 7);
  d6 = random(1, 7);
  d7 = random(1, 7);
  d8 = random(1, 7);
  d9 = random(1, 7);
  d10 = random(1, 7);
  d11 = random(1, 7);
  d12 = random(1, 7);
  d12Roll = (d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + d10 + d11 + d12);
  Serial.print("Dragon Queen Rolled: ");

  Serial.print("  d1: ");
  Serial.print(d1);
  Serial.print("  d2: ");
  Serial.print(d2);
  Serial.print("  d3: ");
  Serial.print(d3);
  Serial.print("  d4: ");
  Serial.print(d4);
  Serial.print("  d5: ");
  Serial.print(d5);
  Serial.print("  d6: ");
  Serial.print(d6);
  Serial.print("  d7: ");
  Serial.print(d7);
  Serial.print("  d8: ");
  Serial.print(d8);
  Serial.print("  d9: ");
  Serial.print(d9);
  Serial.print("  d10: ");
  Serial.print(d10);
  Serial.print("  d11: ");
  Serial.print(d11);
  Serial.print("  d12: ");
  Serial.println(d12);
  Serial.println("");
  delay(1500);
  return (d12Roll);
}

void displayPlayerHP() {
  shortLineSpacing();
  delay(2000);
  Serial.println("Player HP: 100");
  Serial.println("");
  Serial.println("");
  delay(2000);
}
void displayDragonHP() {
  shortLineSpacing();
  delay(2000);
  Serial.println("Dragon HP: 10000");
  Serial.println("");
  Serial.println("");
  delay(2000);
}

void buildUp() {
  int countDelay = 220;
  int setColumn = 0;
  for (count = 0; count <= 16; count++) {
    dieOne = random(1, 7);
    dieTwo = random(1, 7);
    rollingDice = dieOne + dieTwo;
    Serial.print("..");
    Serial.print(rollingDice);
    delay(countDelay);
    countDelay -= 13;
    setColumn += 1;
  }
  Serial.println("");
}

void deadDragon() {
  longLineSpacing();
  for (int i = 0; i < 240; i++) {
    Serial.println("               THE DRAGON QUEEN IS DEAD");
    delay(15000);
  }
  hpDragon = 10000;
  longLineSpacing();
  displayPlayerHP();
  displayDragonHP();
}

void deadPlayer() {
  longLineSpacing();
  Serial.println("               YOU HAVE PERISHED...");
  delay(3000);
  Serial.println("               ...PERHAPS ANOTHER MAY ONE DAY SAVE THE REALM...");
  delay(30000);
  hpPlayer = 100;
  longLineSpacing();
  displayPlayerHP();
  displayDragonHP();
}

void shortLineSpacing() {
  for (int i = 0; i < 3; i++) {
    Serial.println("");
  }
}

void longLineSpacing() {
  for (int i = 0; i < 9; i++) {
    Serial.println("");
  }
}



/* ************  UNIQUE TREASURE CHESTS AND WEAPONS MODIFIER LIST  *****************************

  TREASURES--

  The Treasure of Friendship (find the Diminutive Unicorns to learn their magical language)
** CAP THE UNICORN WAND IN DRAGON BATTLE UNLESS PLAYER HAS "LEARNED"
  UNICORN--EITHER HAVE THIS ARTIFACT OR SOMETHING WITH A UNICORN ON IT
  (BOX #1)

  The Treasure of Insight (find the All-Seeing Eye and learn the Dragon's Secret)
** THIS PLAYER WILL NOT HAVE TO GUESS WHICH WAND TO CHANNEL POWER
  TO TO OPEN DRAGON CHEST, THEY WILL BE TOLD WHICH ONE OPENS IT
  (BOX #2)

  The Treasure of Tenacity (find the Skull Roses)
** THIS PLAYER WILL GET TO TRY ALL WANDS ON DRAGON CHEST IF THEY HAVE THIS ARTIFACT
  (BOX #3)

  The Treasure of Death (find the Butterfly Skull to gain passage through the Valley of the Undead)
** THIS PLAYER MAY CHOOSE TO SKIP THE BATTLE OF THE GHOULS SPONGE TOSS
  (BOX #4)

  The Treasure of Osiris (find the Ankh Necklace to restore Life)
** PARTY GETS EXTRA DRAGON BATTLE CHANCE AS LIFE IS RESTORED ONCE
  (BOX #5 )

  The Treasure Of Davy Jones (find the Pirate Necklace for passage via sea to the Chamber of the Witch)
** PLAYER WILL HAVE PLUS +BATTLE DAMAGE MODIFIER TO BATTLE DRAGON
  (BOX #6  )

  The Treasure of Beauty (find the Jeweled Lace Bracelet to be Blessed by the Witch)
** IF PLAYER IS WEARING THIS IN CHAMBER OF THE WITCH, THEY WILL BE GIVEN A KEY
  (BOX #7  )

  WEAPONS--

  The Sword Of the Paladin K'tash (find the Sword of Leonidas for godlike battle damage)
** THIS PLAYER GETS EXTRA 3D6 (BASE 3d6 SUPREME GHOUL VS 2d6 PLAYER + 1d6 EACH IN PARTY)
  GAME OF DICE AT GHOUL ANIMATRONIC BY SACRIFICE CIRCLE (BODY PARTS) and EXTRA 1d6 vs
  DRAGON MINION and ALLOWS PLAYER TO DICE BATTLE DRAGON QUEEN (NO MODIFIER)


*/
1 Like
  • I also wasn't sure if the wiring for the power is correct, but on the schematic that I found it was wired the same way and it also worked so I didn't really think much about it.... I will switch it if it helps or changes anything.

  • I will look into that and will wire the switch differently and see what happens

  • So for the potentiometer I wired the first and third pin to 5V and GND, the middle pin is connected to the 3rd pin on LCD (thats pin V0)

Thanks! I will look at what I can do with this.
Appreciate the help!

1 Like

It looks like you are one of the increasing number of people who can't hold more that one question at a time in your head.
But please try and answer this one as it could be vital to you seeing anything on the display:-

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.