@ Perry.
Hi Perry.
I have a project that I am working on.
I have used Robbin 2's code from example 3 on serial basics to come this far. I am a complete noob.
I have an EV (Electric Vehicle) on the batteries is a BMS it spits out a load of information every second or so, I used Robin2's code to extract the part I need and then convert it to an int .
This comes into the serial monitor I used an Arduino uno with the softwareserial.h library connected @9600 baud. This is a sample of what I copied from the serial monitor, all one line no breaks.
Z331,331,332,332,332,331,332,332,331,331,331,331,332,333,332,331,331,331,331,331,331,331,332,331,332,332,332,331,330,332,331,332,331,331,331,331,331,331,331,331,331,331,331,332,331,331,331,331,331,331,331,331,332,331;T-20,-20,-20,-20,-20;S-0045;E-0018;
To replicate the BMS info, I use an Arduino uno to send the data TO an Arduino mega it enters serial1 and is captured by the code of Robins the start Marker is the 'E' and the end marker is the ; the bit i use is the -0018 right at the end.
This works without a problem next it is converted to an int whereafter I get a -18 in the serial monitor of the mega.
I have a Nextion 4'3" resistive touch screen on it are a few things the one I want to change is a progress bar object name j2. It's .val works with 0 to 100. So I have tried to use the map function to try and make it so that if the number coming into the serial is a 0 the progress bar is 100 and when the serial gets -30 the progress bar will show 0........
Up till now I am able to get the value into the serial monitor but No change on the screen.
As I am a noob I will chalk it up to incompetence on my behalf.
Can you help me please?
Below is (my) code.
Regards
Charles Gibbs
// Example 3 - Receive with start- and end-markers
const byte numChars = 7;
char receivedChars[numChars];
int AHBarValue; // this variable stores the value for the battery Capacity
boolean newData = false;
int AHVal; // This stores the int value for the progress bar
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithStartEndMarkers();
showNewData();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = 'E';
char endMarker = ';';
char rc;
while (Serial1.available() > 0 && newData == false) {
rc = Serial1.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
//Serial.print("This just in ... ");
AHVal = atoi(receivedChars); //convert to int
AHBarValue = map (AHVal, 0, -30, 100, 0);
Serial.println(AHVal);
Serial2.print ("j2.val=");
Serial2.print (AHVal);
Serial2.print (0xff);
Serial2.print (0xff);
Serial2.print (0xff);
newData = false;
}
}