Nextion progress bar

@ 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;
    }
}

Topic SPLIT.

Bob.

Hello Charles, To be clear, what you are asking is why it is that j2 does not change and what you can do to fix it? Is that correct?

        Serial2.print ("j2.val=");
        Serial2.print (AHVal);
        Serial2.print (0xff);
        Serial2.print (0xff);
        Serial2.print (0xff);

Is wrong. In order to send the 0xff use:

        Serial2.write (0xff);
        Serial2.write (0xff);
        Serial2.write (0xff);

I don't know about the map function as I've never needed it. Put some Serial.prints to check that the conversion is working.

Hi Perry.

Yes you were correct, I now have a problem…

The BMS only has an output value from -0000 for a full capacity battery and -0030 for empty.

Right now if i type in +0018 it’ll move the progress bar up or + 100 to full how do I now get these two numbers to work together, can I make a negative number a positive one? If that makes sense.

The map function I saw here.

thank you very much.

Regards
Charles

A better explanation of the MAP function.

Used it quite a bit to invert readings or scale them. Very versatile.

Bob.

Hi Perry.

Ok. I dug around and found that I could say AHVal = -AHVal; this turns my number to a positive one.

I now need to find out how to make this go from 0 which is actually 100 down to 0 which is 30, the map function is not working for this....

Any help will be welcome....

Thanks Regards Charles

ballscrewbob: A better explanation of the MAP function.

Used it quite a bit to invert readings or scale them. Very versatile.

Bob.

Hi Bob,

I am a bit dumb right now, the Map function is not working now . I've done something to upset things... Again

Can I make a negative number a positive one?

someVariable = abs(signedVariable);

Will put the value of signedVariable into someVariable without the sign, regardless of whether signedVariable is positive or negative to start with.

I now need to find out how to make this go from 0 which is actually 100 down to 0 which is 30, the map function is not working for this....

Now my hear hurts. Reverse the progress bar :confused: Or If you want 0 to 100 to become 100 to 0 then subtract the value you have from 100.

uint8_t reversedValue = 100 - originalValue;

@Charles_Gibbs

It expects four parameters normally.

But as they are integers you might need a line of code before the map function.

This would especially be the case if you are using CHARACTERS which could need converting to an INTEGER.

The MAP function would then return your numbers.

After that if you want them back to Characters you would then have to use another line of code to convert them back.

Bob.

@Bob & Perry.

Thank you both.

I now have a bit more to play with.

I have a day off tomorrow so will get stuck in to what you both have given me and I'll revert back once I have something positive...

Thanks again.

Regards Charles

Hi Perry and Bob.

I was able to get the whole thing working thank you both, I have another question.

Is it possible to have a picture flash on and off on the nextion when the arduino has a certain condition that is met?

Can something like this be done in the nextion directly with only a command from the arduino? or does the timing for the flashing picture have to come from the arduino?

If this is possible how do I go about it?

Thank you

Regards Charles

Is it possible to have a picture flash on and off on the Nextion when the Arduino has a certain condition that
is met?

Have a look at part 4 of my tutorial and download ‘Ardino Nextion cropped images’ The example changes the background of a button when it is pressed to make the button change colour as it is pressed. You should be able to adapt that to make a picture flash on and off.

Something like p0.picc=0 and p0.picc=1 will change which background image p0 uses for example.

Also read the Nextion instruction set on the ITEAD web site, vis will hide or show a component on the display.

Hi Perry,

Thank you, I'll do so.

Thanks again

Regards Charles