Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example program
- #include <iostream>
- #include <string.h>
- int map(int x, int in_min, int in_max, int out_min, int out_max);
- int main()
- {
- using namespace std;
- char data[50] = "";
- char str1[] = "Left";
- char str2[] = "155";
- char key[] = "0123456789";
- char result[5] = "";;
- char *w;
- int value = 0, PWM, n = 0;
- float voltage;
- strcat(data, str1);
- strcat(data, str2);
- if ( strncmp(data, "Left", 4) == 0 )
- {
- //use logic analyzer to measure execution time
- value = strlen(data);
- cout << "Left detected: " << data << ", Length: " << value << endl;
- w = strpbrk(data, key);
- while ( w != NULL )
- {
- result[n++] = *w;
- w = strpbrk(w+1, key);
- }
- cout << "Data: " << result << ", Length: " << strlen(result) << endl;
- value = atoi(result);
- cout << "Conversion: " << value << endl;
- //use logic analyzer to measure execution time
- }
- else if ( strncmp(data, "Right", 5) == 0 )
- {
- //use logic analyzer to measure execution time
- value = strlen(data);
- cout << "Right detected: " << data << " Length: " << value << endl;
- w = strpbrk(data, key);
- while ( w != NULL )
- {
- result[n++] = *w;
- w = strpbrk(w+1, key);
- }
- cout << "Data: " << result << " Length: " <<strlen(result) << endl;
- value = atoi(result);
- PWM = map(value, 0, 1023, 0, 100);
- cout << "PWM value: " << PWM << '%' << endl;
- //use logic analyzer to measure execution time
- }
- else if ( strncmp(data, "Meas", 4) == 0 )
- {
- //use logic analyzer to measure execution time
- value = strlen(data);
- cout << "Meas detected: " << data << " Length: " << value << endl;
- w = strpbrk(data, key);
- while ( w != NULL )
- {
- result[n++] = *w;
- w = strpbrk(w+1, key);
- }
- cout << "Data: " << result << " Length: " <<strlen(result) << endl;
- voltage = atoi(result)*2.56/1023;
- cout << "Conversion: " << voltage*1.953 << "V" << endl;
- //use logic analyzer to measure execution time
- }
- return 0;
- }
- int map(int x, int in_min, int in_max, int out_min, int out_max)
- {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement