Thread: PBasic Probelm
View Single Post
  #10   Spotlight this post!  
Unread 08-04-2004, 18:07
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
Re: PBasic Problem

I would do something like this:
Code:
None         con 0
DoingAction1 con 1
DoingAction2 con 2
DoingAction3 con 3

ProgramState var byte

'Initialization
ProgramState = None

MainLoop:
'---------- Serin Command - Get Data from Master uP -------------
  Serin COMA\COMB, INBAUD, [oi_swA,oi_swB,rc_swA,rc_swB,p1_x,PB_mode,p2_y,p1_y  ]

. . .

  if ProgramState = None then
    if switch1 <> 0 then ProgramState = DoingAction1
    if switch2 <> 0 then ProgramState = DoingAction2
    if switch3 <> 0 then ProgramState = DoingAction3
  endif

  if ProgramState = DoingAction1 then gosub DoAction1
  if ProgramState = DoingAction2 then gosub DoAction2
  if ProgramState = DoingAction3 then gosub DoAction3

. . .

  Serout USERCPU, OUTBAUD, [255,255,127,relayA,127,127,pwm2,pwm1,127,127,127,1  27,pwm9,127,127,127,127,127,127,127]
 
Goto MainLoop:
 
'-------- SUBROUTINES ----------------------------------------

DoAction1:
  [Do some stuff...]
  if Done then ProgramState = None
  return

DoAction2:
  [Do some other stuff...]
  if Done then ProgramState = None
  return

DoAction3:
  [Do some fluff...]
  if Done then ProgramState = None
  return

Stop
(Standard disclamer)


[edit]
Note 1: As Joe mentioned, more than one button could be pressed at the same time. The way I've coded it, button 3 would have the highest priority, and button 1 the lowest (because of the order of the if statements.)

Note 2: I didn't indicate how the variable "Done" gets set.... Or even that it's a variable, for that matter. It could be a logical expression like "elapsedProgramCycles > 80" or "proximitySensor = 1".

Note 3: Make sure your subroutines return quickly. They should only calculate or set outputs. It won't work to do something like this:
Code:
  for i = 1 to 80 
    pwm1 = 255
  next i
  return
Instead, it should be something like:
Code:
  ' Make sure JJ gets initialized somewhere. (Like when you set the ProgramState variable.)
  pwm1 = 255
  JJ = JJ+1
  if JJ = 80 then ProgramState = None
  return
[/edit]
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
“Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me

Last edited by Greg Ross : 08-04-2004 at 21:46. Reason: Added a few notes