NXC: Force sensor to read a specific value?

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: Force sensor to read a specific value?

Post by haydenstudios »

Hey there. So about a year ago I got into NXC and hoped to get more and more familiar with it, but then that kinda failed cause I started getting busy. So now I've got more time, and will try again at this whole mastering NXC thing. I right now have a question. So you are able to reset a rotation sensor, which will drop its value to zero. Is there any way to make a sensor reset to any value you want?

Now you might wonder what I would want this for. Say that I'm making a pong game, and I'm using an RCX rotation sensor to control the tray. I make the tray display with an X value equal to the rotation sensor, so that you can control the tray just like those old Atari controllers. If it goes too far to the left, I can simply reset the rotation sensor, and it will stop at the left edge of the screen. This requires very few lines of code. As for doing this on the right side of the screen, it would be very handy to be able to keep the rotation sensor from reading anything above 76. Here's what code I have so far:

Code: Select all

task main()
{
  SetSensor(IN_2, SENSOR_ROTATION);
  while(true)
  {
    RectOut(SENSOR_2, 0, 24, 4); Wait(30); //Display the tray with the X
    ClearScreen();                        //value equal to the rotation sensor.
    if(SENSOR_2 <= 0)
    {
      ClearSensor(IN_2); //If it goes too far to the left, it resets the sensor to zero
    }                    //so that the tray can't go too far to the left.
  }
}
I searched through this list of API functions, but didn't find what I was looking for. Then again, I didn't search that thoroughly... I could probably get this tray working properly without this sensor manipulation feature, but the code wouldn't be anywhere near as efficient. Is there currently any way to force the reading not to go above a specified value?
-Hayden
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: Force sensor to read a specific value?

Post by spillerrec »

It does not appear to be possible. Just do it manually, it will only have minor impact on the performance.
I would suggest you to separate the code that reads the sensor from the rest of the code. "SENSOR_X" reads the current value from the sensor, so it might change during the execution of the current iteration. If you had:

Code: Select all

RectOut(SENSOR_2, 0, 24, 4);
RectOut(SENSOR_2, 59, 24, 4);
then the two rectangles might not be properly aligned. I would suggest you to do something like this:

Code: Select all

int offset = 0;
while( true ){
   //Get the current value, and limit the range
   int pos = SENSOR_2 + offset;
   if( pos < 0 ){
      offset -= pos;
      pos = 0;
   }
   else if( pos > 74 ){
      offset += pos - 74;
      pos = 74;
   }

   //Start drawing, using pos
   RectOut( pos, 0, 24, 4 );
}
Sure, it is a bit extra code, but since it is done only once per iteration it shouldn't affect your performance (as long as you are doing something useful in your loop).
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
haydenstudios
Posts: 252
Joined: 22 Oct 2010, 20:05
Location: The United States of America
Contact:

Re: NXC: Force sensor to read a specific value?

Post by haydenstudios »

Ok. Looks like I can't directly manipulate the reading, so I guess I'll have to do something more elaborate like what you typed out. Thanks!
-Hayden
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC: Force sensor to read a specific value?

Post by mattallen37 »

If you want to get even more complex, I think you can alter the encoder readings to anything you want using the IO map directly. I don't know that this works, and if so, it might only be for the NXT motor encoders (not necessarily the RCX rotation sensors).

However, as spiller said, a user-code offset would be much better.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests