Main Module                        

Description

Contains the main() function and the game structure.

Implementation

Declare input variables

·        Coin sensor

·        Drop Button

·        Left Toggle button

·        Right Toggle button

·        Right Limit switch

·        Left Limit switch

·        Penny Detector Encoder outputs

Declare output variables

·        Sound output

·        Stepper outputs

·        Swag open output

·        Swag close output

·        Prize LED shift register clock input

·        Prize LED shift register data input

·        Game Timer LED shift register clock input

·        Game Timer LED shift register data input

Declare timer variables

·        TrackTimer will manage how often we run stepper tracking

·        GameTimer will manage how long we have to make a selection

·        LeftToggleDebounceTimer will manage debounce timing of the left toggle button.

·        RightToggleDebounceTimer will manage debounce timing of the right toggle button.

·        SoundPlayTimer will monitor how long the sound should have been played so we can turn it off early.

·        GameTimerLED will manage the timing of the countdown Game Timer LED's.

Declare other variables:

·        CurrentTime will be used for performing timer logic

·        leftlatch will be used for running code only once per button push

·        rightlatch will be used for running code only once per button push

·        InitReturn will be used for evaluating whether the Analog library was configured correctly.

·        EncoderResult is used for evaluating where the penny dropped.

Initialize Timer variables

Set TrackTimer.Delay to 7 = 14ms.

Set LeftToggleDebounceTimer.Delay and RightToggleDebounceTimer.Delay to 25 = 50ms.

The Sound plays for approximately 20seconds.  Set SoundPlayTimer.Delay<20 seconds so we don't retrigger it.  9500=19seconds.

Set GameTimer.Delay to 14000=28seconds

Set GameTimerLED.Delay to 1666=3.333 seconds

Initialize IO variables

Configure the coin sensor input at port M, bit 4.

Configure the drop button input at port AD, bit 5.

Configure the left toggle button input at port E, bit 0.

Configure the right toggle button input at port E, bit 1.

Configure the sound play output at port T, bit 0.

Configure the right limit switch input at port T, bit 3.

Configure the left limit switch input at port T, bit 2.

Configure the stepper outputs at port T, bits 4-7.

Configure the encoder inputs at port AD, bits 1-4.

Configure the swag open output at port M, bit 2.

Configure the swag closeoutput at port M, bit 3.

Configure the prize LED clock output at port M, bit 0.

Configure the prize LED data output at port M, bit 0.

Configure the game timer LED clock output at port AD, bit 6.

Configure the game timer LED data output at port AD, bit 7.

Initialize libraries

Initialize analog library so that pin AD0 is analog, AD1-AD5 is input, AD6-AD7 is output.

Initialize TIMERS12 library.

Initialize Stepper library.

Initialize Swag library.

Initialize PrizeLED libary.

Initialize GameTimerLED library.

 

Begin Game Code

 

Repeat

Set Playsound output high.

            Reset Stepper to right limit switch

            Reset prize LEDs

            Reset game timer LEDs

           

Wait for coin sensor to see the coin

           

Reset prize LEDs

            Reset game timer LEDs

Play Sound

            Wait 200 ms to allow coin to fully fall into dropper before proceeding

            Set sound play output high after it is triggered.

           

Reset timers to current time

·        TrackTimer

·        GameTimer                       

·        SoundPlayTimer

·        GameTimerLED

 

Begin non-blocking loop.  All code within may not use Wait().

 

While the drop button is not depressed

Set CurrentTime equal to the current time using TMRS12_GetTime()

 

If the tracking timer has expired

                                    Execute stepper tracking

Update the timer with CurrentTime

 

If the GameTimerLED timer has expired

Count down on the LED's by 1

Update the timer with CurrentTime

 

If the GameTimer timer has expired

Leave this while loop.

 

If the Left toggle button is not pushed

Update the LeftToggleDebounceTimer with CurrentTime.

Reset leftlatch.

Otherwise, if it is being pushed

If the LeftToggleDebounceTimer has expired and leftlatch is reset

Shift the prize LEDs left one slot.

set leftlatch to prevent running this if statement from running more than once per button push

 

If the right toggle button is not pushed

Update the RightToggleDebounceTimer with CurrentTime.

Reset rightlatch

Otherwise, if it is being pushed

If the RightToggleDebounceTimer has expired and rightlatch is reset

Shift the prize LEDs right one slot.

Set rightlatch to prevent running this if statement from running more than once per button push

 

If Sound Timer hasn’t reached 20s (sound still playing)

Trigger the sound play output to stop the sound.

                        Wait 100 ms

                        Set the sound play output high.

 

Repeat CheckSlot until see the penny pass through one of the bottom Slot

If this is one of the winning slots

        Drop Swag

 

Forever