http://design.stanford.edu/spdl/SPDL-Logo-med-sml.gif         ME218a Penny Arcade       http://paincenter.stanford.edu/Stanford%20image54.jpg

 

Home

How to play

Concept

Mechanical Design

Electrical

Software

Gems of Wisdom

Meet the team  

Download

Software for the Wild Game


     This page contains pseudo code of the program for running our game.  Please note that all the C files and header files for compiling in CodeWarrior to run the C32 are available for download in the download section.

 

Pseudo Code of the Main Program

main(Takes nothing, Return nothing)

Start

  // Let the program start with ‘RESET_STATE’   

 

  Infinite Repeat Loop

  Start

      switch(Current_State)

      {

        case RESET_STATE:

            // Initialization           

            // Initially set all 7-segments LEDs as zero.

            // LCD instructions for a user

            // Waiting for coin detection.

            // When coin detected, set the new current state as COIN_STATE.

          

        case COIN_STATE:

            // Instructions using LCD

            // Receive level selection by potentiometer input.

// When low level

                {

                    // Display ‘low level’.

// Set play time is 99 seconds.

                    // Set life number is 15.

                    // Set the new Current_State as  LOW_PLAY_STATE.

                }

// When Medium level

                {

                    // Display ‘med level’.

// Set play time is 85 seconds.

                    // Set life number is 9.

                    // Set the new Current_State as  MED_PLAY_STATE.

}

// When high level

                {

                    // Display ‘high level’.

// Set play time is 75 seconds.

                    // Set life number is 7.

                    // Set the new Current_State as  HIGH_PLAY_STATE.

                }

 

// Waiting for Start button to be pressed.

// Display the life number

          

        case LOW_PLAY_STATE:

            // Make two DC motors run in opposite direction and the vibration motor off.

            // decrease the playing time and check the sensor inputs.

            // if the number of touching exceeds the life number or playing time is over,

      set the failure flag. ( Every touch causes short buzzer sound )

            // if the player succeed, set the new Current_State as SUCCESS_STATE          

            // else if the player not succeed, set the new Current_State as FAILURE_STATE     

       

         case MED_PLAY_STATE:

            // Make two DC motors run faster in the same direction and the vibration motor off.

            // decrease the playing time and check the sensor inputs.

            // if the number of touching exceeds the life number or playing time is over,

      set the failure flag. ( Every touch causes short buzzer sound )

            // if the player succeed, set the new Current_State as SUCCESS_STATE          

            // else if the player not succeed, set the new Current_State as FAILURE_STATE   

            

         case HIGH_PLAY_STATE:

            // Make two DC motors run faster in the same direction and the vibration motor on.

            // decrease the playing time and check the sensor inputs.

            // if the number of touching exceeds the life number or playing time is over,

      set the failure flag.( Every touch causes short buzzer sound )

            // if the player succeeds, set the new Current_State as SUCCESS_STATE          

            // else if the player succeeds, set the new Current_State as FAILURE_STATE

       

          case FAILURE_STATE:

            // turn off all actuators except SWAG dispenser

           // A buzzer sound that indicates the failure.

           // SWAG dispense for the looser.

           // Set the new Current_State as RESET_STATE            

 

          case SUCCESS_STATE:

            // turn off all actuators except SWAG dispenser

           // A buzzer sound that congratulate the success.

           // SWAG dispense for the winner.

           // Set the new Current_State as RESET_STATE

   Repeat Loop End

End

 

Pseudo Code of the Complete Program

// Port Initialization of C32, P8255(port extension chip)

PORTInit(Takes nothing, Returns nothing) // port Initialization function

Start

  // Set PortM as outputs

  // Set 7,6,5 bits PortT as inputs, otherwise outputs

  // Initially all outputs for data sending (PORT AD)

  // ADC Initialization

  // PWM Initialization

  // Initialization for P8255(port extension chip)

  // Clear the buffer for LCD display

  // Clear the other useful variables

End

 

 

// Port Initialization of P8255(port extension chip)

ExtensionChipInit(Takes nothing, Returns nothing) // port extension chip Initialization function

Start

        // Send the control word to the first(main) P8255

        ( Except, PortC’s 0,1,2,3, bits all other ports are to be output )

        // Send the control word to the second P8255 via the main P8255

        ( All ports are to be output )

End

 

 

// Initialization of a LCD module

LCDInit(Takes nothing, Returns nothing) //  LCD Initialization function

Start

         // LCD initialization by the Datasheet

End

 

 

// A function which allows to print a string, combination of characters on the LCD.

LCDputString(Takes start position and a printed string , Return nothing)

Start

   // Fill the character array with the given string from argument

// Print the character array to the LCD

End

 

 

// A naive delay function for not accurate and simple delay.

delay(Takes delay count, Returns nothing)

Start

        // NOP count*10 times for naive delay

End

 

 

// Accurate delay function. Delay resolution is a millisecond.

delay_ms_times(Takes delay time in millisecond , Returns nothing)

{

  // Set the timer resolution as 1 millisecond

 

// NOP for (1 millisecond) * (delay time).

}

 

 

// 'Accurate delay + event checking' function.

delay_with_service(Takes delay time in millisecond , Returns nothing)

Start

  // Set the timer resolution as 1 millisecond

 

Repeat for (1 millisecond) * (delay time).

start

     // check the current region by checking the check point sensors

             // if the stick is passing the first check point, turn on the solenoid for 3 seconds

 

// check the wire touching if the stick touches the wire or not

        repeat end

End

 

}

 

 

