/*------------------------------------------------------------------\ | | | A very simple program to produce a roaming bug demo for the RCX. | | It assumes the robot has one touch sensor and one light sensor | | (the light sensor is not used). | | | | The robot heads forward until it hits an obstacle, then backs up | | spins, and heads off again in the new direction. | | | | Simon Parsons | | City University of New York | | February 2006 | | | \------------------------------------------------------------------*/ // Define sensor ports #define BUMP SENSOR_1 #define EYE SENSOR_3 // Define motor ports #define LEFT_MOTOR OUT_A #define RIGHT_MOTOR OUT_C // Set light sensor threshold #define STOP_THRESHOLD 40 task main() { // Initialise sensors SetSensor(BUMP, SENSOR_TOUCH); SetSensor(EYE, SENSOR_LIGHT); //Switch on motors OnFwd(LEFT_MOTOR+RIGHT_MOTOR); while(true) { // While the touch sensor is not activated, just keep going if (BUMP == 1) { // Once we hit something, switch motors off, then switch them on // in reverse and wait for a while. Off(LEFT_MOTOR+RIGHT_MOTOR); OnRev(LEFT_MOTOR+RIGHT_MOTOR); Wait(200); // Now switch the motors off again, and switch them on in opposite // directions to turn the robot, again wait a while. Off(LEFT_MOTOR+RIGHT_MOTOR); OnFwd(LEFT_MOTOR); OnRev(RIGHT_MOTOR); Wait(100); // Now switch the motor that was in reverse, to go forward. Off(RIGHT_MOTOR); OnFwd(RIGHT_MOTOR); } } }