Advertisement
djazz

CraneTank - Tank's NXC source

Sep 6th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1.  
  2. task updateSensorValues () {
  3.     string message;
  4.     int light;
  5.  
  6.     while (true) {
  7.         light = Sensor(IN_3);
  8.         message = NumToStr(light) +","+ NumToStr(SensorUS(IN_4)) +","+ NumToStr(SensorUS(IN_1));
  9.        
  10.         SendMessage(10, message);
  11.        
  12.         //TextOut(0, LCD_LINE3, message);
  13.         Wait(100);
  14.     }
  15. }
  16.  
  17. task updateMotors () {
  18.    
  19.     string buffer, message = "";
  20.     char result;
  21.     while (1) {
  22.        
  23.         // read mailbox
  24.         result = ReceiveMessage(0, true, buffer);
  25.         if (result == 0) {
  26.             message = buffer;
  27.             bool gotMotorMessage = true;
  28.  
  29.             switch (message) {
  30.                 case "TankForward": OnRev(OUT_BC, 50); break;
  31.                 case "TankReverse": OnFwd(OUT_BC, 50); break;
  32.                
  33.                 case "TankLeft":  OnFwd(OUT_C, 70); OnRev(OUT_B, 70); break;
  34.                 case "TankRight": OnFwd(OUT_B, 70); OnRev(OUT_C, 70); break;
  35.  
  36.                 case "TankStop": Off(OUT_BC); break;
  37.  
  38.                 default: gotMotorMessage = false; break;
  39.             }
  40.  
  41.             if (gotMotorMessage) {
  42.                 TextOut(0, LCD_LINE3, "Motor: "+message+"       ");
  43.             }
  44.            
  45.         }
  46.        
  47.         Wait(50);
  48.  
  49.     }
  50. }
  51.  
  52. task updateColor () {
  53.    
  54.     string buffer, message = "";
  55.     char result;
  56.  
  57.     while (1) {
  58.        
  59.  
  60.         // read mailbox
  61.         result = ReceiveMessage(2, true, buffer);
  62.         if (result == 0) {
  63.             message = buffer;
  64.  
  65.             switch (message) {
  66.                 case "red": SetSensorType(IN_2, SENSOR_TYPE_COLORRED); break;
  67.                 case "green": SetSensorType(IN_2, SENSOR_TYPE_COLORGREEN); break;
  68.                 case "blue": SetSensorType(IN_2, SENSOR_TYPE_COLORBLUE); break;
  69.                 case "off": SetSensorType(IN_2, SENSOR_TYPE_COLORNONE); break;
  70.             }
  71.  
  72.            
  73.             TextOut(0, LCD_LINE4, "Color: "+message+"       ");
  74.         }
  75.        
  76.         Wait(50);
  77.  
  78.     }
  79. }
  80.  
  81. task main() {
  82.  
  83.     SetSensorLowspeed(IN_1);
  84.     SetSensorType(IN_2, SENSOR_TYPE_COLORNONE);
  85.     SetSensorType(IN_3, SENSOR_TYPE_LIGHT_INACTIVE);
  86.     SetSensorLowspeed(IN_4);
  87.    
  88.  
  89.     Precedes(updateSensorValues, updateMotors, updateColor);
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement