Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Serial Event example
- When new serial data arrives, this sketch adds it to a String.
- When a newline is received, the loop prints the string and clears it.
- A good test for this is to try it with a GPS receiver that sends out
- NMEA 0183 sentences.
- NOTE: The serialEvent() feature is not available on the Leonardo, Micro, or
- other ATmega32U4 based boards.
- created 9 May 2011
- by Tom Igoe
- This example code is in the public domain.
- http://www.arduino.cc/en/Tutorial/SerialEvent
- */
- String inputString = ""; // a String to hold incoming data
- bool stringComplete = false; // whether the string is complete
- // Example 2 - Receive with an end-marker
- const byte numChars = 255;
- char receivedChars[numChars]; // an array to store the received data
- int motors[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- boolean newData = false;
- boolean ledState = false;
- void setup() {
- Serial.begin(115200);
- Serial.println("<Arduino is ready>");
- pinMode(13, OUTPUT);
- }
- void loop() {
- recvWithEndMarker();
- showNewData();
- if (newData) {
- for (int i = 0; i < 18; i++) {
- Serial.print(motors[i]);
- Serial.print(".");
- }
- Serial.println();
- newData = false;
- }
- }
- void recvWithEndMarker() {
- static byte ndx = 0;
- char endMarker = '\n';
- char rc;
- while (Serial.available() > 0 && newData == false) {
- rc = Serial.read();
- if (rc != endMarker) {
- receivedChars[ndx] = rc;
- ndx++;
- if (ndx >= numChars) {
- ndx = numChars - 1;
- }
- }
- else {
- receivedChars[ndx] = '\0'; // terminate the string
- ndx = 0;
- newData = true;
- }
- }
- }
- void showNewData() {
- if (newData == true) {
- Serial.println("This just in ... ");
- String str(receivedChars);
- String motor0 = getValue(str, ';', 0);
- String motor1 = getValue(str, ';', 1);
- String motor2 = getValue(str, ';', 2);
- String motor3 = getValue(str, ';', 3);
- String motor4 = getValue(str, ';', 4);
- String motor5 = getValue(str, ';', 5);
- for (int i = 0; i < 3; i++) {
- motors[i] = getValue(motor0, ':', i).toInt();
- }
- for (int i = 0; i < 3; i++) {
- motors[i+3] = getValue(motor1, ':', i).toInt();
- }
- for (int i = 0; i < 3; i++) {
- motors[i+6] = getValue(motor2, ':', i).toInt();
- }
- for (int i = 0; i < 3; i++) {
- motors[i+9] = getValue(motor3, ':', i).toInt();
- }
- for (int i = 0; i < 3; i++) {
- motors[i+12] = getValue(motor4, ':', i).toInt();
- }
- for (int i = 0; i < 3; i++) {
- motors[i+15] = getValue(motor5, ':', i).toInt();
- }
- }
- }
- String getValue(String data, char separator, int index)
- {
- int found = 0;
- int strIndex[] = {0, -1};
- int maxIndex = data.length()-1;
- for(int i=0; i<=maxIndex && found<=index; i++){
- if(data.charAt(i)==separator || i==maxIndex){
- found++;
- strIndex[0] = strIndex[1]+1;
- strIndex[1] = (i == maxIndex) ? i+1 : i;
- }
- }
- return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement