Go to Post After all, bitterness takes all the fun out of wiring. - Venkatesh [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 13-02-2010, 13:43
Team1710's Avatar
Team1710 Team1710 is offline
Registered User
FRC #1710
 
Join Date: Jan 2009
Rookie Year: 2007
Location: Kansas City
Posts: 95
Team1710 is an unknown quantity at this point
Toggle through button?

We are having some trouble with storing variables. We would like to create some code that when you press a button and then release the button, we can toggle a t/f or change a variable. For example, switch between the numbers 1, 2, and 3 by hitting a button. The problem we've run into is getting the number to stay once the button has been released. As soon as the button goes back to false, the toggle goes back to default...
__________________
  #2   Spotlight this post!  
Unread 13-02-2010, 13:50
EthanMiller EthanMiller is offline
Lead Programmer
AKA: Socks
FTC #4356 (The Zip Ties)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Clayton, NY
Posts: 121
EthanMiller has a spectacular aura aboutEthanMiller has a spectacular aura aboutEthanMiller has a spectacular aura about
Re: Toggle through button?

Depending on what language you're using, and assuming that we're using Java and joystick button 2, the code would look something like:

Code:
if joystick.getButton(2) == true && number < 2 {
     number++
} else if joystick.getButton(2) == true && number == 2 {
     number = 0
}
Note - this code is untested. Be sure to declare "number" and set it to 0 earlier in the program.

Also, this code will use values 0, 1, and 2, rotating each time the button is pressed.
__________________
When all else fails, read the manual.

FRC 1713 K Island Gears 2009, 2010 (Not 2011 due to budget, hopefully 2012!) - Fingerlakes Regional

FTC 4356 The Zip Ties 2010-2011 Season - NNYRC (2010 9th seed).
  #3   Spotlight this post!  
Unread 13-02-2010, 14:12
daltore's Avatar
daltore daltore is offline
Electronics/programming/design
AKA: Aaron Osmer
FRC #3529 (ausTIN CANs)
Team Role: Mentor
 
Join Date: Dec 2007
Rookie Year: 2007
Location: San Antonio, TX
Posts: 272
daltore has a spectacular aura aboutdaltore has a spectacular aura aboutdaltore has a spectacular aura about
Send a message via AIM to daltore Send a message via MSN to daltore Send a message via Yahoo to daltore
Re: Toggle through button?

I don't know how the Java code handles the button calls, but wouldn't that just cycle through the numbers without stopping? Unless getButton removes the read value when you read it the first time, this would set one number one loop, and see that the button is still pressed in the next loop and set the next number. You need a second variable to hold the last read button value. In the same Java code, it would look like this:

Code:
if joystick.getButton(2)==true && number<2 && lastState==false {
     number++
} else if joystick.getButton(2)==true && number==2 && lastState==false {
     number = 0
}
   lastState=joystick.getButton(2);
Since lastState is set after the if() structure, the new value will not be read until the next run through. So if it's FALSE now and you push the button, it will increment the variable, then set lastState to TRUE, which will be read the next time, and the structure will not run. Then, when you release the button, it sets lastState to FALSE again, and you can increment the variable again.

You can also use a shorter code system utilizing modulus. Modulus (%) reports the remainder in a division. So, 5 % 3 = 2. This will only report the values 0 (divisible by 3), 1 (1 more than a factor of 3) and 2 (2 more than a factor of 3). This will allow you to use a slightly longer equation, but shorter code which might actually run a bit faster (however insignificant with a 400Mhz processor):

Code:
if joystick.getButton(2) == true && lastState == false {
     number = (number+1) % 3;
}
   lastState = joystick.getButton(2);
Good luck, and let us know if you have any more problems.
  #4   Spotlight this post!  
Unread 13-02-2010, 14:14
EthanMiller EthanMiller is offline
Lead Programmer
AKA: Socks
FTC #4356 (The Zip Ties)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Clayton, NY
Posts: 121
EthanMiller has a spectacular aura aboutEthanMiller has a spectacular aura aboutEthanMiller has a spectacular aura about
Re: Toggle through button?

@Daltore: You're probably right, I didn't think of that.
__________________
When all else fails, read the manual.

FRC 1713 K Island Gears 2009, 2010 (Not 2011 due to budget, hopefully 2012!) - Fingerlakes Regional

FTC 4356 The Zip Ties 2010-2011 Season - NNYRC (2010 9th seed).
  #5   Spotlight this post!  
Unread 13-02-2010, 14:34
Team1710's Avatar
Team1710 Team1710 is offline
Registered User
FRC #1710
 
Join Date: Jan 2009
Rookie Year: 2007
Location: Kansas City
Posts: 95
Team1710 is an unknown quantity at this point
Re: Toggle through button?

We aren't in java, i forgot to mention we are in Labview...
__________________
  #6   Spotlight this post!  
Unread 13-02-2010, 23:13
daltore's Avatar
daltore daltore is offline
Electronics/programming/design
AKA: Aaron Osmer
FRC #3529 (ausTIN CANs)
Team Role: Mentor
 
Join Date: Dec 2007
Rookie Year: 2007
Location: San Antonio, TX
Posts: 272
daltore has a spectacular aura aboutdaltore has a spectacular aura aboutdaltore has a spectacular aura about
Send a message via AIM to daltore Send a message via MSN to daltore Send a message via Yahoo to daltore
Re: Toggle through button?

Okay, so the setup for LabView will be the same as in Java, you need to variables. In LabView, variables are just read/write calls to indicators and controls. Create two indicators or controls on the front panel (one boolean, one numeric). Then go to the block diagram, right-click, and go to the Structures pallet. There will be a gray box near the bottom that says "LOCAL". Click this and place it on the block diagram. It will have a question mark and a pin on the left. Click the question mark, and it will show a list of indicators and controls that currently exist in that VI. Select your boolean or numeric. To change the variable to read instead of write, right-click on the variable, and select "Change to Read". This will allow you to input and output data from the controls and indicators in your VI more than once.

However, since LabView is by nature a parallel language, we need a way of doing the increment and setting the last seen variable separately. To do this, you can use either a Flat Sequence Structure or a Stacked Sequence Structure (both in the structures pallet). A Flat Sequence structure looks like a film reel, and each frame is executed one after another, from left to right. To add a frame, right click on the right edge of the structure and select "Add Frame After". In the first frame, you need to have an AND operator (boolean pallet), a NOT operator on the lastSeen variable to negate its value (also boolean pallet), a Quotient and Remainder VI (numeric pallet, I think), and a case statement. The Quotient and Remainder VI works just like the "%" modulus symbol in the Java example, you plug in "number + 1" into X and "3" into Y, and the remainder appears on the top right corner (R, for "remainder). Wire that to your "number" variable.

In the next frame, you need to set "lastSeen" (or whatever you called your boolean variable) to the joystick button value.

That should be it, let me know if you want me to explain anything further.
  #7   Spotlight this post!  
Unread 14-02-2010, 00:35
exprg:melonhead's Avatar
exprg:melonhead exprg:melonhead is offline
The Teamless/ Travelling Mentor
AKA: Justin Sticht
FRC #3278 (Qwerty)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2004
Location: Fargo, ND
Posts: 34
exprg:melonhead has a spectacular aura aboutexprg:melonhead has a spectacular aura aboutexprg:melonhead has a spectacular aura about
Re: Toggle through button?

http://frcmastery.com/try-me/state-machines/
this may actually be able to explain it better than any of us can. this is the clean way of doing that
  #8   Spotlight this post!  
Unread 14-02-2010, 00:52
apalrd's Avatar
apalrd apalrd is offline
More Torque!
AKA: Andrew Palardy (Most people call me Palardy)
VRC #3333
Team Role: College Student
 
Join Date: Mar 2009
Rookie Year: 2009
Location: Auburn Hills, MI
Posts: 1,347
apalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond repute
Re: Toggle through button?

Quote:
Originally Posted by daltore View Post
In LabView, variables are just read/write calls to indicators and controls...
You could just use a Shift Register or Feedback Node, that seems like the simplest thing to do. Add a feedback node, set the input (bottom terminal) to what you want it to start as, then when you set it set the right terminal and that data will be available to the left terminal when you next run the loop.
__________________
Kettering University - Computer Engineering
Kettering Motorsports
Williams International - Commercial Engines - Controls and Accessories
FRC 33 - The Killer Bees - 2009-2012 Student, 2013-2014 Advisor
VEX IQ 3333 - The Bumble Bees - 2014+ Mentor

"Sometimes, the elegant implementation is a function. Not a method. Not a class. Not a framework. Just a function." ~ John Carmack
  #9   Spotlight this post!  
Unread 14-02-2010, 01:10
Tom Line's Avatar
Tom Line Tom Line is offline
Raptors can't turn doorknobs.
FRC #1718 (The Fighting Pi)
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 1999
Location: Armada, Michigan
Posts: 2,521
Tom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond reputeTom Line has a reputation beyond repute
Re: Toggle through button?

Yep. In labview, a true/false structure that increments or decrements the number, along with an in-range check to keep it within the numbers you want and a feedback node to return the last value. The only hitch is that you will need to code a trigger-style VI that latches until it sees the button released so it won't count up if it loops several times during a single button press.
  #10   Spotlight this post!  
Unread 15-02-2010, 18:10
Omar's Avatar
Omar Omar is offline
Registered User
no team
Team Role: Mentor
 
Join Date: Dec 2008
Rookie Year: 2008
Location: Huntsville, AL
Posts: 122
Omar is a glorious beacon of lightOmar is a glorious beacon of lightOmar is a glorious beacon of lightOmar is a glorious beacon of lightOmar is a glorious beacon of light
Re: Toggle through button?

Quote:
Originally Posted by Team1710 View Post
switch between the numbers 1, 2, and 3 by hitting a button.
The LabVIEW key word is
uninitialized shift register
Attached Thumbnails
Click image for larger version

Name:	Untitled.png
Views:	47
Size:	9.0 KB
ID:	8693  
__________________
Omar
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
Labview toggle switch GunfighterJ NI LabVIEW 8 13-02-2011 18:03
can you use a single button as a toggle? woogit General Forum 1 15-02-2008 14:21
toggle switch Windward Programming 31 27-01-2006 22:38
Code: Toggle on Tap Dave Scheck Programming 0 11-02-2005 18:07
Toggle? f22flyboy Programming 5 03-11-2002 08:03


All times are GMT -5. The time now is 02:01.

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