/*LCD1602
You should now see your LCD1602 display the result of rolling a d20
Created by L. M.
2018.9.17*/
#include <LiquidCrystal.h>// include the library code
char array1[] = "Side yy!"; //the string to print on the LCD
char array2[] = "You rolled a xx!"; //the string to print on the LCD
int tim = 50; //the value of delay time
int num = 0;
int randNumber = 0;
int keyIn = 2;
int switchDie = 3;
int number = 1;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
void setup()
{
lcd.begin(16, 1); // set up the LCD's number of columns and rows:
pinMode(keyIn, INPUT_PULLUP);
pinMode(switchDie, INPUT_PULLUP);
Serial.begin(9600);
Serial.print("button press");
lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
lcd.setCursor(15, 1); // set the cursor to column 15, line 1
for (int positionCounter1 = 0; positionCounter1 < 8; positionCounter1++)
{
/*printf(positionCounter1);*/
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.
//lcd.print(array1[positionCounter1]); // Print a message to the LCD.
delay(tim); //wait for 50 ms
}
}
void loop()
{
int stat = digitalRead(keyIn); //store value read from keyIn
int die = digitalRead(switchDie);
if(stat == LOW) // check if the pushbutton is pressed
{
num = 1;
}
else
{
num = 0;
}
//Serial.println(num); // print the num on serial monitor
Serial.print("button press");
if (die == LOW)
{
number = 1;
if((number ++) == 6)
{
//number = 1;
}
}
//lcd.print(array1[position counter1])
//lcd.print(04)
if(num == 1) //when pushbutton is pressed
{
switch (number)
{
case 1: //d4
randNumber = random(1,5); //store random between 1 and 4
lcd.clear();
lcd.setCursor(15, 1);
lcd.scrollDisplayLeft();
lcd.print("Side 04!");
delay(tim);
break;
case 2: //d6
randNumber = random(1,7); //store random between 1 and 6
lcd.clear();
lcd.setCursor(15, 1);
lcd.scrollDisplayLeft();
lcd.print("Side 06!");
delay(tim);
break;
case 3: //d8
randNumber = random(1,9); //store random between 1 and 8
lcd.clear();
lcd.setCursor(15, 1);
lcd.scrollDisplayLeft();
lcd.print("Side 08!");
delay(tim);
break;
case 4: //d10
randNumber = random(1,11); //store random between 1 and 10
lcd.clear();
lcd.setCursor(15, 1);
lcd.scrollDisplayLeft();
lcd.print("Side 10!");
delay(tim);
break;
case 5: //d12
randNumber = random(1,13); //store random between 1 and 12
lcd.clear();
lcd.setCursor(15, 1);
lcd.scrollDisplayLeft();
lcd.print("Side 12!");
delay(tim);
break;
case 6: //20
randNumber = random(1,21); //store random between 1 and 21
lcd.clear();
lcd.setCursor(15, 1);
lcd.scrollDisplayLeft();
lcd.print("Side 20!");
delay(tim);
break;
default: //cant get here
randNumber = 42;
break;
}
array2[13] = (randNumber / 10) + 0x30;
array2[14] = (randNumber % 10) + 0x30;
lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
lcd.setCursor(15, 1); // set the cursor to column 15, line 1
for (int positionCounter2 = 0; positionCounter2 < 16; positionCounter2++)
{
/*printf(positionCounter2);*/
lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.
lcd.print(array2[positionCounter2]); // Print a message to the LCD.
delay(tim); //wait for 250 ms
}
}
}
The goal of this program is to do two things.
The button tied to the #2 pin will roll a random number for a selected dice.
The button tied to the #3 pin will allow you to select a dice. Every button push is supposed to select the next button up.
e.g. D4 push D6 push D8 , D10 , D12 , D20
So far, rolling a dice works well, unfortunately it is stuck on the D4.
I have been looking online before posting here but I can't seem to find a solution to this.
Help very much appreciated!
Note: The code does not throw any errors at this time.