Pseudo-Code

 

Main Module:

 

Description:

Initializes the bowling game and contains the main event detection loop

 

Pseudocode Implementation:

 

main()

            Initialize_Game()  [Event_Stages module]

            repeat Forever:

                        Event_Stages()  [Event_Stages module]

                        (this function contains all of the event checking code)

            end Repeat

end Function

 

 

Event_Stages Module:

 

Description:

Contains the main event checking and response routines, initializes and resets the game, and keeps track of primary game variables (score, number of tries left, difficulty, target pin, and current stage of game). 

 

Module variables:

 

Functions:

 

Pseudocode Implementation:

 

Module variables:

stage, difficulty, score, target, num_tries

 

Event_Stages()

            switch stage

                        case PENNY_INSERT:

                                    if Penny_Insert() returns 1  [Penny_Sensor module]

                                    (this means that a penny was detected)

                                                if the first penny was inserted, Reset_Score()

                                                set stage equal to PRE_START

                                    end if

                        case PRE_START:

set the difficulty variable using Get_Difficulty()  [Pre_Start module]

                                     if Start_Press() returns 1 [Pre_Start module]

                                    (this means that the start button was pressed)

select the appropriate pin target and set the target variable using Select_Target() [Get_Impact module]

                                                set stage equal to COUNT_RELEASE

                                    end if

                        case COUNT_RELEASE:

                                    if Countdown_Release() returns 1 [Count_Release module]

                                    (this means that the countdown is complete)

                                                set stage equal to GET_IMPACT

end if

case GET_IMPACT:

                                    if Get_Impact() returns 1 [Get_Impact module]

                                    (this means that a hit or a miss was registered)

                                    Increase_Score()

                                    Decrease_Num_Tries()

                                    set stage equal to PRIZE_DISPENSE

                        case PRIZE_DISPENSE:

                                    if Prize_Dispense() returns 1 [Prize_Dispensor module]

                                                set stage equal to PENNY_INSERT

                                    else if Prize_Dispense() returns 2 [Prize_Dispensor module]

                                                Reset_Game()

                                                set stage equal to PENNY_INSERT

                                    end if

            end switch

end function

 

Initialize_Game()

            Initialize the timer

            Initialize_PreStart()  [Pre_Start module]

            Initialize_PennyInsert()  [Penny_Sensor module]

            Initialize_CountRelease()  [Count_Release module]

            Initialize_Impact()  [Get_Impact module]

            Initialize_Score()  [Score_Controller module]

            Initialize_Timer()  [Timer_Controller module]

            Initialize_Prize()  [Prize_Dispense module]

            Initialize_LEDs()  [LED_Controller module]

end function

 

Reset_Game()

     set num_tries equal to 3

            light the num_tries LEDs using Light_Num_Tries() [LED_Controller module]

end function

 

Reset_Score()

            set the score variable equal to zero

            Clear_Score()  [Score_Controller module]

end function

 

Decrease_Num_Tries()

            subtract one from the num_tries variable

            light the num_tries LEDs using Light_Num_Tries() [LED_Controller module]

end function

 

Increase_Score()

get the last score obtained from the last pin hit or miss using the Get_Last_Score() function [Get_Impact module]

use Pulse_Score() to increase the score accordingly [Score_Controller module]

end function

 

 

Count_Release Module:

 

Description:

Contains event checking and response code for use when the game is in the COUNT_RELEASE stage. 

 

Functions:

 

Pseudocode Implementation:

 

Initialize_CountRelease()

            set port AD5 to be low (solenoid off)

end function

 

 

 

Countdown_Release(difficulty)   

            static local variables:

countdown_timer, prev_time, flag

local variable:

current_time

 

            set current_time equal to the current time

            if this is the first iteration (flag is not 0)

                        set the prev_time equal to the current_time

                        countdown_timer = difficulty*1000

                        indicate that this is no longer the first iteration (flag = 0)

            end if

 

countdown_timer -= (current_time - prev_time)

set the prev_time equal to the current_time

 

            if (countdown_timer > 0)

                        display the time remaining in countdown_timer using Set_Timer()                       [Timer_Controller module]

                        return 0

            else

                        Clear_Timer()  [Timer_Controller module]

                        activate the solenoid: set AD5 high

pause for 500 milliseconds using pause()  [Utility module]

                        deactivate the solenoid (set AD5 low)

set flag variable equal to 1

            return 1

end if

end function

 

 

Get_Impact Module:

 

Description:

Contains event checking and response code for use when the game is in the GET_IMPACT stage. 

 

Module variable:

 

Functions:

 

Pseudocode Implementation:

 

Module variable:

score_add

 

Initialize_Impact ()

            set the T port data direction register to low (input)

            initialize port AD6 low (buzzer off)

end function

 

Get_Impact(difficulty, target)

            static local variables:

            flag, release_time, roll_time

            local variables:

            current_time, bit

 

            if the function is being called for the first time this try (flag is not zero)

                        set release_time to be the current time

                        set flag equal to zero

                        set the score_add variable to zero

                        return 0

            end if

 

            store the current time as current_time

 

if current_time - release_time >= roll_time  (the timer has expired)

                        turn the target LED off using LED_Off()  [LED_Controller module]

                        turn the miss LED on using LED_On()  [LED_Controller module]

                        Activate_Buzzer()

                        reset the first iteration flag (flag = 1)

                        score_add = 0

                        return 1

            end if

 

            *check all of the switches to detect an impact*

            repeat for each switch (iteration variable is i):

                        set the test bit equal to 2^i power

                        if the switch has not been activated (port T & bit is at least 1)

                                    do nothing

                        else (the switch has been activated)

                                    if the correct switch was hit (target is equal to i)

                                                turn the target LED_Off()  [LED_Controller module]

                                                turn the hit LED_On()  [LED_Controller module]

                                                adjust the score:  score_add = 300 - (difficulty-1)*50

                                                flash the target LED:

                                                repeat 10 times:

                                                            turn target LED_On()[LED_Controller module]

pause() for 100 milliseconds  [Utility module]

                                                            target LED_Off()[LED_Controller module]

pause() for 100 milliseconds  [Utility module]

                                                end repeat

                                                reset the first iteration flag (flag = 1)

                        return 1

            else  (an incorrect switch was hit)

turn the miss LED_On()  [LED_Controller module]

turn the target LED_Off()  [LED_Controller module]

            Activate_Buzzer()

            reset the first iteration flag (flag = 1)

            score_add = 0

                                                return 1

                                    end if

                        end if

            end repeat

return 0

end function

 

Select_Target ()

select a random pin using the current time and a modulus 8 operator (since there are 8 pins)

Pin_LEDs_Off()  [LED_Controller module]

turn the selected LED_On()  [LED_Controller module]

return the randomly selected pin

end function

 

Get_Last_Score ()

            return score_add

end function

 

Activate_Buzzer()

            Repeat 10 times:  (to pulse buzzer at resonance frequency)

                        Set AD6 high (buzzer on)

                        pause() for 1 millisecond  [Utility module]

                        Set AD6 low (buzzer off)

                        pause() for 1 millisecond  [Utility module]

            end repeat

            Set AD6 high (buzzer on)

            pause() for 400 milliseconds  [Utility module]

            Set AD6 low (buzzer on)

end function

 

 

LED_Controller Module:

 

Description:

Controls the lighting of all LEDs in the bowling game. 

 

Module variables:

 

Functions:

 

Pseudocode Implementation:

 

Module variables:

Num_LED, LED_Array[]

 

Initialize_LEDs()

            set port AD3 low

            set port AD4 low

            Clear_LEDs()

            light the number of tries LEDs using Light_Num_Tries()

end function

 

LED_On(index)

            set given array index of LED_Array[] equal to 1

            Load_Registers()

end function

 

LED_Off(index)

set given array index of LED_Array[] equal to 0

            Load_Registers()

end function

 

