View unanswered posts | View active topics It is currently Tue May 31, 2016 10:52 am

 Page 1 of 1 [ 12 posts ]
 Print view Previous topic | Next topic
Making a toggle???
Author Message
Rookie

Joined: Wed Feb 15, 2012 4:44 am
Posts: 2
Making a toggle???
Hey,

I always used this to make a motor spin with a button.

 Code:if(joy1Btn(01))                      {    motor[motorC] = 50;               }    else    {     motor[motorC] = 0;    }

But now I have to make it so I press the button once than it goes spinning at 50% and if I press the button again it stops.
i hope anybody can help.

Wed Feb 15, 2012 4:54 am
Rookie

Joined: Wed Feb 15, 2012 4:44 am
Posts: 2
Re: Making a toggle???
miki wrote:
Hi,
I don't know FTC rules but the code below should work
 Code:int debounce=0;int speed = 0;int btn=0;while(1){  Wait1Msec(100); // check button every 100ms  btn = joy1Btn(01);  if(btn && (btn != debounce))    speed = speed?0:50;  // speed is modified only if btn is pressed and was unpressed.  motor[motoC] = speed;  debounce = btn;}

regards,
Miki.

edit: replace 'and' by '&&'

It works tnx a lot
but now we have a problem because of this code the rest of the program stops working
and i cant use a while(1) command cuz that is activating the robot on command so we cant use it without a different programm telling it to run.
so i tried to remove the while true command and than the command doesnt work

i am a noob programmer :}

Wed Feb 15, 2012 9:17 am
Rookie

Joined: Sun Feb 27, 2011 3:41 pm
Posts: 20
Re: Making a toggle???
It won't work because you need to adapt it into a seperate task....

Fri Mar 09, 2012 12:20 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Re: Making a toggle???
No, you don't need a separate task. You just need to put your other stuff inside the same while loop.
 Code:int debounce=0;int speed = 0;int btn=0;while(1){  Wait1Msec(100); // check button every 100ms  btn = joy1Btn(01);  if(btn && (btn != debounce))    speed = speed?0:50;  // speed is modified only if btn is pressed and was unpressed.  motor[motoC] = speed;  debounce = btn;  //  // Do other stuff here.  //}

Fri Mar 09, 2012 1:52 pm
Rookie

Joined: Sun Feb 27, 2011 3:41 pm
Posts: 20
Re: Making a toggle???
right but when we do that, everything will get delayed by 100ms...Probably not a lot though

Mon Mar 12, 2012 10:59 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
Re: Making a toggle???
It is recommended to do some delay between each loop but it doesn't have to be 100 msec. The delay will help with the button debounce. If your other stuff needs to be processed more often then the minimum debounce time, you could process the button once every n loops. Then you can do a 10 msec delay or less in each loop.

Tue Mar 13, 2012 12:41 am
Rookie

Joined: Sun Feb 27, 2011 3:41 pm
Posts: 20
Re: Making a toggle???
we got around this problem but using a timer to check whether it has been 200ms since the last check. Then if so, you reset the timer and do the cycle again.

Thu Apr 19, 2012 12:08 am
Rookie

Joined: Mon Feb 21, 2011 3:40 pm
Posts: 15
Re: Making a toggle???
Sorry if this is old; but wouldn't it work if you use:

 Code:bool shift = false;if(joy1Btn(1) {   if(!shift)   {       //action   }   shift = true;}else {shift = false;}

Thu May 17, 2012 9:02 pm
Rookie

Joined: Sun Feb 27, 2011 3:41 pm
Posts: 20
Re: Making a toggle???
Ah! that's actually a nice way of doing it...

Sun Aug 12, 2012 3:31 pm
Rookie

Joined: Mon Feb 21, 2011 3:40 pm
Posts: 15
Re: Making a toggle???
I sometimes find that simple is better.

Sun Aug 19, 2012 10:56 am
Rookie

Joined: Tue Aug 21, 2012 2:36 pm
Posts: 1
Re: Making a toggle???
What's a toggle?

Tue Aug 21, 2012 8:34 pm

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Re: Making a toggle???
A toggle is like a button that when you press it, it alternatingly switches something on or off.

- Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Wed Aug 22, 2012 2:46 am
Display posts from previous:  Sort by
 Page 1 of 1 [ 12 posts ]

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning