An error is occurring, but where?

J-M-L, This test is the first thing on my agenda tomorrow. After that, if no success, or even if It is successful, I'll try Idahowalker suggestion. I don't understand how his change fixed his issue, but there must be some significance to the change in the way the data is received. I have not tinkered in this area before, so it should be fun to see the outcome. I'll report back tomorrow.

Hi,
What is the load that you are measuring the current to and from?

Can you draw a simple diagram showing your project layout?

What is the distance from the 2025 and the shunts?
Is the wiring to the shunts twisted pair or at least away from other cables, including the heavy current cables?
Your shunt connection leads could be picking up noise from the other wiring.

Is the connection of G1 and G2 together at the shunt?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Thank you TomGeorge, I really do appreciate your looking into this topic enough to to dig this sheet that you have pasted in here. The distance of the shunt is about 12" away from the Battery Monitor, and the leads are a twisted pair. After implementing, J-M-LJackson's suggestion to try altering my code in 2 subtle ways: First modify the Serial.begin to:

Serial.begin(115200);

2nd by modifying the Serial.available. The weird thing is, I didn't modify it, I just added to it. I don't think that they are conflicting. In my original code I have:

while(!Serial1.available()){}

and a few lines later, I have:

char a = Serial1.read();

What I did to perform the test is to insert, between the two lines above:

if (Serial1.available()) {
    byte r = Serial1.read();
    Serial.write(r);
    if (r == '%') Serial.println();
  }

The affect of adding the above coding made it so that only the Battery Monitor Data is printed to the Serial Monitor. The results show that the Battery Monitor is sending out clean Battery Current, 'A' values, as shown below:

=094,W=24.7,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=01.9,FA=02.1,PW=2FF,AH=-22.5,%
=094,W=24.8,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=01.9,FA=02.1,PW=2FF,AH=-22.5,%
=094,W=24.8,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=01.9,FA=02.1,PW=2FF,AH=-22.5,%
=094,W=24.8,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=01.9,FA=02.1,PW=2FF,AH=-22.5,%
=094,W=24.8,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=01.9,FA=02.1,PW=2FF,AH=-22.5,%
=094,W=24.8,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=02.0,FA=02.0,PW=2FF,AH=-22.5,%
=094,W=25.0,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=02.0,FA=02.0,PW=2FF,AH=-22.5,%
=094,W=25.0,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=02.0,FA=02.0,PW=2FF,AH=-22.5,%
=094,W=25.0,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=02.0,FA=02.0,PW=2FF,AH=-22.5,%
=094,W=25.0,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=02.0,FA=02.0,PW=2FF,AH=-22.5,%
=094,W=24.8,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=01.9,FA=02.0,PW=2FF,AH=-22.5,%
=094,W=24.8,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=02.0,FA=02.0,PW=2FF,AH=-22.5,%
=094,W=25.0,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=01.9,FA=02.0,PW=2FF,AH=-22.5,%
=094,W=25.0,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=02.0,FA=02.0,PW=2FF,AH=-22.5,%
=094,W=25.0,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=02.0,FA=02.0,PW=2FF,AH=-22.5,%

So I am convinced now that it's how the way that I'm reading the data. I don't understand the significance of the difference between how I have received this data in my sketch before and how this subtle change clears it up. I'd really appreciate any pointers on this matter.
Now to implement

this is blocking your sketch until something arrives

while(!Serial1.available()){}

this will print the data as soon as it arrives

if (Serial1.available()) {
    byte r = Serial1.read();
    Serial.write(r);
    if (r == '%') Serial.println();
  }

and remove it from the Serial& buffer of course

is the full code in #6 ?

Yes, J-M-LJackson, it is. Can you suggest a better way for me to implement the coding change that you suggested?

The code has lots of blocking parts, this is usually not good for Serial ports management so I would suggest to rewrite the code in a non blocking manner.

Just to be clear on what you actually get from the battery monitor, could you run this code (and only that)

void setup() {
  Serial.begin(115200);   //Serial is the USB cable Serial Monitor connection to computer
  Serial1.begin(2400);    //Serial1 is pin 18 (Tx) & 19 (Rx) from the battery monitor
}

void loop() {
  if (Serial1.available()) {
    if (r == '%') Serial.println();
    byte r = Serial1.read();
    Serial.write(r);
  }
}

and give me the first 10 lines you see

also I noticed PW=2FF, is that hexadecimal whereas everything else seems just floating point values?

here is another code to test

#define batteryMonitorSerial Serial1

