Advertisement
RybaSG

KonswersjaUpdatevol2

Jul 5th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.51 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string.h>
  4.  
  5. int map(int x, int in_min, int in_max, int out_min, int out_max);
  6.  
  7. int main()
  8. {
  9.   using namespace std;
  10.  
  11.   char data[50] = "";
  12.   char str1[] = "Left";
  13.   char str2[] = "155";
  14.   char key[] = "0123456789";
  15.   char result[5] = "";;
  16.   char *w;
  17.  
  18.   int value = 0, PWM, n = 0;
  19.  
  20.   float voltage;
  21.  
  22.   strcat(data, str1);
  23.   strcat(data, str2);
  24.  
  25.     if ( strncmp(data, "Left", 4) == 0 )
  26.     {
  27.         //use logic analyzer to measure execution time
  28.         value = strlen(data);
  29.         cout << "Left detected: " << data << ", Length: " << value << endl;
  30.        
  31.         w = strpbrk(data, key);
  32.         while ( w != NULL )
  33.         {
  34.             result[n++] = *w;
  35.             w = strpbrk(w+1, key);
  36.         }
  37.        
  38.         cout << "Data: " << result << ", Length: " << strlen(result) << endl;
  39.        
  40.         value = atoi(result);
  41.        
  42.         cout << "Conversion: " << value << endl;
  43.         //use logic analyzer to measure execution time
  44.     }
  45.     else if ( strncmp(data, "Right", 5) == 0 )
  46.     {
  47.         //use logic analyzer to measure execution time
  48.         value = strlen(data);
  49.         cout << "Right detected: " << data << " Length: " << value << endl;
  50.        
  51.         w = strpbrk(data, key);
  52.         while ( w != NULL )
  53.         {
  54.             result[n++] = *w;
  55.             w = strpbrk(w+1, key);
  56.         }
  57.        
  58.         cout << "Data: " << result << " Length: " <<strlen(result) << endl;
  59.        
  60.         value = atoi(result);
  61.         PWM = map(value, 0, 1023, 0, 100);
  62.        
  63.         cout << "PWM value: " << PWM << '%' << endl;
  64.        
  65.         //use logic analyzer to measure execution time
  66.     }
  67.     else if ( strncmp(data, "Meas", 4) == 0 )
  68.     {
  69.         //use logic analyzer to measure execution time
  70.         value = strlen(data);
  71.         cout << "Meas detected: " << data << " Length: " << value << endl;
  72.        
  73.         w = strpbrk(data, key);
  74.         while ( w != NULL )
  75.         {
  76.             result[n++] = *w;
  77.             w = strpbrk(w+1, key);
  78.         }
  79.        
  80.         cout << "Data: " << result << " Length: " <<strlen(result) << endl;
  81.        
  82.         voltage = atoi(result)*2.56/1023;
  83.        
  84.         cout << "Conversion: " << voltage*1.953 << "V" << endl;
  85.         //use logic analyzer to measure execution time
  86.     }
  87.  
  88.     return 0;
  89. }
  90.  
  91.  
  92. int map(int x, int in_min, int in_max, int out_min, int out_max)
  93. {
  94.   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement