Advertisement
ollikol

Week 5 assignments by group 11

Apr 19th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.50 KB | None | 0 0
  1. #if 1
  2. //week 5, assignment 1 by Juuso
  3. void zmain(void)
  4. {
  5.     //start real time clock and set time
  6.    
  7.     RTC_Start();
  8.     RTC_TIME_DATE now;
  9.  
  10.     now.Hour = 23;
  11.     now.Min = 59;
  12.     now.Sec = 00;
  13.     RTC_WriteTime(&now);
  14.    
  15.     printf("\nBoot\n");
  16.     send_mqtt("Zumo11/time", "Boot");
  17.  
  18.     while(true)
  19.     {
  20.         //print current time when user button is pressed
  21.         if (SW1_Read() == 0) {
  22.         print_mqtt("Zumo11/time", "%2d:%02d.%02d\n", now.Hour, now.Min, now.Sec, SW1_Read());
  23.         }
  24.         vTaskDelay(1000);
  25.        
  26.         //function for clock
  27.         now.Sec++;
  28.        
  29.         if(now.Sec==60)
  30.         {
  31.         now.Min+=1;
  32.         now.Sec=0;
  33.         }
  34.            
  35.         if(now.Min==60)
  36.         {
  37.         now.Hour+=1;
  38.         now.Min=0;
  39.         }
  40.            
  41.         if(now.Hour==24)
  42.         {
  43.         now.Hour=0;
  44.         now.Min=0;
  45.         now.Sec=0;
  46.         }
  47.     }
  48.  }  
  49. #endif
  50.  
  51. #if 0   //Assignment 2
  52. void zmain(void)
  53. {
  54.     while(true)
  55.     {
  56.         Ultra_Start();
  57.         printf("\nBoot\n");
  58.         send_mqtt("Zumo01/debug", "Boot");
  59.         while(SW1_Read()) vTaskDelay(200);
  60.         motor_start();
  61.         motor_forward(100,0);
  62.         while(true)
  63.         {
  64.             int dist = Ultra_GetDistance();         //Starts detecting distance
  65.             printf("Distance = %d\r\n", dist);          
  66.             vTaskDelay(200);        //Delay between distance scans
  67.             if(dist <= 5)
  68.             {
  69.                 randomturn();
  70.                 motor_forward(100,0);
  71.             }
  72.         }
  73.     }
  74. }
  75. #endif
  76.  
  77. int randomturn()
  78. {
  79.     int randomdir1, randomdir2, randomdir;
  80.     int turn = 262;         //Value for a 90 degree turn
  81.     randomdir = rand() % 2;                  //Calculates random direction for turning
  82.    
  83.     if(randomdir == 1)  {           //Ensures the tracks go in different directions
  84.         randomdir1 = 1;
  85.         randomdir2= 0;
  86.         char direction[] ="left";
  87.         print_mqtt("Zumo11/debug", "%s", direction);         //Sends MQTT message
  88.         printf("%s", direction);
  89.     }
  90.     else    {
  91.         randomdir1 = 0;
  92.         randomdir2 = 1;
  93.         char direction[] ="right";
  94.         print_mqtt("Zumo11/debug", "%s", direction);
  95.         printf("%s", direction);
  96.        
  97.     }
  98.  
  99.     SetMotors(randomdir1,randomdir2, 100, 100, turn);      //calls SetMotors to perform the turn
  100.     printf("randomturn = %d\rms\n", turn);
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement