Advertisement
RybaSG

konwersjaUpdate

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