Clear_LEDs()

            Repeat Num_LED times:

                        set the current indec of the LED_Array[] equal to 0

            end repeat

            Load-Registers()

end function

 

Load_Registers()

            repeat Num_LED times:

                        if the current index of LED_Array[] is zero

                                    set port AD3 low

                        else

                                    set port AD3 high

                        end if

                        pulse the clock  :  set port AD4 high, then set it low again

            end repeat

            set port AD3 to zero

end function

 

Pin_LEDs_On()

            repeat 8 times for the 8 bowling pin LEDs:

                        set the current index of LED_Array[] to 1

            end repeat

            Load_Registers()

end function

 

Pin_LEDs_Off()

            repeat 8 times for the 8 bowling pin LEDs:

                        set the current index of LED_Array[] to 0

            end repeat

            Load_Registers()

end function

 

Flash_Pin_LEDs(Num_flashes)

            repeat Num_flashes times:

                        Pin_LEDs_On()

                        pause() for 200 milliseconds [Utility module]

                        Pin_LEDs_Off()

                        pause() for 200 milliseconds [Utility module]

            end repeat

end function

 

Light_Num_Tries(numtries)

            repeat 3 times for the 3 LEDs representing number of tries:

                        set the current LED_Array[] element to 0

                        if numtries >= current repeat index

                                    set the current LED_Array[] element to 1

                        end if

            end repeat

            Load_Registers()

end function

 

Clear_HitMiss()

            Set the miss indicator element of LED_Array[] to 0

            Set the hit indicator element of LED_Array[] to 0

            Load_Registers()

end function

 

 

Penny_Sensor Module:

 

Description:

Contains event checking and response code for use when the game is in the PENNY_INSERT stage. 

 

Functions:

 

Pseudocode Implementation:

 

Initialize_PennyInsert()

            set data direction register M5 low (the penny sensor input)

end function

 

Penny_Insert()

Static local variable:

            prev_state

            Local variables:

            current_state, state_change

 

            store the current_state of the penny sensor by testing port M5

            detect a state_change using the State_Change() function [Utility module]

            set prev_state equal to current_state

            if state_change is equal to 2 (the penny was inserted)

                        Flash_Pin_LEDs() twice  [LED_Controller module]

                        Clear_Hit_Miss()  [LED_Controller module]

                        return 1

            end if

            return 0

end function

 

 

Pre_Start Module:

 

Description:

Contains event checking and response code for use when the game is in the PRE_START stage.   

 

Module variables:

 

Functions:

 

Pseudocode Implementation:

 

Module variables:

last_diff, uncertainty

 

Initialize_PreStart()

            set data direction register for port M4 to low (input for start button)

initialize the AD interface and configure so that AD0 is an analog input, and the rest of the ports are outputs

            initialize all AD output ports to be zero

end function

 

Start_Press()

Static local variable:

            prev_state

            Local variables:

            current_state, state_change

 

            store the current_state of the start button by testing port M4

            detect a state_change using the State_Change() function [Utility module]

            set prev_state equal to current_state

            if state_change is equal to 1 (the start button was pressed)

                        Flash_Pin_LEDs() 5 times  [LED_Controller module]

                        set last_diff equal to 3

                        return 1

            end if

            return 0

end function

 

Get_Difficulty()

Local variable:

ADRead

   

            store the current state of pin AD0 as ADRead   

            if ADRead < 44 - uncertainty

                        Set_Timer() to 1  [Timer_Controller module]

                        last_diff = 1

                        return 1

             else if ADRead >= 44+uncertainty && ADRead < 88-uncertainty

Set_Timer() to 2  [Timer_Controller module]

                        last_diff = 2

                        return 2

            else if ADRead >= 88 + uncertainty

                        Set_Timer() to 3  [Timer_Controller module]

                        last_diff = 3

                        return 3

            else  (we are in between difficulties; leave difficulty the same)

                        Set_Timer to last_diff [Timer_Controller module]

                        return last_diff

            end if

end function

 

 

Prize_Dispense Module:

 

Description:

