#include <stdio.h>
#include <stdlib.h>

int sumconvint = 0;

void setup(){
  Serial.begin(9600);
  Serial.flush();// clear receive buffer

}

void loop(){
  delay(2000);
  char cmdbuf[5];
  char intbuf[5];
  memset(cmdbuf, '\0', 5);
  memset(intbuf, '\0', 5);

  if(Serial.available() > 4){// at least 6 bytes in the receive buffer
    byte nBytes = 0;
    while( Serial.available()){// read all data from receive buffer
      if(nBytes < 4) cmdbuf[nBytes] = Serial.read();
      else if (nBytes <8) intbuf[nBytes-4] = Serial.read();
      else Serial.read();
      nBytes++;
    }
    Serial.flush();

int rxint = atoi(intbuf);
sumconvint += rxint;
    // debug - remove later
    Serial.print("Received: ");
    Serial.print(nBytes);
    Serial.print(" B. First 4B: ");
    Serial.print(cmdbuf);
    Serial.print(" next B:");
    Serial.println(intbuf);
    Serial.print("Integer: ");
    Serial.print(rxint);
   Serial.print(" total sum so far: ");
   Serial.print(sumconvint);
    Serial.println("  ");




  }// more than 4 bytes in rx buf
}







