Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. /*
  2. Serial Event example
  3.  
  4. When new serial data arrives, this sketch adds it to a String.
  5. When a newline is received, the loop prints the string and clears it.
  6.  
  7. A good test for this is to try it with a GPS receiver that sends out
  8. NMEA 0183 sentences.
  9.  
  10. NOTE: The serialEvent() feature is not available on the Leonardo, Micro, or
  11. other ATmega32U4 based boards.
  12.  
  13. created 9 May 2011
  14. by Tom Igoe
  15.  
  16. This example code is in the public domain.
  17.  
  18. http://www.arduino.cc/en/Tutorial/SerialEvent
  19. */
  20.  
  21. String inputString = ""; // a String to hold incoming data
  22. bool stringComplete = false; // whether the string is complete
  23.  
  24. // Example 2 - Receive with an end-marker
  25.  
  26. const byte numChars = 255;
  27. char receivedChars[numChars]; // an array to store the received data
  28. int motors[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  29. boolean newData = false;
  30. boolean ledState = false;
  31.  
  32. void setup() {
  33. Serial.begin(115200);
  34. Serial.println("<Arduino is ready>");
  35. pinMode(13, OUTPUT);
  36. }
  37.  
  38. void loop() {
  39. recvWithEndMarker();
  40. showNewData();
  41. if (newData) {
  42.  
  43. for (int i = 0; i < 18; i++) {
  44. Serial.print(motors[i]);
  45. Serial.print(".");
  46. }
  47. Serial.println();
  48. newData = false;
  49.  
  50.  
  51. }
  52.  
  53. }
  54.  
  55. void recvWithEndMarker() {
  56. static byte ndx = 0;
  57. char endMarker = '\n';
  58. char rc;
  59.  
  60. while (Serial.available() > 0 && newData == false) {
  61. rc = Serial.read();
  62.  
  63. if (rc != endMarker) {
  64. receivedChars[ndx] = rc;
  65. ndx++;
  66. if (ndx >= numChars) {
  67. ndx = numChars - 1;
  68. }
  69. }
  70. else {
  71. receivedChars[ndx] = '\0'; // terminate the string
  72. ndx = 0;
  73. newData = true;
  74. }
  75. }
  76. }
  77.  
  78.  
  79. void showNewData() {
  80. if (newData == true) {
  81. Serial.println("This just in ... ");
  82. String str(receivedChars);
  83. String motor0 = getValue(str, ';', 0);
  84. String motor1 = getValue(str, ';', 1);
  85. String motor2 = getValue(str, ';', 2);
  86. String motor3 = getValue(str, ';', 3);
  87. String motor4 = getValue(str, ';', 4);
  88. String motor5 = getValue(str, ';', 5);
  89.  
  90. for (int i = 0; i < 3; i++) {
  91. motors[i] = getValue(motor0, ':', i).toInt();
  92. }
  93.  
  94. for (int i = 0; i < 3; i++) {
  95. motors[i+3] = getValue(motor1, ':', i).toInt();
  96. }
  97.  
  98. for (int i = 0; i < 3; i++) {
  99. motors[i+6] = getValue(motor2, ':', i).toInt();
  100. }
  101.  
  102. for (int i = 0; i < 3; i++) {
  103. motors[i+9] = getValue(motor3, ':', i).toInt();
  104. }
  105.  
  106. for (int i = 0; i < 3; i++) {
  107. motors[i+12] = getValue(motor4, ':', i).toInt();
  108. }
  109. for (int i = 0; i < 3; i++) {
  110. motors[i+15] = getValue(motor5, ':', i).toInt();
  111. }
  112. }
  113. }
  114.  
  115.  
  116. String getValue(String data, char separator, int index)
  117. {
  118. int found = 0;
  119. int strIndex[] = {0, -1};
  120. int maxIndex = data.length()-1;
  121.  
  122. for(int i=0; i<=maxIndex && found<=index; i++){
  123. if(data.charAt(i)==separator || i==maxIndex){
  124. found++;
  125. strIndex[0] = strIndex[1]+1;
  126. strIndex[1] = (i == maxIndex) ? i+1 : i;
  127. }
  128. }
  129.  
  130. return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement