Page 1 of 1

Using the NXT buttons with NXC

Posted: 04 Oct 2010, 21:56
by mattallen37
How do you use the NXT buttons with NXC? I know all this was discussed on nxtasy, but I forget how to do it, so I am asking here. Ideally, I would like an example program that includes the functions, including the whole "back" button thing (locking/delaying for exiting programs that way). Thanks.

Edit: I just realized the starter can edit the topic name! (unlike nxtasy).
Edit2: you can also delete posts without needing to request it be done.

Re: Using the NXT buttons with NXC

Posted: 04 Oct 2010, 22:40
by afanofosc
What may help you more is reading the NXC Guide and Help section on the button module and experimenting with the available functions to see what they do. Using the back button requires the enhanced NBC/NXC firmware, of course. With it on your brick you can call SetLongAbort(true). You can also totally disable the abort operation by setting the abort flag to be something that can never happen (look for SetAbortFlag in the help). If you do that then pulling a battery or using a direct command remotely will be the only way to stop your program. Or your program could have an exit condition within its user interface that lets you stop the program.

http://bricxcc.sourceforge.net/nbc/nxcd ... tions.html

From the NXC help:

Code: Select all


task main()
{
#ifdef __ENHANCED_FIRMWARE
  SetLongAbort(true);
#endif
  while(true)
  {
#ifdef __ENHANCED_FIRMWARE
    NumOut(0, LCD_LINE1, ButtonPressed(BTNEXIT, false));
#endif
    NumOut(0, LCD_LINE2, ButtonPressed(BTNRIGHT, false));
    NumOut(0, LCD_LINE3, ButtonPressed(BTNLEFT, false));
    NumOut(0, LCD_LINE4, ButtonPressed(BTNCENTER, false));
  }
}
Normally you want to check for a button click or "bump" which involves waiting for a press and then waiting for a not press (aka release). You may want to write a function that encapsulates this in a way you prefer which you can then reuse in all your programs that require button press handling.

John Hansen

Re: Using the NXT buttons with NXC

Posted: 04 Oct 2010, 23:01
by mattallen37
Exactly what I was looking for, thanks.

Re: Using the NXT buttons with NXC

Posted: 05 Oct 2010, 00:55
by dhystad
Somehow I think he missed the suggestion about reading the guide.

Re: Using the NXT buttons with NXC

Posted: 05 Oct 2010, 01:02
by mattallen37
dhystad wrote:Somehow I think he missed the suggestion about reading the guide.
Me? I did go and read it for a while, and I found it helpful, however, example programs ALWAYS are (for me) a better way to learn what can be done with a function (and more importantly, HOW to use it) than just reading what the guide has to say about it. Just in the program he provided, John answered all the questions I had at the time (in regard to the situation).

Re: Using the NXT buttons with NXC

Posted: 05 Oct 2010, 03:37
by m-goldberg
mattallen37 wrote:
dhystad wrote:Somehow I think he missed the suggestion about reading the guide.
Me? I did go and read it for a while, and I found it helpful, however, example programs ALWAYS are (for me) a better way to learn what can be done with a function (and more importantly, HOW to use it) than just reading what the guide has to say about it. Just in the program he provided, John answered all the questions I had at the time (in regard to the situation).
I strongly urge you to purchase a copy of John Hansen's book LEGO MINDSTORMS NXT, POWER PROGRAMMING IN C, (2nd edition). It is full of the kind of examples you are looking for and can be bought for less than US$20 from Amazon.

Re: Using the NXT buttons with NXC

Posted: 05 Oct 2010, 03:43
by mattallen37
Is there a digital version (PDF) of the book?

Re: Using the NXT buttons with NXC

Posted: 05 Oct 2010, 03:48
by m-goldberg
mattallen37 wrote:Is there a digital version (PDF) of the book?
Not that I am aware of. It would be useful if there were. I'd buy a PDF copy if I could.

Re: Using the NXT buttons with NXC

Posted: 05 Oct 2010, 03:50
by mattallen37
I already have two paperback Mindstorms books, and any time I want to program based on an example, I have to type it all in, which can take a LONG time. It would be really nice to have them all in PDF.

Re: Using the NXT buttons with NXC

Posted: 06 Oct 2010, 19:27
by mattallen37
Did you know that you CAN make a virus for the NXT? I just made one, and it took me 10+ minutes to restore it, even with the reset button and all that.