View unanswered posts | View active topics It is currently Sat Oct 25, 2014 3:45 pm






Reply to topic  [ 3 posts ] 
Problems with NXT buttons 
Author Message
Rookie
User avatar

Joined: Mon Jun 17, 2013 12:12 pm
Posts: 4
Location: Italy
Post 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");
}


Mon Jun 17, 2013 12:34 pm
Profile
Rookie

Joined: Fri Jun 21, 2013 4:24 am
Posts: 2
Post 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.


Mon Jun 24, 2013 6:00 am
Profile
Rookie
User avatar

Joined: Mon Jun 17, 2013 12:12 pm
Posts: 4
Location: Italy
Post 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.


Tue Mar 11, 2014 3:51 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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.