Go to Post This thread should surface like this before every new season. It is motivating and inspiring. And jawdropping. - JaneYoung [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 08-04-2004, 03:30
PBoss PBoss is offline
Registered User
#1212
 
Join Date: Jan 2003
Location: Nowhere
Posts: 9
PBoss is an unknown quantity at this point
PBasic Probelm

Hi,
I have 3 push buttons that I am using as a switches. I am using last year's robot controller. What I want to do is when I press a button once, I want it to be like a toggle button. When I press a button, I want the controller to run a sub routine. That is simple, I know, but here is the catch:
If while running the sub-routine, a second button is pressed, I want the RC to ignore the second button. When the RC is done executing the subroutine, then I want it to be able to recieve a signal from any other button, but not until it is done executing the first sub routine.
Also, I forgot, in PBasic, is this how you used to do sub routines?

IF condition THEN
gosub FIRST
ENDIF

FIRST:
'code


Is that all there is to it or do I have to declare the sub routine in a different way or end the sub routine in a different way. I'll appreciate any help.
  #2   Spotlight this post!  
Unread 08-04-2004, 06:39
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: PBasic Probelm

Quote:
Originally Posted by PBoss
Also, I forgot, in PBasic, is this how you used to do sub routines?

IF condition THEN
gosub FIRST
ENDIF

FIRST:
'code


Is that all there is to it or do I have to declare the sub routine in a different way or end the sub routine in a different way. I'll appreciate any help.
I think that you can do that. I never worked with last years controllers though, so don't believe me.
__________________


Last edited by Ryan M. : 08-04-2004 at 09:53.
  #3   Spotlight this post!  
Unread 08-04-2004, 09:44
Grommit Grommit is offline
Registered User
#0115 (Monta Vista Robotics)
 
Join Date: Oct 2002
Location: Cupertino
Posts: 47
Grommit will become famous soon enoughGrommit will become famous soon enough
Send a message via AIM to Grommit
Re: PBasic Probelm

You need two subroutines:
One for when the button state is up doing:
Set flag to 0
And the other when the button state is down doing:
Set flag to 1
(other stuff)

This way, the flag will be 1 if the button is pressed down.
Then at the top of the other button's code, put an if flag = 0 then,
so that code will not execute unless the button is up.
__________________
Shrenik Shah
Engineering Director
Team 115: Monta Vista Robotics

Congratulations to Mr. Shinta for winning Woodie Flowers at Silicon Valley!
  #4   Spotlight this post!  
Unread 08-04-2004, 09:58
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: PBasic Probelm

Do something like this: (this is in pseudo-code)
Code:
global variable FirstActivated initialized to 0

Check if first button is press
  If it is, toggle the state of FirstActivation (I.E. if it is 1 then make it 0; if it is 0 then make it 1)

Check if FirstActivated is 1
  If it is, do what ever you want to do here.

Check if Second button is pressed.
  If it is, check if FirstActivated is 0
    If is is, then do your second button action.
__________________

  #5   Spotlight this post!  
Unread 08-04-2004, 10:14
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,600
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: PBasic Probelm

Quote:
Originally Posted by PBoss
When I press a button, I want the controller to run a sub routine. That is simple, I know, but here is the catch:
If while running the sub-routine, a second button is pressed, I want the RC to ignore the second button. When the RC is done executing the subroutine, then I want it to be able to recieve a signal from any other button, but not until it is done executing the first sub routine.
One thing you have to remember is that the state of the buttons can only change when you do the serin statement at the top of your code. Thus, even if button2 is pressed while you are doing the subroutine for button1, you won't see that button2 is pressed until the next loop. If button1 and button2 were pressed at approximately the same time, you'll see both after the serin. In other words, you never have to worry about subroutine2 executing in the middle of subroutine1, your code will flow in order.
  #6   Spotlight this post!  
Unread 08-04-2004, 12:44
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,906
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: PBasic Probelm

Quote:
Originally Posted by PBoss
Also, I forgot, in PBasic, is this how you used to do sub routines?

IF condition THEN
gosub FIRST
ENDIF

FIRST:
'code
You also need a branch after the IF statement to hop over the "FIRST: code", otherwise the code just falls into the "FIRST: code."
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
  #7   Spotlight this post!  
Unread 08-04-2004, 13:21
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Question Re: PBasic Probelm

Quote:
Originally Posted by PBoss
Also, I forgot, in PBasic, is this how you used to do sub routines?

IF condition THEN
gosub FIRST
ENDIF

FIRST:
'code
I don't know PBASIC specifically, but I'll try.

FIRST is a label. Wouldn't you use goto, not gosub?
I know that in Visual Basic (similar so far), you define a sub as:
Code:
Public Sub foo ()
 'Some Code Here
 'Call subroutine
 foo2
End Sub
Is it any diferent in PBASIC?
  #8   Spotlight this post!  
Unread 08-04-2004, 13:55
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,906
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: PBasic Probelm

Quote:
Originally Posted by Astronouth7303
FIRST is a label. Wouldn't you use goto, not gosub?
In PBasic the "basic" program structure would look like this:
Code:
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]
. . .
  gosub RESCALE_STICK
. . .
  Serout USERCPU, OUTBAUD, [255,255,127,relayA,127,127,pwm2,pwm1,127,127,127,127,pwm9,127,127,127,127,127,127,127]
 
Goto MainLoop:
 
'-------- SUBROUTINES ----------------------------------------
 
RESCALE_STICK:	 
'Rescale the output of the joysticks
. . .
RETURN
Stop
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 08-04-2004 at 13:58.
  #9   Spotlight this post!  
Unread 08-04-2004, 14:05
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: PBasic Probelm

1 routine. 'gosub' instead of 'goto'. 'return' to go to the most recent gosub. 'stop' to halt everything. Got it.
  #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
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Obscure PBASIC for RoboEmu rbayer Programming 1 28-03-2003 23:57
PBasic 2.5 vs. 2.0 Anthony Kesich Programming 6 09-02-2003 22:06
PBasic Question Melissa H. Programming 28 17-11-2002 18:53
Output voltage in PBASIC diego25 Programming 7 31-01-2002 22:50
Another PBASIC Question Ian W. Programming 4 31-01-2002 21:36


All times are GMT -5. The time now is 03:08.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi