LEGO Engineering

| | | | | | | |
In NXT-G, how can I write a program that allows two touch sensors to control the motors of an NXT car?


20th of April, 2010

(15 )
93% thumbs up 6% thumbs down

Here are three possible ways for two touch sensors to control a car's movement:

Firstly, and most simply, have a sensor in front and a sensor in back so that if the car runs into something it backs up, and if it backs into something it goes forward. The following program does just that, with everything in an infinite loop so that the car goes back and forth continuously.




The second two methods involve the use of touch sensors as a type of external controller.

The first controller has two touch sensors. When one is held down, the car moves forward; when the other is held down, it goes backwards.  The only way to do this is to use embedded touch sensor switches, as shown below. A switch allows two actions to happen when certain criteria are met, similar to an event in ROBOLAB.  For example, the touch switch executes one set of commands when the sensor is pressed and another when it is not pressed. The process may look involved, but it is relatively simple: Check touch sensor 1. If it is pressed, first stop the motors (to reset the system, so to speak) and then move forward.  If it is NOT pressed, then check sensor two. If sensor 2 is pressed, then stop the motors, then move backwards. If it is NOT pressed, then stop all the motors. Put this whole sequence in an infinite loop to allow the touch sensors to have continuous control while the program is running, and you're done!



The third and final way, and perhaps the recommended way, would be to have each touch sensor control only ONE motor. This way, if you only press one touch sensor, you can turn your car as long as the other motor is 'floated' (allowed to turn freely), designated in NXT by the 'coast' option. Pressing both sensors results in forward motion. Use caution when using this on 4-wheeled vehicles, however, because even when floated there will probably be too much friction for the car to turn. This works expecially well with either tank treads or vehicles with 2 wheels and a skid for balance. The programming on this consists of 2 simultaneous tasks, each consisting of one of the touch switches seen above. This is simpler because there is no confusion over embedding the loops and/or giving the NXT contradictory commands simultaneously with each switch.


   :
   :
* :
* :
 


Home | About Us | Contact Us

© 2013 Center for Engineering Educational Outreach, Tufts University
© 2013 The LEGO Group. All rights reserved.