Advertisement
Guest User

NxC NxT Robot Code

a guest
Jul 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.17 KB | None | 0 0
  1. void Reset() { // Resets the speed of the motors to 0
  2.   Off(OUT_B);
  3.   Off(OUT_A);
  4. }
  5.  
  6. void TurnInCircle() { // Turns the robot in a clockwise motion.
  7.   int speed = 35; //Speed in which it turns.
  8.   OnFwd(OUT_A, -speed);
  9.   OnFwd(OUT_B, speed);
  10. }
  11.  
  12. void TurnRight() { //Turn right
  13.   int speed = 35; //Speed in which it turns.
  14.   Reset();
  15.   OnFwd(OUT_B, speed);
  16. }
  17.  
  18. void TurnLeft() { //Turn left
  19.   int speed = 35; //Speed in which it turns.
  20.   Reset();
  21.   OnFwd(OUT_A, speed);
  22. }
  23.  
  24. void MoveForward() { //Move forward
  25.   int speed = 50; //Speed in which it moves.
  26.   OnFwd(OUT_A, speed - 1); //Tries to stabalise the motor speed
  27.   OnFwd(OUT_B, speed + 2); //Tries to stabalise the motor speed
  28. }
  29.  
  30. bool CheckForLine() { //Checks for the line.
  31.   int minValue = 30; //The lowest value allowed to be considered on the line.
  32.   if(SensorValue(IN_2) < minValue) { //If the sensor value is less than the lowest value.
  33.     TextOut(0,LCD_LINE1,"LINE: NOT"); //Print on brick
  34.     return true;
  35.   } else {
  36.         TextOut(0,LCD_LINE1,"LINE: YES"); //Print on brick
  37.     return false;
  38.  
  39.   }
  40. }
  41.  
  42. void CheckLine() { //Is it on the line?
  43.   if(CheckForLine()) { //If the robot is / isn't on the line.
  44.     TurnInCircle(); //If it's not, turn in a circle to look for it.
  45.   } else {
  46.     MoveForward(); //If it is, move forward.
  47.   }
  48. }
  49.  
  50. void CheckSound() { //Has someone clapped?
  51.   int threshold = 60; //The highest amount of sound allowed.
  52.   if(SensorValue(IN_3) > threshold) { //If it is more than the threshold.
  53.     Wait(300); //Wait for .3 seconds.
  54.     TurnInCircle(); //If it's not, turn in a circle.
  55.     Wait(3660 + (3660 / 4)); //Wait till it has turned 360 + 180
  56.     Reset();
  57.   }
  58. }
  59.  
  60. bool CheckObstacles() { //Is there an obstacle in the way?
  61.   int distance = 20; //lowest distance between the robot and the obstacle
  62.   bool noLine = false; //bool for the while loop.
  63.  
  64.   if(SensorUS(IN_1) < distance) { //Is the robot too close to an obstacle.
  65.     noLine = true; //Prepare the bool for the loop.
  66.     Reset(); //Reset the motors.
  67.     TurnLeft(); //Turn left
  68.     Wait(1500);
  69.     MoveForward(); //Move forward
  70.     Wait(1500);
  71.     TurnRight(); //Turn right
  72.     Wait(1500);
  73.     MoveForward(); //Move forward
  74.     Wait(2500);
  75.     TurnRight(); //Turn right
  76.     Wait(1600);
  77.     Reset(); //Reset the motors.
  78.     Wait(SEC_2);
  79.     while (noLine) { //While loop to see if it's on the line yet.
  80.       if(CheckForLine()) {
  81.         MoveForward(); //If it's not on the line, then move forward.
  82.       } else {
  83.         TurnLeft(); // Else, move to the position where it can correctly follow the line.
  84.         Wait(1000);
  85.         MoveForward();
  86.         Wait(1000);
  87.         TurnRight();
  88.         Wait(500);
  89.         MoveForward();
  90.         Wait(500);
  91.         return true;
  92.       }
  93.     }
  94.   } else {
  95.     return true;
  96.   }
  97. }
  98.  
  99. task main() { //Main task that runs the other methods.
  100.   SetSensorLight(IN_2); //Init the sensors.
  101.   SetSensorUltrasonic(IN_1);
  102.   SetSensorSound(IN_3);
  103.   while(true) { //While the programme is running.
  104.     if(CheckObstacles()) { //If there isn't an obstacle in the way then...
  105.       CheckSound(); //Check for a clap.
  106.       CheckLine(); //Check for the line.
  107.     }
  108.   }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement