NXC: Force sensor to read a specific value?
Posted: 18 May 2012, 20:32
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:
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?
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.
}
}