home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / iTunes Remote / trackstompdance.ncq < prev   
Encoding:
Text File  |  2001-06-23  |  3.0 KB  |  173 lines

  1. // use defines to make it easy to switch motors or sensors
  2. #define LEFT OUT_A
  3. #define RIGHT OUT_C
  4. #define ARM OUT_B
  5. #define EYE SENSOR_2
  6. #define MOTOR_PAUSE 2
  7. #define TURN_TRESHOLD 8
  8. #define DANCE_LEN 8
  9.  
  10. task main()
  11. {
  12.     ClearMessage();
  13.     until( Message() == 2 );
  14.  
  15.  
  16.     int min_threshold;
  17.     int max_threshold;
  18.     int correct_turn;
  19.     int pre_eye;
  20.     int cur_eye;
  21.     int last_turn; // 1 - right 0 - left
  22.     int counter;
  23.     int i;
  24.     int j;
  25.     
  26.     counter = 0;
  27.     last_turn = 0;
  28.         
  29.     // tell RCX that sensor 2 is a light sensor
  30.     SetSensor(EYE, SENSOR_LIGHT);
  31.     
  32.     // set threshold to a little darker than current
  33.     min_threshold = EYE - 4;
  34.     max_threshold = EYE + 4;
  35.     
  36.     // turn on both motors (robot moves forward)
  37.     On(RIGHT + LEFT);
  38.     
  39.     // wait for something darker
  40.     until( EYE <= min_threshold ) { // will stop at black line
  41.     
  42.         if( EYE >= max_threshold ) { // going off line
  43.         
  44.             counter = 0;
  45.             
  46.             if( last_turn == 1 ) {
  47.                 correct_turn = 0;
  48.                 cur_eye = EYE;
  49.                 until( cur_eye <= max_threshold ) { // try turning right
  50.                     last_turn = 1;
  51.                     Rev( RIGHT );
  52.                     Wait( MOTOR_PAUSE );
  53.                     
  54.                     if( EYE >= max_threshold ) {
  55.                         correct_turn = correct_turn + 1;
  56.                     } else {
  57.                         correct_turn = 0;
  58.                     }
  59.                     
  60.                     if( correct_turn >= TURN_TRESHOLD) {
  61.                         correct_turn = 0;
  62.                         Fwd( RIGHT );
  63.                         cur_eye = EYE;
  64.                         until( cur_eye <= max_threshold ) { 
  65.                             last_turn = 0;
  66.                             Rev( LEFT );
  67.                             Wait( MOTOR_PAUSE );
  68.                             cur_eye = EYE;
  69.                         }
  70.                         Fwd( LEFT );
  71.                     }
  72.                     cur_eye = EYE;
  73.                 }
  74.                 Fwd( RIGHT );
  75.             } else {
  76.                 correct_turn = 0;
  77.                 until( EYE <= max_threshold ) { // try turning left
  78.                     last_turn = 0;
  79.                     Rev( LEFT );
  80.                     Wait( MOTOR_PAUSE );
  81.                     cur_eye = EYE;
  82.                     
  83.                     if( EYE >= max_threshold ) {
  84.                         correct_turn = correct_turn + 1;
  85.                     } else {
  86.                         correct_turn = 0;
  87.                     }
  88.                     
  89.                     if( correct_turn >= TURN_TRESHOLD ) {
  90.                         correct_turn = 0;
  91.                         Fwd( LEFT );
  92.                         until( EYE <= max_threshold ) { 
  93.                             last_turn = 1;
  94.                             Rev( RIGHT );
  95.                             Wait( MOTOR_PAUSE );
  96.                         }
  97.                         Fwd( RIGHT );
  98.                     }
  99.                 }
  100.                 Fwd( LEFT );
  101.             }
  102.             
  103.         }
  104.     counter = counter + 1;
  105.     
  106.     if( counter == 30 ) { // change last direction after straight line
  107.         if( last_turn == 0 ) {
  108.             last_turn = 1;
  109.         } else {
  110.             last_turn = 0;
  111.         }
  112.     }
  113.             
  114.     }
  115.         
  116.     
  117.     // stop the robot and play a sound
  118.     Off(RIGHT + LEFT);
  119.  
  120.     // hit key
  121.     On( ARM );
  122.     Wait( 23 );
  123.     Off( ARM );
  124.     
  125.     // wait for tune to begin
  126.     Wait( 250 );
  127.     
  128.     // retract arm
  129.     Rev( ARM );
  130.     On( ARM );
  131.     Wait( 20 );
  132.     Off( ARM );
  133.     
  134.     // withdraw
  135.     Rev( LEFT + RIGHT );
  136.     On( LEFT + RIGHT );
  137.     Wait( 100 );
  138.  
  139.     // dance
  140.     Fwd( LEFT + ARM );
  141.     Rev( RIGHT );
  142.  
  143.     On( ARM + LEFT + RIGHT );
  144.  
  145.     for( j = 0; j < 2; j++) {
  146.  
  147.         for( i = 0; i < DANCE_LEN; i++) {
  148.         Toggle(  ARM + LEFT + RIGHT );
  149.         Wait( 15 );
  150.         Off( ARM );
  151.         Wait( 15 );
  152.         On ( ARM );
  153.         }
  154.     
  155.         Fwd( LEFT + RIGHT );        
  156.     }
  157.     
  158.     Rev( RIGHT );
  159.     
  160.     for( i = 0; i < DANCE_LEN; i++) {
  161.         Toggle( ARM );
  162.         Wait( 15 );
  163.         Off( ARM );
  164.         Wait( 15 );
  165.         On ( ARM );
  166.     }
  167.     
  168.     Fwd( ARM );
  169.     Wait( 20 ); 
  170.  
  171.     // stop and end
  172.     Off( ARM + LEFT + RIGHT );
  173. }