NXC: BT direct commands and Multi button presses

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
haydenstudios
Posts: 252
Joined: 22 Oct 2010, 20:05
Location: The United States of America
Contact:

NXC: BT direct commands and Multi button presses

Post by haydenstudios »

Hey guys, I'm still not super experienced with NXC so please bear with me. I am right now trying to make a remote in NXC with direct Bluetooth commands, using an NXT with two touch sensors. I would like for someone to modify my code so that the multi-button pressing in lines 23-44 works. I looked at John's website and in his book, but he used some API functions in the sample programs that were not covered in Daniele's tutorial, so I found them hard to understand, as well as in his book. Also, I would like for you to help me out with the Bluetooth direct control. It seems I'm not doing it right since I get some compiler errors, see lines 10, 16, and 95-155. I also had the same comprehension problems with this as I did the multi button pressing. Daniele's tutorial touched up on it slightly, but not enough for me to do it properly. So, without further ado, heres my code:

Code: Select all

int x;

task grab()
{
    while(true)
    {
        until(SENSOR_2 == 1)
        until(SENSOR_2 == 0)
        RemoteOnRev(1, OUT_A, 100);
        until(SENSOR_2 == 1)
        until(SENSOR_2 == 0)
        RemoteOnFwd(1, OUT_A, 100);
        Wait(1000);
        RemoteOff(1, OUT_A);
    }
}

task determine()
{
    while(true)
      {
         if(ButtonPressed(BTNCENTER, true))
         {
             if(ButtonPressed(BTNLEFT, true))
             {
                 if(ButtonPressed(BTNRIGHT, true))
                 {
                 }
                 else
                 {
                      x = 3;
                 }
             }
             if(ButtonPressed(BTNRIGHT, true))
             {
                 if(ButtonPressed(BTNRIGHT, true))
                 {
                 }
                 else
                 {
                     x = 2;
                 }
             }
             else
             {
                 if(ButtonPressed(BTNRIGHT, true))
                 {
                 }
                 else
                 {
                     x = 1;
                 }
             }
         }
         else
         {
             if(ButtonPressed(BTNLEFT, true))
             {
                 if(ButtonPressed(BTNRIGHT, true))
                 {
                 }
                 else
                 {
                     x = 5;
                 }
             }
             if(ButtonPressed(BTNRIGHT, true))
             {
                 if(ButtonPressed(BTNLEFT, true))
                 {
                 }
                 else
                 {
                      x = 4;
                 }
             }
             else
             {
                 if(ButtonPressed(BTNLEFT, true))
                 {
                 }
                 else
                 {
                      x = 0;
                 }
             }
         }
         ClearScreen();
         NumOut(50, LCD_LINE4, x);
         Wait(30);
      }
}