// A function which check the input signals of the coin detector and the start switch.

GetSignal(Takes the object pin want to check the current input, return the input value)

Start

    if want to check the coin detector

          if the value of coin detector is ‘1’

            return 1;                        // Coin detected

          else

            return 0;                        // NO COIN

    else if want to check the start switch

          if the value of the start button is ‘1’

 return 1; 

          else

            return 0;                        // Still not pushed

    else (i.e. checking the wire contact)

          if the player touches the wire

            return 1; 

          else

            return 0;                        // Not touched

    end if

End

 

 

LED_TimeDisplay(Takes two digit second, Returns nothing)

Start

    lower_digit = sec%10;

    upper_digit = sec-lower_digit;

   

    if(upper_digit == 0)

        set the upper digit LED as ‘0’

    else if(upper _digit == 10)

set the upper digit LED as ‘1’

    else if(upper _digit == 20)

        set the upper digit LED as ‘2’

    else if(upper _digit == 30)

set the upper digit LED as ‘3’

    else if(upper _digit == 40)

set the upper digit LED as ‘4’

    else if(upper _digit == 50)

        set the upper digit LED as ‘5’

    else if(upper _digit == 60)

        set the upper digit LED as ‘6’   

else if(upper _digit == 70)

        set the upper digit LED as ‘7’   

else if(upper _digit == 80)

set the upper digit LED as ‘8’   

else

        set the upper digit LED as ‘9’

   

   

    if(lower_digit == 0)

        set the lower digit LED as ‘0’

    else if(lower_digit == 1)

set the lower digit LED as ‘1’

    else if(lower_digit == 2)

set the lower digit LED as ‘2’

    else if(lower_digit == 3)

set the lower digit LED as ‘3’

    else if(lower_digit == 4)

set the lower digit LED as ‘4’

    else if(lower_digit == 5)

        set the lower digit LED as ‘5’

    else if(lower_digit == 6)

        set the lower digit LED as ‘6’

    else if(lower_digit == 7)

        set the lower digit LED as ‘7’

    else if(lower_digit == 8)

        set the lower digit LED as ‘8’

    else

        set the lower digit LED as ‘0’

End

      

      

// A function which displays the game level that the player currently chooses on the 7 segment LED.      

LED_LevelDisplay(Takes the selected level, Return nothing)

Start

   

   if(level == 1)

          set the level display LED as ‘1’

   else if(level == 2)

         set the level display LED as ‘2’  

else if(level == 3)

         set the level display LED as ‘3’  

else

 set the level display LED as ‘0’

End

 

// A function which displays the remain life time that the player currently has on the 7 segment LED

LED_LifeDisplay(Takes the current number of lives, Return nothing)

Start

   if(lifenumber == 0)

         set the level display LED as ‘0’

   else if(lifenumber == 1)

         set the level display LED as ‘1’  

else if(lifenumber == 2)

         set the level display LED as ‘2’

   else if(lifenumber == 3)

         set the level display LED as ‘3’

   else if(lifenumber == 4)

         set the level display LED as ‘4’

   else if(lifenumber == 5)

         set the level display LED as ‘5’

   else if(lifenumber == 6)

         set the level display LED as ‘6’

   else if(lifenumber == 7)

         set the level display LED as ‘7’

   else if(lifenumber == 8)

         set the level display LED as ‘8’

   else if(lifenumber == 9)

         set the level display LED as ‘9’

   else (if the number of lives is over 9)

         set the level display LED as ‘9’

End

 

 

// A function which sends data to LCD or actuators via the main P8255( A port extension chip )

DataSend(Takes portNumber and the data transferred)

Start

 

    if(portNumber == PortA) // means port A

      // send the data to PortA

    else if(portNumber == PortB) // means port B

      // send the data to PortB

    else   // means upper bits of PortC

      // send the data to PortA

    end if

End

 

 

 

