home *** CD-ROM | disk | FTP | other *** search
- // use defines to make it easy to switch motors or sensors
- #define LEFT OUT_A
- #define RIGHT OUT_C
- #define ARM OUT_B
- #define EYE SENSOR_2
- #define MOTOR_PAUSE 2
- #define TURN_TRESHOLD 8
- #define DANCE_LEN 8
-
- task main()
- {
- ClearMessage();
- until( Message() == 2 );
-
-
- int min_threshold;
- int max_threshold;
- int correct_turn;
- int pre_eye;
- int cur_eye;
- int last_turn; // 1 - right 0 - left
- int counter;
- int i;
- int j;
-
- counter = 0;
- last_turn = 0;
-
- // tell RCX that sensor 2 is a light sensor
- SetSensor(EYE, SENSOR_LIGHT);
-
- // set threshold to a little darker than current
- min_threshold = EYE - 4;
- max_threshold = EYE + 4;
-
- // turn on both motors (robot moves forward)
- On(RIGHT + LEFT);
-
- // wait for something darker
- until( EYE <= min_threshold ) { // will stop at black line
-
- if( EYE >= max_threshold ) { // going off line
-
- counter = 0;
-
- if( last_turn == 1 ) {
- correct_turn = 0;
- cur_eye = EYE;
- until( cur_eye <= max_threshold ) { // try turning right
- last_turn = 1;
- Rev( RIGHT );
- Wait( MOTOR_PAUSE );
-
- if( EYE >= max_threshold ) {
- correct_turn = correct_turn + 1;
- } else {
- correct_turn = 0;
- }
-
- if( correct_turn >= TURN_TRESHOLD) {
- correct_turn = 0;
- Fwd( RIGHT );
- cur_eye = EYE;
- until( cur_eye <= max_threshold ) {
- last_turn = 0;
- Rev( LEFT );
- Wait( MOTOR_PAUSE );
- cur_eye = EYE;
- }
- Fwd( LEFT );
- }
- cur_eye = EYE;
- }
- Fwd( RIGHT );
- } else {
- correct_turn = 0;
- until( EYE <= max_threshold ) { // try turning left
- last_turn = 0;
- Rev( LEFT );
- Wait( MOTOR_PAUSE );
- cur_eye = EYE;
-
- if( EYE >= max_threshold ) {
- correct_turn = correct_turn + 1;
- } else {
- correct_turn = 0;
- }
-
- if( correct_turn >= TURN_TRESHOLD ) {
- correct_turn = 0;
- Fwd( LEFT );
- until( EYE <= max_threshold ) {
- last_turn = 1;
- Rev( RIGHT );
- Wait( MOTOR_PAUSE );
- }
- Fwd( RIGHT );
- }
- }
- Fwd( LEFT );
- }
-
- }
- counter = counter + 1;
-
- if( counter == 30 ) { // change last direction after straight line
- if( last_turn == 0 ) {
- last_turn = 1;
- } else {
- last_turn = 0;
- }
- }
-
- }
-
-
- // stop the robot and play a sound
- Off(RIGHT + LEFT);
-
- // hit key
- On( ARM );
- Wait( 23 );
- Off( ARM );
-
- // wait for tune to begin
- Wait( 250 );
-
- // retract arm
- Rev( ARM );
- On( ARM );
- Wait( 20 );
- Off( ARM );
-
- // withdraw
- Rev( LEFT + RIGHT );
- On( LEFT + RIGHT );
- Wait( 100 );
-
- // dance
- Fwd( LEFT + ARM );
- Rev( RIGHT );
-
- On( ARM + LEFT + RIGHT );
-
- for( j = 0; j < 2; j++) {
-
- for( i = 0; i < DANCE_LEN; i++) {
- Toggle( ARM + LEFT + RIGHT );
- Wait( 15 );
- Off( ARM );
- Wait( 15 );
- On ( ARM );
- }
-
- Fwd( LEFT + RIGHT );
- }
-
- Rev( RIGHT );
-
- for( i = 0; i < DANCE_LEN; i++) {
- Toggle( ARM );
- Wait( 15 );
- Off( ARM );
- Wait( 15 );
- On ( ARM );
- }
-
- Fwd( ARM );
- Wait( 20 );
-
- // stop and end
- Off( ARM + LEFT + RIGHT );
- }