#include <stdio.h>

int totalSum = 0;

void setup(){
  Serial.begin(9600);
  Serial.flush();// clear receive buffer
  totalSum = 0;
}

void loop(){
  delay(2000);
  if(Serial.available() > 3){// more than 4 bytes in the receive buffer
    byte nBytes = 0;
    char cmdbuf[5] = "    ";
    char intbuf[5] = "    ";

    while( Serial.available()){// read all data from receive buffer
      if( nBytes < 4 ){// collect first 4 bytes from receive buffer
        cmdbuf[nBytes++] = Serial.read();
      }
      else if (nBytes < 8){// collect second lot of 4 bytes max
        intbuf[nBytes++ - 4] = Serial.read();
      }
    }
    cmdbuf[5] = '\0';
    intbuf[5] = '\0';

    Serial.flush();

//    int userInput = atoi(intbuf);
//    totalSum+=userInput;
    // debug - remove later
    Serial.print("Received: ");
    Serial.print(nBytes);
    Serial.print(" first 4B: ");
    Serial.print(cmdbuf);
    Serial.print(" next 4B: ");
    Serial.println(intbuf);
//    Serial.print(" total input: ");
//    Serial.println(totalSum);



  }// more than 4 bytes in rx buf
}