// A function which receives data from the portC of the main P8255 chip.

DataReceive(Takes nothing, Returns the data received)

Start

        // receive the current input of PortC’s lower bits

        // return the value.

End

 

 

// Coin detection fucntion.

CoinDetecting( Takes nothing, Return nothing)

Start

   // When the coin appears, check it.

   // When the coin disappears, check it.

 

  //  When the above two conditions are both satisfied, it’s regarded as a ‘coin detecting’.

End

 

 

// Game START detection function.

static void StartDetecting( Takes nothing, Return nothing)

Start

   // When the button is pushed, check it.

   // When the button is released check it.

 

  //  When the above two conditions are both satisfied, it’s regarded as a ‘button pressed’.

End

 

 

 

// Wire detection funciton.

static void WireDetecting( Takes nothing, Return nothing)

Start

   // When the stick touches the wire, check it.

  

// If wire touched, set the failure flag as ‘1’

End

 

 

 

// SWAG despense function.

SWAG_dispense(Takes a integer which means duration)

Start

    // Open the door of the SWAG container

    waits for ‘dutation’ milliseconds

    // Close the door of the SWAG container

End

 

 

// A function directly controls two spining motors and the vibrating motor of the game stick.

Motor_Vibrator_Control(Takes motor_mode and motor_speed, vibrator_on)

Start

    if(motor_mode==0) //opposite direction

    {

        if(vibrator_on == 0) // vibrator off

            // Let the motors spins in opposite direction and the vibration off.

        else

            // Let the motors spins in opposite direction and the vibration on.

       

    }else   // the same direction

    {

        if(vibrator_on == 0) // vibrator off

            // Let the motors spins in the same direction and the vibration off.

        else

            // Let the motors spins in the same direction and the vibration on.       

     }

End

   

 

 

main(Takes nothing, Return nothing)

Start

  // Let the program start with ‘RESET_STATE’   

 

  Infinite Repeat Loop

  Start

      switch(Current_State)

      {

        case RESET_STATE:

            // Initialization           

            // Initially set all 7-segments LEDs as zero.

            // LCD instructions for a user

            // Waiting for coin detection.

            // When coin detected, set the new current state as COIN_STATE.

          

        case COIN_STATE:

            // Instructions using LCD

            // Receive level selection by potentiometer input.

// When low level

                {

                    // Display ‘low level’.

// Set play time is 99 seconds.

                    // Set life number is 15.

                    // Set the new Current_State as  LOW_PLAY_STATE.

                }

// When Med level

                {

                    // Display ‘med level’.

// Set play time is 85 seconds.

                    // Set life number is 9.

                    // Set the new Current_State as  MED_PLAY_STATE.

}

// When high level

                {

                    // Display ‘high level’.

// Set play time is 75 seconds.

                    // Set life number is 7.

                    // Set the new Current_State as  HIGH_PLAY_STATE.

                }

 

// Waiting for Start button is pressed.

// Display the life number

          

            

       

case LOW_PLAY_STATE:

            // Make two DC motors run in opposite direction and the vibration motor off.

            // decrease the playing time and check the sensor inputs.

            // if the number of touching exceeds the life number or playing time is over,

      set the failure flag. ( Every touch causes short buzzer sound )

            // if the player succeeds, set the new Current_State as SUCCESS_STATE          

            // else if the player succeeds, set the new Current_State as FAILURE_STATE  

       

       case MED_PLAY_STATE:

            // Make two DC motors run in the same direction more fast and the vibration motor off.

            // decrease the playing time and check the sensor inputs.

            // if the number of touching exceeds the life number or playing time is over,

      set the failure flag. ( Every touch causes short buzzer sound )

            // if the player succeeds, set the new Current_State as SUCCESS_STATE          

            // else if the player succeeds, set the new Current_State as FAILURE_STATE

           

       case HIGH_PLAY_STATE:

            // Make two DC motors run in the same direction more fast and the vibration motor on.

            // decrease the playing time and check the sensor inputs.

            // if the number of touching exceeds the life number or playing time is over,

      set the failure flag.( Every touch causes short buzzer sound )

            // if the player succeeds, set the new Current_State as SUCCESS_STATE          

            // else if the player succeeds, set the new Current_State as FAILURE_STATE

       

case FAILURE_STATE:

            // turn off all actuators except SWAG dispenser

           // A buzzer sound that indicates the failure.

           // SWAG dispense for the looser.

           // Set the new Current_State as RESET_STATE            

 

case SUCCESS_STATE:

            // turn off all actuators except SWAG dispenser

           // A buzzer sound that congratulate the success.

           // SWAG dispense for the winner.

           // Set the new Current_State as RESET_STATE

   Repeat Loop End

End