Advertisement
Guest User

Week 3 Programming Assignment 2

a guest
Apr 8th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | None | 0 0
  1. #if 1
  2. //ultrasonic sensor//
  3.    
  4.     void motor_tankturn(int turn_speed, int delay);
  5.     void motor_tankturn_random(void);
  6.    
  7. void zmain(void)
  8. {
  9.     bool OnOff = false;
  10.     Ultra_Start();                          // Ultra Sonic Start function
  11.     motor_stop();
  12.    
  13.     while(true) {  // Waits for users button press to start
  14.      if(SW1_Read() == 0) {
  15.         printf("\nStarting...\n");
  16.         motor_start();
  17.         motor_forward(0,0);
  18.         OnOff = true;
  19.         break;
  20.         }
  21.     }
  22.    
  23.     while(OnOff) {
  24.        
  25.        
  26.         int d = Ultra_GetDistance();
  27.         if ( d <= 10 )
  28.         {
  29.             printf("WARNING! Distance to obstacle: %dcm\r\n", d);
  30.             motor_forward(0,0);
  31.             motor_backward(100,400);
  32.             motor_backward(0,0);
  33.             motor_tankturn_random();
  34.             /*motor_tankturn(53,990);*/
  35.             vTaskDelay(500);
  36.         } else {
  37.             // Print the detected distance (centimeters)
  38.         printf("distance = %d\r\n", d);
  39.         motor_forward(50,200);
  40.         motor_forward(0,0);
  41.         }
  42.     }
  43. }  
  44.  
  45. void motor_tankturn(int turn_speed, int delay)
  46. {
  47.     SetMotors(0,0,turn_speed, 0,delay);
  48.     motor_forward(0,0);
  49. }
  50. void motor_tankturn_random(void)
  51. {
  52.     int delay = (rand() % (2965 +1 - 990) + 990);
  53.     int leftright = (rand() % (1 + 1));
  54.     printf("\n%d ", leftright);
  55.     printf("- %d\n", delay);
  56.     if (leftright == 1) {
  57.         SetMotors(0,0,53, 0,delay);
  58.     } else {
  59.         SetMotors(0,0,0,53,delay);
  60.     }
  61.     motor_forward(0,0);
  62. }
  63. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement