Button

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
chanyh
Posts: 17
Joined: 11 Oct 2010, 05:14

Button

Post by chanyh »

What is the meaning of the statement " (0 == ButtonCount(BTNCENTER,true)) "?

What is its purpose in the following program?

Code: Select all

...................................................................
while (0 == ButtonCount(BTNCENTER,true))
                {
                   if (ButtonCount(BTNLEFT,true) && N > 0)
                      {
                         N--;
                      }

                   if (ButtonCount(BTNRIGHT,true) && N < 10)
                      {
                         N++;
                      }
......................................................................
Can someone help?


YH Chan
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Button

Post by mattallen37 »

(0 == ButtonCount(BTNCENTER,true)) is basically a conditional statement. The value is true if the center button is NOT pressed, and false if it IS pressed.

For what code there is, I would say that it does the following.

Code: Select all

...................................................................
while (0 == ButtonCount(BTNCENTER,true))                      //as long as the center button is NOT pressed, repeat the following code.
                {
                   if (ButtonCount(BTNLEFT,true) && N > 0)     //if variable N is greater than 0, AND the left button is pressed, variable N is reduced by one.
                      {
                         N--;
                      }

                   if (ButtonCount(BTNRIGHT,true) && N < 10) //if variable N is less than 10, AND the right button is pressed, variable N is increased by one.
                      {
                         N++;
                      }
......................................................................
The apparent purpose, is to be able to increase or decrease a variable (N) using the buttons, but limiting it to the 0-10 range.

I see one major flaw. There is nothing to wait until the button is released, so it would zoom through it so fast (and so many times) that the only likely states for variable N would be 0 or 10, not likely to be in between.

This is not a complete program, so it is hard to say anything is for sure.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
chanyh
Posts: 17
Joined: 11 Oct 2010, 05:14

Re: Button

Post by chanyh »

Hi, Mattallen,

What is the difference between "(0 == ButtonCount(BTNCENTER,true)) " and "(ButtonCount(BTNCENTER,true)==0)"?

In the following program, If the statement "while (0 == ButtonCount(BTNCENTER,true))" is replaced by "while (ButtonCount(BTNCENTER,true)==0) ", the two programs, both, have the same result.

Code: Select all

task main()
   {
      int Alphabet[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      int Number;
      int N = 0;
      while (true)
         {
             while (ButtonCount(BTNCENTER,true)==0)
             //while (0 == ButtonCount(BTNCENTER,true))
                {
                   if (ButtonCount(BTNLEFT,true) && N > 0)
                      {
                         N--;
                      }

                   if (ButtonCount(BTNRIGHT,true) && N < 10)
                      {
                         N++;
                      }
                   until (N <10)
                      {
                         ClearScreen();
                         N=0;
                      }

                   Number = Alphabet[N];
                   NumOut(40, LCD_LINE5, Number, true);
                   Wait(20);
                }
         }
    }
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Button

Post by mattallen37 »

It is a conditional statement. It compares the "0" value with the "ButtonCount(BTNCENTER,true)" value. The == means that both parts must be of the same value for the statement to be true. The statement in this case is either "(0 == ButtonCount(BTNCENTER,true))" or "(ButtonCount(BTNCENTER,true)==0)". Both compare the two parts with each other. It is like asking, "Is red your favorite color?" vs. "Is your favorite color red?". Both questions will have the same answer, regardless of the order of the two parts being compared.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
gloomyandy
Posts: 323
Joined: 29 Sep 2010, 05:03

Re: Button

Post by gloomyandy »

Hi,
Thought you might like to know why this rather odd form of a comparison is often used...

In languages like C and Java that allow assignment operations to be embedded within a statement (so you can have code like this in C...

Code: Select all

char *p1 = oldString;
char *p2 = newString;
while (*p2++ = *p1++) {}
which will basically copy the contents of one string to another until the end of the (null terminated) string oldString is reached, a very common mistake is to use = instead of == so for instance...

Code: Select all

int val = 2;

if (val = 1)
{
  // This test will always be true
}
// at this point val will have the value 1

The code should probably have been...

Code: Select all

int val = 2;
if (val == 1)
{
}
// val will still have the value 2
One way to avoid this potential problem is to put the constant first...

Code: Select all

int val = 2;

// The following will generate a compiler error...
if (1 = val)
{
}

// But this will not
if (1 == val)
{
}
Which will mean that if you do make a mistake then the compiler will be able to spot it... Some programming style books recommend this rather (strange at first sight) way of doing things to make your code more robust and to help you avoid errors. So you may see a lot of code around that uses this form...

Andy
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Button

Post by afanofosc »

ButtonCount is not the same as ButtonPressed. Passing true into the ButtonCount function will reset the count back to zero. The count returned by this function is the same value returned by ButtonReleaseCount which is the sum of ButtonLongReleaseCount and ButtonShortReleaseCount. These 3 functions, along with ButtonPressCount, ButtonLongPressCount, and ButtonState all use the IOMapRead system call (or IOMapReadByID if targetting the enhanced firmware) to directly read values from the Button module IOMap. The Set* versions of these low level functions use the IOMapWrite system call (or IOMapWriteByID) to write to the Button module IOMap. Both ButtonPressed and ButtonCount use the ReadButton system call as does the ReadButtonEx API function.

If you use ButtonPressed then you have to check for a release if you want to truly process a "click", i.e., a press and release action. ButtonCount will return a value > 0 if since the last time it was called with "true" passed in as the second parameter at least one press and release has occurred. If the specified button has not been pressed and released since the last reset of the counter then it will return 0. If you don't care about counting clicks for a particular button then using ButtonCount and always resetting the counter is a good way to check for a button "click".

And as gloomyandy mentioned, it is often written with the constant on the left to help protect against the == vs = bug that is easy to run into. The two forms of the comparison are functionally equivalent.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests