ROBOTC.net forums
http://robotc.net/forums/

Problems with NXT buttons
http://robotc.net/forums/viewtopic.php?f=1&t=5979
Page 1 of 1

Author:  Kowalski [ Mon Jun 17, 2013 12:34 pm ]
Post subject:  Problems with NXT buttons

Hi, people! :biggrin:
I'm new in your community. I'm from Italy, so I'm sorry for my english mistakes. I hope to give you my contribute.

I am recently fond of robotics and I decided to buy a Lego NXT kit. I have a problem with the buttons of the NXT brick trying to run a function to wait for a selected button pressed.

It works only if I want to detect different buttons. If want to detect the same button pressed as it is in the code below, it doesn't work correctly.

After the first
Code:
until_button_pressed("enter");
the program pass directly to the first
Code:
until_button_pressed("left");
pressing once the "enter". It should request from the user to press "enter" twice (the same for "left").

I hope I'm enough clear to explain it.

Code:
void until_button_pressed(char *button)
   {
   writeDebugStreamLine("until_button_pressed(%s)",button);

   TButtons nBtn;
   nNxtButtonTask  = -2;

        do {nBtn = nNxtButtonPressed;}
   while (nBtn == -1);
   
   if (button=="exit"||button=="right"||button=="left"||button=="enter")
      {
            
      if (button=="exit")   
         {   nNxtExitClicks = 2;
         
         do {nBtn = nNxtButtonPressed;}
         while(nBtn!=kExitButton);
         
         nNxtExitClicks = 1; }
         
      if (button=="right")
         {
         do {nBtn = nNxtButtonPressed;}
         while(nBtn!=kRightButton);      
         }
      
      if (button=="left")   
         {
         do {nBtn = nNxtButtonPressed;}
         while(nBtn!=kLeftButton);   
         }
      
      if (button=="enter")
         {
         do {nBtn = nNxtButtonPressed;}
         while(nBtn!=kEnterButton);   
         }
      
      }
        writeDebugStreamLine(" - button pressed = %s",button);
   writeDebugStreamLine(" - button pressed = %f",nBtn);
   return;   
   }

task main()
{
clearDebugStream();
until_button_pressed("enter");
until_button_pressed("enter");
until_button_pressed("left");
until_button_pressed("left");
}

Author:  ribak [ Mon Jun 24, 2013 6:00 am ]
Post subject:  Re: Problems with NXT buttons

It could be that the processor goes to fast that while you are releasing the button, it is already executing the second until_button_pressed("enter");

Try this,

task main()
{
clearDebugStream();
until_button_pressed("enter");
until_button_pressed("left");
until_button_pressed("enter");
until_button_pressed("left");
}

and if it works, you should probably add some waits between the calls to give you time to release the button, or do some loop that ends when the button is not pressed.

Hope this helps.

Author:  Kowalski [ Tue Mar 11, 2014 3:51 pm ]
Post subject:  Re: Problems with NXT buttons

Thank you so much. I corrected my code and now it works correctly. I apologize to be so late in answering you.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/