// sample input %=094,W=24.7,DSC=0.96,DSE=217,PW=2FF,V=12.8,FV=12.8,V2=00.0,A=01.9,FA=02.1,PW=2FF,AH=-22.5,

const char* labels[] = {"%=", "W=", "DSC=", "DSE=", "PW=", "V=", "FV=", "V2=", "A=", "FA=", "PW=", "AH="};
const byte labelsCount = sizeof labels / sizeof labels[0];

const uint8_t maxMessageSize = 20;
char batteryMonitorMessage[maxMessageSize + 1];

boolean gotBatteryMessage(const char endMarker = ',') {
  static uint8_t index = 0;
  boolean messageComplete = false;
  int r = batteryMonitorSerial.read();
  if (r != -1) {
    if (!isspace(r)) { // don't take spaces into account
      if (r == endMarker) {
        batteryMonitorMessage[index] = '\0';
        index = 0;
        messageComplete = true;
      }
      else if (r != '\r') {
        batteryMonitorMessage[index++] = (char) r;
        batteryMonitorMessage[index] = '\0';
        if (index >= maxMessageSize) {
          Serial.println("Dropping");
          index = maxMessageSize - 1;
        }
      }
    }
  }
  return messageComplete;
}

void handleBatteryMonitor() {
  bool foundLabel = false;
  byte labelIndex = 0;
  double labelValueF = 0;
  unsigned long labelValueH = 0;
  if (gotBatteryMessage()) { // message is in batteryMonitorMessage
    Serial.print(F("Parsing ")); Serial.println(batteryMonitorMessage);

    for (byte i = 0; i < labelsCount; i++) {
      if (!strncmp(batteryMonitorMessage, labels[i], strlen(labels[i]))) {
        foundLabel = true;
        labelIndex = i;
        break;
      }
    } // end for

    if (foundLabel) {
      char * endPtr = nullptr;
      Serial.print(F("Found label: ")); Serial.println(labels[labelIndex]);
      if (strcmp(labels[labelIndex], "PW=")) {
        // we have a float value for this input
        labelValueF = strtod(&(batteryMonitorMessage[strlen(labels[labelIndex])]), &endPtr);
        if (*endPtr == '\0') {
          Serial.print(F("Value: ")); Serial.println(labelValueF); Serial.println();
        } else {
          Serial.print(F("could not parse Value: ")); Serial.println(&(batteryMonitorMessage[strlen(labels[labelIndex])]));
        }
      } else {
        // we have an HEX value for this input
        labelValueH = strtol(&(batteryMonitorMessage[strlen(labels[labelIndex])]), &endPtr, 16);
        if (*endPtr == '\0') {
          Serial.print(F("Value: 0x")); Serial.println(labelValueH, HEX); Serial.println();
        } else {
          Serial.print(F("could not parse Hex Value: ")); Serial.println(&(batteryMonitorMessage[strlen(labels[labelIndex])]));
        }
      }
    } else Serial.println(F("No label identified"));
  }
}

void setup() {
  Serial.begin(115200);           //  Serial is the USB cable Serial Monitor connection to computer
  batteryMonitorSerial.begin(2400);
}

void loop() {
  handleBatteryMonitor();
}

I would be curious to see if this works to extract the values and labels correctly

Thanks, J-M-LJackson. As for the 1st piece of code, I'm getting an error while compiling:
"exit status 1
'r' was not declared in this scope"
I edited it like this:

void setup() {
  Serial.begin(115200);   //Serial is the USB cable Serial Monitor connection to computer
  Serial1.begin(2400);    //Serial1 is pin 18 (Tx) & 19 (Rx) from the battery monitor
}
byte r;
void loop() {
  if (Serial1.available()) {
    if (r == '%') Serial.println();
    byte r = Serial1.read();
    Serial.write(r);
  }
}

It compiled. I'll give it a go.

There is no new line or carriage return, so all data continues all on the same line.

DSE=217,PW=2FF,,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.5,PW=2FF,AH=-23.7,%=094,W=39.4,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.5,PW=2FF,AH=-23.7,%=094,W=39.4,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.5,PW=2FF,AH=-23.7,%=094,W=39.7,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.5,PW=2FF,AH=-23.7,%=094,W=39.8,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.4,PW=2FF,AH=-23.7,%=094,W=39.8,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.4,PW=2FF,AH=-23.7,%=094,W=39.8,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.4,PW=2FF,AH=-23.7,%=094,W=39.8,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.4,PW=2FF,AH=-23.7,%=094,W=39.8,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.3,PW=2FF,AH=-23.7,%=094,W=39.7,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.3,PW=2FF,AH=-23.6,%=094,W=39.7,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.3,PW=2FF,AH=-23.6,%=094,W=39.7,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.3,PW=2FF,AH=-23.6,%=094,W=39.6,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.2,PW=2FF,AH=-23.6,%=094,W=39.7,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.2,PW=2FF,AH=-23.6,%=094,W=39.6,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.2,PW=2FF,AH=-23.6,%=094,W=39.4,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.2,PW=2FF,AH=-23.6,%=094,W=39.6,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.2,PW=2FF,AH=-23.6,%=094,W=39.4,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.1,PW=2FF,AH=-23.6,%=094,W=39.6,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.1,PW=2FF,AH=-23.6,%=094,W=39.4,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.1,PW=2FF,AH=-23.6,%=094,W=39.4,DSC=1.10,DSE=217,PW=2FF,X=28A,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.1,PW=2FF,AH=-23.6,%=094,W=39.4,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.0,PW=2FF,AH=-23.6,%=094,W=39.4,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.0,PW=2FF,AH=-23.6,%=094,W=39.6,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=-00.0,PW=2FF,AH=-23.6,%=094,W=39.4,DSC=1.10,DSE=217,PW=2FF,V=12.8,FV=12.7,V2=00.0,A=03.1,FA=00.0,PW=2FF,AH=-23.6,%=094,W=39.3,DSC=1.10,DSE=217,PW=2FF,V=

All of the above data was printed to a single line. Your loop does include

Serial.println();

So I'm not sure why it all printed to a single line.

Thanks J-M-LJackson, These are some results that the Serial Monitor spit out while running your 2nd suggested code sampling. You may have noticed, and you did elude to it in post #26 about something you noticed:

also I noticed PW=2FF , is that hexadecimal whereas everything else seems just floating point values?

I have always known that it was there, but wasn't interested enough nor curious enough about it, to ever do anything with it. So I just throw it out.

The results of the of the 2nd piece of code recommended:

Parsing 
No label identified
Parsing W=65.0
Found label: W=
Value: 65.00

Parsing 094
No label identified
Parsing W=64.9
Found label: W=
Value: 64.90

Parsing DSC=1.11
Found label: DSC=
Value: 1.11

Parsing DSE=217
Found label: DSE=
Value: 217.00

Parsing PW=2FF
Found label: PW=
Value: 0x2FF

Parsing V=12.9
Found label: V=
Value: 12.90

Parsing FV=12.9
Found label: FV=
Value: 12.90

Parsing V2=00.0
Found label: V2=
Value: 0.00

Parsing A=05.0
Found label: A=
Value: 5.00

Parsing FA=04.3
Found label: FA=
Value: 4.30

Parsing PW=2FF
Found label: PW=
Value: 0x2FF

Parsing AH=-22.8
Found label: AH=
Value: -22.80

Parsing %=094
Found label: %=
Value: 94.00

Parsing W=65.0
Found label: W=
Value: 65.00

Parsing DSC=1.11
Found label: DSC=
Value: 1.11

Parsing DSE=217
Found label: DSE=
Value: 217.00

Parsing PW=2FF
Found label: PW=
Value: 0x2FF

Parsing V=12.9
Found label: V=
Value: 12.90

Parsing FV=12.9
Found label: FV=
Value: 12.90

Parsing V2=00.0
Found label: V2=
Value: 0.00


Just a few of the lines.

Ok so seems you can parse as data comes in and it seems to work. You could use this approach and maintain variables and save in your file when you detect the % variable

(The other code was printing before reading the byte)

Thanks, so much for your help.

In this new format, I not sure how to mark this issue solved.

J-M-L Jackson, I have been studying the piece of code that you sent me in your post #27. I’m still trying to learn everything I can about coding in the Arduino ide. You have this line, near the top:

const char* labels[] = {"%=", "W=", "DSC=", "DSE=", "PW=", "V=", "FV=", "V2=", "A=", "FA=", "PW=", "AH="};

I understand that the * means there is a pointer involved, but when I tried to do a little research on its us>e, I found examples like:

Const char *somevariable

So the question is, Does the position of the asterisk matter. Is there a difference between its position, being located after char, like you have or just before the variable name. Does this mean that every element in the labels array are now pointers.

If you could help me understand or point me in the direction read further about pointer and their use. Especially how you have used it here. Thank you so much.

you read types from right to left

const char* labels[] is an array ([ ]) named labels of pointers (*) to characters (char) that are constant (const)

so labels[x]is a const char* (a cString)

labels[0]is the pointer in memory to the place the compiler stored "%="

Thank you JML, that was very helpful.