How do you feel about this article? Help us to provide better content for you.
Thank you! Your feedback has been received.
There was a problem submitting your feedback, please try again later.
What do you think of this article?
Introduction and Objectives
Nowadays one of the trendiest games is WORDLE, which consists of guessing a hidden word in six attempts. In each attempt the player will introduce a five-letter word and below this will appear clues to guess the hidden word. In case the player doesn’t guess in six words the correct one, the game will be over. The following project is based on this game.
In contrast to the traditional game that uses a colour code to represent whether the letter is contained in the word or if it is in the correct place, we will use a symbol-based code as it is detailed below:
- Dot (“.”): the letter is not in the hidden word.
- Underclass (“_”): the letter is in the hidden word but not in the correct position.
- Asterisk (“*”): the letter is in the hidden word and in the correct place.
Furthermore, the game will also have a time limit. We will select the difficulty using a potentiometer as follows.
- Easy level: 0V - 0.83V at 8 minutes
- Hard level: 0.83V – 4.17V at 4 minutes
- Extreme level: 4.17V – 5V at 2 minutes
8 LEDs from the MicrochipTM Mechatronics Development board will turn off as time goes by. For example, in an extreme level, a LED will turn off every 15 seconds. In case all LEDs are off before finding the hidden word the game will be over and the player loses the game.
If the player guesses the word in six attempts or less and within the set time, the game is finished and the player wins. When this happens, a melody will sound through a piezo-buzzer as a sign of success.
Connections to the Microcontroller
The components needed to develop the project are the following:
- PIC16F877A microcontroller from Microchip
- PICDEM 2 PLUS development board from Microchip
- PICDEM™ Mechatronics development board from Microchip
- USART module with Hercules program
- In-circuit debugger/programmer (ICD)
- Computer with MPLAB IDE software
The microcontroller has been programmed in C using MPLAB integrated development environment. The program code is attached at the end of the article.
The connections of the modules with the microcontroller are shown in Figure 1 below:
Figure 1. Microcontroller connections
Therefore, we have the following inputs and outputs:
- Analog input Potentiometer
- Digital input
- Digital output: Buzzer RC2 and LEDs PORTD
Figure 2. Circuit connection
Modules used and Configuration
The program that controls the hardware uses the following modules integrated into the microcontroller architecture: Timer0, Timer1, CCP2, CAD and USART:
In the initial configuration are defined the libraries to use htc.h, stdio.h, stdlib.h, usart.h; the frequency of the internal oscillator has been set to 4MHZ and finally the configuration used by the microcontroller has been included using the following config statement:
__CONFIG(WRT_OFF & WDTE_OFF & PWRTE_OFF & FOSC_XT & LVP_OFF)
RBO, TIMER0, CAD, CCP2 y USART are handled using interruptions. Thus, the corresponding interruptions have been enabled in the INTCON, PIE1 and PIE2 registers.
Below are the global variables used in the program sorted by types
CHAR: this type of variable is used to store dimension 5 vectors with different functions.
- palabra: stores the hidden word
- jugador: stores the Word introduced by the player
- simbolos: stores symbols that indicate the game tracks
- pot: stores input value of potentiometer (analogue)
INT: Variables of this type are counters and signalers in loops.
- cont,cont1,cont: are employed to count the time in the interruption of the Timer0
- fin: indicates if the word has been correct, thus ending the game.
- fintiempo: indicates whether the playing time is over or not.
- intentos: stores the number of attempts that have been used. Ranges from 0 to 6.
- jugando: is used to go through the different game screens.
- modo_actual and modo_anterior: control the game mode to be played and if necessary print the change of difficulty
- i: variable used to reproduce sounds by the buzzer.
Inputs and Outputs
All terminals on D and C ports have been configured as outputs. The eight terminals of the PORTD are used to illuminate the LEDs of the mechatronics board. The RC2 terminal is used by the PORTC to control the buzzer and the RC6 and RC7 terminals for the USART module.
On the other hand, on port A, the input of the RA0 terminal has been configured as an analogue input. Finally, in terminal B, the RB0 pushbutton has been configured as a digital input.
TIMER 0 is configured as a timer (Ti = 4/FOSC), with a prescaler of P = 256 and has a TMR0=22 stored to timer a total of 60ms. Timer 0, handled by interruption, is used to count the total play time and the start time of the LEDs.
The game will start pushing RB0. Then, each time the interrupt of TMR0 is handled cont is increased. When cont==50, the cont1 will decrease. Each time this cont1 reaches 0, the cont2 is decreased from 8 to 0 and the LEDs() function is called to change the illumination of the LEDs.
Then, the counters are recharged according to the difficulty level. The lower the numbers stored in the counters, the shorter the game and, therefore, the higher the difficulty. The table below shows the counter values for each level, the total duration of the game and how often the LEDs turn off.
Module CCP2, Timer1
The CCP2 module is in compare mode. When the comparison with the TIMER1 is positive, the CCP2IF interrupt is raised, the TIMER1 is reset and the A/D conversion is launched, as this module is enabled.
For the interrupt to be generated to launch an analog to digital conversion every 500ms and get the potentiometer valued, CCPR2=62500 is stored. TIMER1 is configured with a Prescaler P=8 and the internal clock Ti=FOSC/4.
ANALOG-TO-DIGITAL CONVERTER (A/D) MODULE
When the A/D conversion is complete, the A/D interrupt flag bit ADIF is set and the voltage value is stored in the pot variable. The potentiometer is read by AN0, that is, by the conversion channel 0.
The A/D module has been configured using ADCON0 register with a clock for the conversion of Fosc/8, channel 0 (AN0) and module on. ADCON1 register sets all inputs associated with the A/D module as analog. The A/D result format is right justified, with the six most significant bits of ADRESH read as ‘0’.
The resolution of the A/D converter of the PIC is 10bits, that is, 4.88mV/bit, therefore, the reading of the potentiometer obtains a number between 0 and 1024 (2n=210). This value is multiplied to obtain a value between 0 and 5V and then scaled between 0 and 60.
The configuration and use of the USART module has been done using the usart.h library. The module is configured at 9600 baud, in asynchronous mode of 8 bits at high speed.
The module transmits and receives information through the COM1 port to the computer using the Hercules HyperTerminal program running on the computer to show the results of the communication.
Diagrams of Functionality
WORDLE program can be explained using block diagrams. There are two parts: The main program block diagram and the Interruption service routine block diagram.
Main program block diagram:
Interruption service routine (ISR) block diagram: