Page 1 of 1

[NXC]Questions from a newbie :-)

Posted: 02 Apr 2012, 22:15
by jojoguy14
Hello people!

I' starting to learn NXC (slowly!) I have John Hansen's book and I've been using it as a reference.
I'm an NXT-G guy. I LOVE graphical programming languages :) . So, I've started "copying" the NXT-G Robot Center programs to NXC. I'm on the ShooterBot with the Color Sensor program (but, I'm using a light sensor in my program).
1. I need help (in general) how to setup a sensor and using/reading from it.
2. How to use a While loop to control when I want the program to stop.
Here's the code (MUCH easier pasting NXC code than NXT-G code, lol! :) )

Code: Select all

task main() {
  SetSensorType(S3, SENSOR_TYPE_LIGHT_INACTIVE);
  SetSensorMode(S3, SENSOR_MODE_PERCENT);
    while (ButtonState(BTNCENTER) = false) {
    if (Sensor(S3) < 50) {
      RotateMotor(OUT_B, 75, 954);
      RotateMotor(OUT_C, 75, -954);
      RotateMotor(OUT_BC, 75, 1440);
    } else {
    OnFwd(OUT_BC, 50);
    }
  }
}
I hope you can help me out!
Joey "jojoguy10"

Re: [NXC]Questions from a newbie :-)

Posted: 03 Apr 2012, 01:41
by mattallen37
This compiles:

Code: Select all

task main() {
  SetSensorType(S3, SENSOR_TYPE_LIGHT_INACTIVE);
  SetSensorMode(S3, SENSOR_MODE_PERCENT);
    while (ButtonState(BTNCENTER) == false) {        // For comparison use == and not =
    if (Sensor(S3) < 50) {
      RotateMotor(OUT_B, 75, 954);
      RotateMotor(OUT_C, 75, -954);
      RotateMotor(OUT_BC, 75, 1440);
    } 
    else {
      OnFwd(OUT_BC, 50);
    }
  }
}
When you're comparing values, you need to use 2 equals signs, instead of one.

Re: [NXC]Questions from a newbie :-)

Posted: 03 Apr 2012, 12:49
by jojoguy14
Thanks a bunch!
Another question:
How do I make a point turn in NXC (both motors move in opposite directions at the same time)?

Re: [NXC]Questions from a newbie :-)

Posted: 03 Apr 2012, 15:10
by mattallen37
Like this?

Code: Select all

OnFwd(OUT_B, 100);
OnRev(OUT_C, 100);

Re: [NXC]Questions from a newbie :-)

Posted: 03 Apr 2012, 18:12
by jojoguy14
mattallen37 wrote:Like this?

Code: Select all

OnFwd(OUT_B, 100);
OnRev(OUT_C, 100);
Yes, but how do you do that for a certain amount of degrees?

Re: [NXC]Questions from a newbie :-)

Posted: 03 Apr 2012, 18:34
by h-g-t

Re: [NXC]Questions from a newbie :-)

Posted: 03 Apr 2012, 18:49
by mattallen37
For absolute position control, I usually use the APR functions that are FW implemented. I don't think I have ever really used RotateMotor before, so I'm not exactly sure how it works.

Re: [NXC]Questions from a newbie :-)

Posted: 03 Apr 2012, 19:58
by jojoguy14
h-g-t wrote:You need to use rotate motor - RotateMotor(OUT_AC, 50,360 );

See http://css.engineering.uiowa.edu/~cie/L ... torial.pdf

and http://bricxcc.sourceforge.net/nbc/nxcdoc/NXC_Guide.pdf
Well....thanks, but that didn't help with turning the motors in OPPOSITE directions. But, thanks to you, I looked it up and found something similar: RotateMotorEx. With that, I can define the "turnpct" :-). I set that to 100% and it worked like a charm!

Thanks!