Dice Roller With Lcd

by 733417 in Circuits > Arduino

30 Views, 0 Favorites, 0 Comments

Dice Roller With Lcd

IMG_4282.jpg

This is a curcuit in which a dice rolls and chooses a random number from 1-6. Then it uses code to determine what the number is and if it is even or odd.

Wiring Push Button -> 555 Timer -> Decade Counter ->leds -> Arduino

IMG_4281.jpeg
IMG_4282.jpg

First you ground and power the push button, 555 timer, decade counter and leds. You connect the push button to pin 8 of the 555 timer and then connect pin 2 to pin 6. You then connect pin 2 and 3 with resister and pin 2 to ground with capacitor. Then you connect pin 3 of 555 timer to pin 15 of decade counter. Finally you connect 15 to pin 5. Then you connect the decade counter to pins to the leds. Your dice roller should start working but you also connect the leds 1-6 to pins [13, 12, 11, 10, 9, 8] of arduino to start with the code. Then you wire the lcd to the arduino to use later. You connect Vcc to power, Gnd to Ground, Sda to A4 and Scl to A5

Code Part 1

image_2024-06-18_083809619.png

The provided Arduino code initializes an array `ledPins` containing pins connected to LEDs (pins 13 to 8) and sets pin 6 (`checkButtonPin`) as an input with a pull-up resistor. Upon setup, it configures these pins accordingly, initiates serial communication at 9600 baud, and prompts via the serial monitor to press `checkButtonPin` to determine the current LED number (1-6). The `readCurrentLedNumber()` function iterates through `ledPins`, identifying which LED is lit by checking each pin's state (`HIGH`), and updates `currentLedNumber` accordingly. When `checkButtonPin` is pressed, the function prints whether the detected LED number is even or odd, providing real-time feedback based on the LED's status and button press. This setup enables interactive monitoring of LED states and user engagement via the serial interface for immediate feedback on LED status and parity. I also declared the I2C lcd screen to use to print later on

Code Part 2

image_2024-06-18_084544082.png

In the `loop()` function, the code continuously monitors whether the button connected to `checkButtonPin` is pressed (`LOW` state). When the button is detected as pressed, it calls `readCurrentLedNumber()` to determine which LED is currently lit based on the pins defined in `ledPins`. It then evaluates whether the detected LED number (`currentLedNumber`) is even or odd using the modulo operator (`%`). If the LED number is even, it outputs a message to the lcd and serial monitor stating that the LED number followed by "is even." If the LED number is odd, it prints that the LED number followed by "is odd." After printing the result, the code waits for the button to be released using a `while` loop that checks if `checkButtonPin` is still `LOW`, ensuring debounce and preventing multiple detections from a single button press. Finally, a small delay of 200 milliseconds (`delay(200)`) further debounces the input, ensuring accurate button press detection and preventing rapid iterations through the loop due to physical button bounce. This structure allows for real-time feedback on the parity (even or odd) of the currently lit LED whenever the button is pressed, facilitating interactive monitoring and user interaction via the serial interface.