View unanswered posts | View active topics It is currently Sun Dec 21, 2014 8:06 am






Reply to topic  [ 12 posts ] 
Making a toggle??? 
Author Message
Rookie

Joined: Wed Feb 15, 2012 4:44 am
Posts: 2
Post 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. :biggrin:


Wed Feb 15, 2012 4:54 am
Profile
Rookie

Joined: Wed Feb 15, 2012 4:44 am
Posts: 2
Post 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 '&&' :biggrin:


It works tnx a lot :bigthumb:
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 :} :breakcomputer:


Wed Feb 15, 2012 9:17 am
Profile
Rookie

Joined: Sun Feb 27, 2011 3:41 pm
Posts: 20
Post 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
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1371
Post 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
Profile
Rookie

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


Mon Mar 12, 2012 10:59 pm
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1371
Post 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
Profile
Rookie

Joined: Sun Feb 27, 2011 3:41 pm
Posts: 20
Post 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
Profile
Rookie

Joined: Mon Feb 21, 2011 3:40 pm
Posts: 15
Post 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;}


What would be the advantages/disadvantages of either?


Thu May 17, 2012 9:02 pm
Profile
Rookie

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


Sun Aug 12, 2012 3:31 pm
Profile
Rookie

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


Sun Aug 19, 2012 10:56 am
Profile
Rookie

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


Tue Aug 21, 2012 8:34 pm
Profile
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3297
Location: Rotterdam, The Netherlands
Post 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
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 12 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.