Software

Link to view C Code

Link to download C Code

The C code to control the microcontroller is extremely complex and is about 50 pages in length. The code can be broken down into the following sections: Dart Detection-Polling, Video Generation, Button Polling, Drawing Menus-Games, and Controlling Menus-Games.

Dart Detection

 

The C code to control the microcontroller is extremely complex and is about 50 pages in length. The code can be broken down into the following sections: Dart Detection-Polling, Video Generation, Button Polling, Drawing Menus-Games, and Controlling Menus-Games.

The single most important function for the microcontroller to execute is the dart detection. The pressure sensor map was hooked up to the Mega 32 pins. A contact pad would be pressed, while the input pins were polled. The Mega would read Port A to determine which pin the output was on. The poll result was then matched to the corresponding 8 bit character map, and the input pin that generated the output was noted. The function dart_hit_update would then match the input line to the output bitmap to get the exact region hit. The variable Dartvalue was used to calculate the dartscore variable based on whether a triple, single or double region was hit. The default if the program could not calculate a specific dart region was to return a 0. The dart detection code ran continuously with video generation. The Update variable provided a means to clear the current dart hit variables, and wait for Update=yes before the dart hit variables would be loaded for game control. Update would equal Yes when there was a dart hit. Upon a dart hit and detection, Update would be set to No for 500 ms to allow for game control calculations. The dart score calculation and hit definition were carried out in a function that. The dart polling and video generation was executed in a regularly scheduled interrupt.


An issued occurred when the dart detection code was merged with the video generation code. The dart polling could not occur fast enough between screen updates, and was too slow to poll all 10 lines between line writes. The result was either missing a dart hit, or having flicker on the screen. The first complex section of code written incorporates the dart polling with the video generation.

Video Generation

The hardware section illustrates how the timing was devised for the polling. The dart polling code had to be written within the video generation code for the Atmel Mega 32 provided by Bruce Land.
//http://instruct1.cit.cornell.edu/courses/ee476/video/Video32v2.c

The video generation code for each line contained 128 no-ops. It was discovered that these no-ops could be replaced with dart polling functions, without generating flicker. A dart poll was inserted right before the display to screen was executed, and right after the display was executed. This provided approximately 2 µs/ dart poll, with 2 dart polls per video line. This provided the pins ample time to charge for a quick pulse to be detected by the output.

Button polling

Button Polling was accomplished within a simple function that could be called by the code in the main While loop. The button polling simply checked the pins on the Mega 32 for the pin that was triggered high. This would register as a single button push and was de-bounced. The user would need to lift the button before pressing it again to get a second button push, the code did not account for multiple button pushes by holding the button down.

Drawing/Controlling Menus

The menus are the first section of code displayed on the video screen. The menus and game code are controlled by a state machine within the Linecount=231 of the main While loop. The state machine is initialized to state Menu_Draw. The code for Menu_Draw writes the lines to the screen, the page title, "Select Game", and the 4 game types. Menu_Draw then sends the state to Menu, when the button presses are tracked to save which game was selected. A screen shot of the Game Menu is shown below. Note the arrow that shows the current selection.

The player menu_draw and menu code is very similar to the game menu. The only difference is that the game options are replaced by the number of players. A screen shot of this is show below.

 

Drawing/Controlling Games

Next to the dart polling during video generation, the games were the most complex element to code for. Each game took about two full days to write and fully test. Cricket and 301 were especially difficult since every attempt was made to ensure they followed the accepted dart rules for these games.

The game menus and player code is located within the state machine of the main While loop. Each game draw and game play is a different state.

The game menus were very straightforward. It took time to set up the graphics, and special considerations had to be taken to display the correct number of players. The display of the games can be found by clicking on the link 301 Cricket Grading Game Drinking Game.

301 and Cricket both contained a bounce-out function, which would allow the user to erase their last throw. These games are fairly long and sometimes serious and it was felt that adding this would be a player benefit. The down side was the increased complexity with the bounce out, especially with Cricket. It was decided that as soon as the next player button was pressed, the previous player could not decide to bounce out his last throw.
In 301 a bounce-out meant the last dart thrown was erased, including the Hit message, and the total score displayed. A bust then bounce-out was even more complicated since the bust would go back to the score before the round, but the bounce-out would have to override the bust, and just go back one dart throw. This was accomplished by making temporary variables and setting tight conditions for recalling previous scores.

A bounce-out in cricket was also extremely tricky to code for. Not only does the score and hit message have to be erased, if a Cricket region was hit (15-Bull), the symbol would have to be redisplayed to what was there before the dart hit. This was again accomplished with temporary variables.

During any game the reset button can be pushed to go back to the main menu. The reset is a state that erases all the text on the screen, while leaving the vertical and horizontal lines. Redrawing these lines generally caused some flicker, thus it was avoided.

Game notes for 301
The rules for 301 can be found on the game page. The code for 301 was basically displaying the hit region, subtracting the dart hot from the total, and incrementing the dart variable to ensure the user only gets 3 throws. A call to the message functions check to see if a special message should be displayed, "Triple, Double, Bulls, DBull." There is also a check for a total=0 to declare a winner. Included in the code is a case for Bust, when the dart throw variable is set to make and the total score is returned to the score before the round started.

Game notes for Cricket
Cricket was very complex to code for. A player array was set to 5 for each player before the game began, for players not playing it is left equal to 2. The 7 element array corresponds to every Cricket region. Every time a dart hits the cricket region, the dartvalue variable is subtracted from 5. If the hit was a double, dartvalue would equal 2. Thus the array would keep track of the player's progress. If the value was 1, 0, this meant the player had not closed the region before the dart throw, but the hit not only closed the region, but scored points. Thus, if any of the players did not close out points would be added. For simplicity, players not playing had their arrays set to close out so active players could not score against players not selected.

A winner check is carried out for each player by making sure each Cricket region in closed out, by making sure each element in the array is equal to 2. If this is the case, then the game code checks to make sure the player is greater or equal to all the scores. If this is the case then the player is the winner.

Special game graphics were made for number of times a region was hit: "/"1 hit, "X" 2 hit and "funny round thing" 3 hit.

Game notes for Drinking Game
The drinking game was much easier to code for since it does not include a bounce out function. The code simply tallies each players score from three dart throws for each round. After each player has thrown, the code calculates who has the lowest score and displays a message for them to drink. A special shot glass symbol was created. The trick with this code was resetting the totals after each round, and calculating who had the lowest score.

Game notes for Grading Game
The grading game is a single round game without the bounce out function. The code allows a person 5 dart throws, tallies the total and displays the corresponding grade.

The only thing that did not work was a high score attribute. The players cannot enter their names, and with the games provided there is no real absolute ranking system. Each dart game is relative.