Contains event checking and response code for use when the game is in the PRIZE_DISPENSE stage.   

 

Module variable:

 

Functions:

 

 

Pseudocode Implementation:

 

Module variable:

            quarter_rot

 

Initialize_Prize()

            set port AD1 to low (data bit of shift register)

            set port AD2 to low (clock bit of shift register)

            Clear_Register()

end function

 

Prize_Dispense(numtries, score)

            Local variable:

            prize_rots

           

set prize_rots according to Prize_Rotations() function

if numtries is zero

                        repeat prize_rots times:  (number of 90 degree rotations)

repeat quarter_rots times:  (number steps to rotate 90 degrees)

                                                set port AD1 high

                                                repeat 4 times (4 clock pulses):

                                                            set port AD2 high

                                                            pause() for 15 milliseconds [Utility module]

                                                            set port AD2 low

                                                            if this is the first clock pulse

                                                                        set AD1 low

                                                            end if

                                                end repeat

                                    end repeat

                        end repeat

*we need to switch between 12 and 13 steps every other time to avoid offset problems, since the stepper motor has 200 steps, so 50 steps rotate the motor 90 degrees, and 50/4 = 12.5*

                        if quarter_rot is currently 12

                                    quarter_rot = 13

                        else

                                    quarter_rot = 12

                        end if

Clear_Register()

                        return 2

else

return 1

            end if

end function

 

 

Clear_Register()

            set port AD1 to low

            repeat 9 times:  (to be safe; only 8 times are needed for 8 parallel outputs)

                        set port AD2 high

                        pause() for 1 millisecond [Utility module]

                        set port AD2 low

            end repeat

end function

 

Prize_Rotations(score)

if score >= 700

                        return 3

             else if score >= 450 

return 2

            else if score >= 200

                        return 1

            else

                        return 0

            end if

end function

 

 

Score_Controller Module:

 

Description:

Controls the lighting of the three 7-segment LED displays responsible for displaying the score. 

 

Functions:

 

Pseudocode Implementation:

 

Initialize_Score()

     set data direction register for M0 to high (counter chip clear output)

            set data direction register for M1 to high (counter chip clock output)

            set port M0 high

            set port M1 low

            Clear_Score()

            Pulse_Score() once

            Clear_Score()

end function

 

Clear_Score()

            set port M0 high

            pause() for 1 millisecond  [Utility module]

            set port M0 low

end function

 

Pulse_Score(pulses)

            repeat ‘pulses’ times:

                        set port M1 low

                        pause() for 1 millisecond  [Utility module]

                        set port M1 high

            end repeat

end function

 

 

Timer_Controller Module:

 

Description:

Controls the lighting of the 7-segment LED display responsible for displaying the timer.    

 

 

Module Variable:

 

Functions:

 

 

Pseudocode Implementation:

 

Module Variable:

prev_time

 

Initialize_Timer()

            set data direction register for M2 to high (counter chip clear output)

            set data direction register for M3 to high (counter chip clock output)

            set port M2 high

            set port M3 low

            Clear_Timer()

end function

 

Clear_Timer()

            set port M2 high

            pause() for 1 millisecond  [Utility module]

            set port M2 low

end function

 

Set_Timer(time_left)

            if prev_time is equal to time_left

                        return

            Clear_Timer()

            repeattime_left’ times:

                        set port M3 low

                        pause() for 1 millisecond  [Utility module]

                        set port M3 high

            end repeat

            prev_time = time_left

end function

 

 

Utility Module:

 

Description:

Provides two useful general-purpose functions that are used in several of the other modules.

 

 

Functions:

 

Pseudocode Implementation:

 

pause(miliseconds)

            store the current time in a temporary variable ‘x’

            while (current time – x) < milliseconds

                        do nothing

            end while

end function

 

State_change(prev_state, current_state)

if (prev_state is zero and current_state is 0)

return 1

else if (prev_state is not 0 and  current_state is 0)

return 2

else

return 0

            end if

end function

 

Full Code Listing

 

Return to Main Page