task operate()
{
    if(x == 0)
    {
        RemoteOff(1, OUT_BC);
    }
    if(x == 1)
    {
        if(SENSOR_1 == 1)
        {
            RemoteOnFwd(1, OUT_BC, 75);
        }
        else
        {
            RemoteOnRev(1, OUT_BC, 75);
        }
    if(x == 2)
    {
         if(SENSOR_1 == 1)
        {
            RemoteOnFwdSync(1, OUT_BC, 75, 100);
        }
        else
        {
            RemoteOnRevSync(1, OUT_BC, 75, 100);
        }
    }
    if(x == 3)
    {
         if(SENSOR_1 == 1)
        {
            RemoteOnFwdSync(1, OUT_BC, 75, -100);
        }
        else
        {
            RemoteOnRevSync(1, OUT_BC, 75, -100);
        }
    }
    if(x == 4)
    {
         if(SENSOR_1 == 1)
        {
            RemoteOnFwd(1, OUT_B, 75);
        }
        else
        {
            RemoteOnRev(1, OUT_B, 75);
        }
    }
    if(x == 5)
    {
         if(SENSOR_1 == 1)
        {
            RemoteOnFwd(1, OUT_C, 75);
        }
        else
        {
            RemoteOnRev(1, OUT_C, 75);
        }
    }
}

task main()
{
    SetSensorTouch(IN_1);
    SetSensorTouch(IN_2);
    Precedes(grab, determine, operate);
}
The sooner you guys can reply the better, cause I've got a deadline for this project.

P.S. If you're Muntoo viewing this, I know you told me not to use "until(cond);" and to instead use "while(!(cond));" because it's more C like. Well the former is easier to remember so I'm going to stick with that.
-Hayden
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: BT direct commands and Multi button presses

Post by afanofosc »

Here's a version of your program that fixes all the compiler errors.

Code: Select all

int x;

task grab()
{
    while(true)
    {
        until(SENSOR_2 == 1);
        until(SENSOR_2 == 0);
//        RemoteOnRev(1, OUT_A, 100);
        until(SENSOR_2 == 1);
        until(SENSOR_2 == 0);
//        RemoteOnFwd(1, OUT_A, 100);
        Wait(1000);
//        RemoteOff(1, OUT_A);
    }
}

task determine()
{
  while(true)
  {
    if(ButtonPressed(BTNCENTER, true))
    {
      if(ButtonPressed(BTNLEFT, true))
      {
        if(ButtonPressed(BTNRIGHT, true))
        {
        }
        else
        {
          x = 3;
        }
      }
      if(ButtonPressed(BTNRIGHT, true))
      {
        if(ButtonPressed(BTNRIGHT, true))
        {
        }
        else
        {
          x = 2;
        }
      }
      else
      {
        if(ButtonPressed(BTNRIGHT, true))
        {
        }
        else
        {
          x = 1;
        }
      }
    }
    else
    {
      if(ButtonPressed(BTNLEFT, true))
      {
        if(ButtonPressed(BTNRIGHT, true))
        {
        }
        else
        {
          x = 5;
        }
      }
      if(ButtonPressed(BTNRIGHT, true))
      {
        if(ButtonPressed(BTNLEFT, true))
        {
        }
        else
        {
          x = 4;
        }
      }
      else
      {
        if(ButtonPressed(BTNLEFT, true))
        {
        }
        else
        {
          x = 0;
        }
      }
    }
    ClearScreen();
    NumOut(50, LCD_LINE4, x);
    Wait(30);
  }
}

task operate()
{
    if(x == 0)
    {
//        RemoteOff(1, OUT_BC);
    }
    if(x == 1)
    {
        if(SENSOR_1 == 1)
        {
//            RemoteOnFwd(1, OUT_BC, 75);
        }
        else
        {
//            RemoteOnRev(1, OUT_BC, 75);
        }
    }
    if(x == 2)
    {
         if(SENSOR_1 == 1)
        {
//            RemoteOnFwdSync(1, OUT_BC, 75, 100);
        }
        else
        {
//            RemoteOnRevSync(1, OUT_BC, 75, 100);
        }
    }
    if(x == 3)
    {
         if(SENSOR_1 == 1)
        {
//            RemoteOnFwdSync(1, OUT_BC, 75, -100);
        }
        else
        {
//            RemoteOnRevSync(1, OUT_BC, 75, -100);
        }
    }
    if(x == 4)
    {
         if(SENSOR_1 == 1)
        {
//            RemoteOnFwd(1, OUT_B, 75);
        }
        else
        {
//            RemoteOnRev(1, OUT_B, 75);
        }
    }
    if(x == 5)
    {
        if(SENSOR_1 == 1)
        {
//            RemoteOnFwd(1, OUT_C, 75);
        }
        else
        {
//            RemoteOnRev(1, OUT_C, 75);
        }
    }
}

task main()
{
    Precedes(grab, determine, operate);
    SetSensorTouch(IN_1);
    SetSensorTouch(IN_2);
}
Here are links to all the Remote* functions defined in the NXC API for the system commands and the direct commands:

http://bricxcc.sourceforge.net/nbc/nxcd ... 3ccc0.html

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

As you can see there are no functions called RemoteOnRev or RemoteOnFwd or RemoteOff. Can you tell me where you got these function names from?

Also, if you are going to use either while(!condition); or until(condition); then you really have to stick the semicolon on at the end or else you will not get the results that you anticipate.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: BT direct commands and Multi button presses

Post by afanofosc »

Your operate task will also terminate very quickly since you don't have an infinite while loop in it. Are you trying to sense simultaneous presses of multiple buttons? If so, that definitely requires the enhanced firmware and only certain button combinations can be used simultaneously. Which ones work and which ones do not I don't recall off the top of my head. Maybe Muntoo does? I'm pretty sure there is a thread here (but maybe it was on nxtasy.org) about how to use that enhanced firmware feature.

I would try, if I were you, to remember the KISS principle. Which in this case means: only use one task unless you absolutely can't do what you are trying to do without having more than one task. I don't see any indication in your code that you absolutely need multiple tasks (except maybe the wait 1000 in task grab)

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
haydenstudios
Posts: 252
Joined: 22 Oct 2010, 20:05
Location: The United States of America
Contact:

Re: NXC: BT direct commands and Multi button presses

Post by haydenstudios »

Sorry for taking so long to reply, I've been a bit occupied. While I was able to make a work around and fulfill my deadline, your reply is appriciated none the less.
afanofosc wrote:As you can see there are no functions called RemoteOnRev or RemoteOnFwd or RemoteOff. Can you tell me where you got these function names from?
I actually did not directly get those non-existent API functions from anywhere. One of the examples in Danielle's tutorial:

Code: Select all

//MASTER
#define BT_CONN 1
#define MOTOR(p,s) RemoteSetOutputState(BT_CONN, p, s, \
OUT_MODE_MOTORON+OUT_MODE_BRAKE+OUT_MODE_REGULATED, \
OUT_REGMODE_SPEED, 0, OUT_RUNSTATE_RUNNING, 0)
sub BTCheck(int conn){
if (!BluetoothStatus(conn)==NO_ERR){
TextOut(5,LCD_LINE2,"Error");
Wait(1000);
Stop(true);
}
}
task main(){
BTCheck(BT_CONN);
RemotePlayTone(BT_CONN, 4000, 100);
until(BluetoothStatus(BT_CONN)==NO_ERR);
Wait(110);
RemotePlaySoundFile(BT_CONN, "! Click.rso", false);
until(BluetoothStatus(BT_CONN)==NO_ERR);
//Wait(500);
RemoteResetMotorPosition(BT_CONN,OUT_A,true);
until(BluetoothStatus(BT_CONN)==NO_ERR);
MOTOR(OUT_A,100);
Wait(1000);
MOTOR(OUT_A,0);
}
showed remote sound functions with no difference from the original command except that it has "Remote", and the first extra value in the parentheses being the connection number to which it is sent, I assumed that the same was so for all commands. So it was my own assumption that led me to that mistake.
afanofosc wrote:Your operate task will also terminate very quickly since you don't have an infinite while loop in it.
Whoops, thanks for catching that, even though that wasn't why I posted this topic. I have changed my code to have that in a loop, and will edit my original post soon after I finish this post. EDIT: *yawn* Maybe I'll wait till tomorrow to do that, so I think I will go to bed now.
afanofosc wrote:Are you trying to sense simultaneous presses of multiple buttons?
Yes, precisely. That is the main reason I posted this topic. The bluetooth commands were a sidequest, the simultaneous button press is what I REALLY posted this for.
afanofosc wrote:If so, that definitely requires the enhanced firmware and only certain button combinations can be used simultaneously. Which ones work and which ones do not I don't recall off the top of my head. Maybe Muntoo does? I'm pretty sure there is a thread here (but maybe it was on nxtasy.org) about how to use that enhanced firmware feature.
I have the enhenced firmware on both ym NXTs. I used the search function to make sure no such topic previously existed, but found none. Then again, it does eliminate words under four letters, and too common of words, (which is very annoying) so that might have filtered that topic out of the results.
afanofosc wrote:I would try, if I were you, to remember the KISS principle. Which in this case means: only use one task unless you absolutely can't do what you are trying to do without having more than one task. I don't see any indication in your code that you absolutely need multiple tasks (except maybe the wait 1000 in task grab)

John Hansen
Ok, thanks for the tip.
-Hayden
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 6 guests