You are on page 1of 2

Functions in module: InitDrivingService, PostDrivingService, RunDrivingService,

CheckLEDAlignment, Check4Gun
Data private to module: static uint32_t lastADC[1]

InitDrivingService

Set MyPriority to Priority


Set CurrentState to InitDriving
Post Event ES_Init to this service
End of InitializeDriving

RunDrivingService
Define Event ReturnEvent
Set EventType to ES_NO_EVENT
Define NextState
Set NextState to CurrentState

If CurrentState is InitDriving
If ThisEvent is ES_INIT
Configure analog input for potentiometer and IR Transistor
Set up ADC for analog inputs
Configure digital outputs for PointLED pins
Set NextState to Waiting2Begin
If CurrentState is Waiting2Begin
If ThisEvent is ES_BEGIN
Pick PointLED at random
Turn on PointLED
Set Target Timer
Set NextState to Steering
If CurrentState is Steering
Event is ES_ALIGNED
Turn on TargetLED
Set NextState to Aligned
If ThisEvent is ES_TIMEOUT
Turn off PointLED
Pick new PointLED at random
Turn on new PointLED
Reset Target Timer
If ThisEvent is ES_ACTIVITY_TIMEOUT
Turn off PointLED
Disable Target Timer
Set NextState to Waiting2Begin
If ThisEvent is ES_RECEIVE_SERVO_POS
Set CurrentPosition to EventParam
If CurrentState is Aligned
If ThisEvent is ES_TARGET_SHOT
Turn off PointLED
Pick new PointLED at random
Turn on new PointLED
Turn off TargetLED
Reset Target Timer
Reset Activity Timer
Create ES_ADD_SCORE event
Set EventParam to 5 (points to be added to score)
Post event to LEDService
Set NextState to Steering
If ThisEvent is ES_TIMEOUT
Turn off PointLED
Pick new PointLED at random
Turn on new PointLED
Turn off TargetLED
Reset Target Timer
Set NextState to Steering
If ThisEvent is ES_ACTIVITY_TIMEOUT
Turn off TargetLED
Turn off PointLED
Disable Target Timer
Set NextState to Waiting2Begin
If ThisEvent is ES_NOT_ALIGNED
Turn off TargetLED
Set CurrentState to Steering
If ThisEvent is ES_RECEIVE_SERVO_POS
Set CurrentPosition to EventParam
Set CurrentState to NextState
Return ReturnEvent

CheckLEDAlignment

Set bool ReturnVal to false


If the Servo position is within range for the PointLED (aligned)
If CurrentState is Steering
Post ES_ALIGNED event to this service
Post ES_ALIGNED event to AudioService
Set ReturnVal to true
Else (not aligned)
If CurrentState is Aligned
Post ES_NOT_ALIGNED event to this service
Return ReturnVal

Check4Gun

Read ADC value corresponding to IR phototransistor


If ADC value is greater than threshold value
Set ADC value to EventParam
Post ES_TARGET_SHOT to all services
Else
Ignore

You might also like