Programming Question with a Color SmartGPU O'LED display

Good morning!
I'm working on an O2 analyzer. I've written some code which I've included below.
The code writes a floating point voltage to the SmartGPU screen and seems to work well with one exception, the "lcd.string" output of "Sensor 1,2, & 3" that displays on the screen does not update correctly. The only way to update the output is to reset the SmartGPU OR I have also found that including the following command "lcd.imageSD(0,0,"main7");" inside the "void loop()" function will update the output but it continues to flash repeatedly, can the code be changed to update Sensors 1,2 & 3 without resetting or using the "lcd.imageSD(0,0,"main7")" command?

I've pasted my code below, any help you can provide would be great!
Thank you so much!
Dave

     #include <SMARTGPU.h> 
     #define MULTIPLIER 100 
     #define aref_voltage 3.0

  SMARTGPU lcd;//create LCD object  
     

//****************************O2 SENSOR INPUTS****************************     
      const int sensor1  = A0;  // the analog pin 02 Sensor#1 Vout pin is connected to
            int sensorValue1;   //declare a variable 
    
      const int sensor2  = A1;  // the analog pin 02 Sensor#2 Vout pin is connected to  
            int sensorValue2;   // declare a variable   
               
      const int sensor3  = A2;  // the analog pin 02 sensor#3 Vout pin is connected to
            int sensorValue3;   // declare a variable
            
        
            
void Display(){
  
//***************************O2 Sensor 1*********************************//      
      sensorValue1 = analogRead(sensor1);
      float VoltageSensor1 = sensorValue1*aref_voltage;
      VoltageSensor1 /= 1024.0;
      long lWhole1 = 0;
      unsigned long ulPart1 = 0;
      lWhole1=(long)((float)VoltageSensor1);// whole part of the number to the left of the decimal point
      ulPart1=(long)((float)VoltageSensor1*MULTIPLIER)-lWhole1*MULTIPLIER;// fractional part to the right of the decimal point

      char PPO1[1]= "";
      sprintf(PPO1, "%li.%02li", lWhole1,ulPart1);
      lcd.string(9,70,100,50,GREEN,FONT7,TRANS,PPO1);
      
//**************************O2 Sensor 2**********************************//    
      sensorValue2 = analogRead(sensor2);
      float VoltageSensor2 = sensorValue2*aref_voltage;
      VoltageSensor2 /= 1024.0;
      long lWhole2 = 0;
      unsigned long ulPart2 = 0;
      lWhole2=(long)((float)VoltageSensor2);// whole part of the number to the left of the decimal point
      ulPart2=(long)((float)VoltageSensor2*MULTIPLIER)-lWhole2*MULTIPLIER;// fractional part to the right of the decimal point

      char PPO2[2]= "";
      sprintf(PPO2, "%li.%02li", lWhole2,ulPart2);
      lcd.string(123,70,255,65,GREEN,FONT7,TRANS,PPO2);
      
//***************************O2 Sensor 3*********************************//    
      sensorValue3 = analogRead(sensor3);
      float VoltageSensor3 = sensorValue3*aref_voltage;
      VoltageSensor3 /= 1024.0;
      long lWhole3 = 0;
      unsigned long ulPart3 = 0;
      lWhole3=(long)((float)VoltageSensor3);// whole part of the number to left of decimal point
      ulPart3=(long)((float)VoltageSensor3*MULTIPLIER)-lWhole3*MULTIPLIER;// fractional part to the right of decimal point

      char PPO3[3]= "";
      sprintf(PPO3, "%li.%02li", lWhole3,ulPart3);
      lcd.string(238,70,320,65,GREEN,FONT7,TRANS,PPO3);
      

  }
            

void setup(){

      lcd.init();  //configure serial and pinout on arduino for SMARTGPU 
      lcd.start(); //initialize SMARTGPU processor  
      analogReference(EXTERNAL); // set external analog Ref for PPO output       

  }  

void loop(){
       
        lcd.imageSD(0,0,"main7");
        Display();      
  }

How many bytes have you allocated to the print buffers PPO1/2/3 ?

I see 1 byte for PPO1, 2 for PPO2 and 3 for PPO3, unless I am mistaken.

      char PPO3[3]= "";
      sprintf(PPO3, "%li.%02li", lWhole3,ulPart3);
      lcd.string(238,70,320,65,GREEN,FONT7,TRANS,PPO3);

The decimal point takes one place in the array. The %02li format specifier will take two places in the array. There is no room for the %li part or the terminating NULL. Your arrays are too small. And, this is the biggest of them.

PaulS:

      char PPO3[3]= "";

sprintf(PPO3, "%li.%02li", lWhole3,ulPart3);
      lcd.string(238,70,320,65,GREEN,FONT7,TRANS,PPO3);



The decimal point takes one place in the array. The %02li format specifier will take two places in the array. There is no room for the %li part or the terminating NULL. Your arrays are too small. And, this is the biggest of them.

Hi Paul,
Thanx for your response, I'm not sure what your suggesting I try. I can send you a video of the circuit in action it works fine and the string prints fine to the screen with the correct whole and decimal number. Is there any chance you can clarify your response or what you suggest I try in order to update the output without using "lcd.imageSD(0,0,"main7");" in the loop......?

UKHeliBob:
How many bytes have you allocated to the print buffers PPO1/2/3 ?

I see 1 byte for PPO1, 2 for PPO2 and 3 for PPO3, unless I am mistaken.

Hi Bob,
I'm not sure, could this be my problem. I do know the output prints fine to the screen its just when I change the input voltage on sensor 1,2 or 3 the updated voltage just over wrights on top of the previous value and this continues until I reset the screen them the same process repeats itself over again.

When one posts code that has fundamental flaws, those flaws need to be fixed, first. Then, if there is still a problem, we can address that problem.

The value in the square brackets defines how many elements the array has. You are trying to write some characters into that array. You need to determine the maximum number of characters that will be written before the decimal point and after the decimal point. Add two more for the decimal point and the terminating NULL. That is how big the three arrays need to be. Actually, you only need one array. Reuse it, and save space.

That said, some of your other code is questionable. Which Arduino do you have that is running at 3.0 volts? Arduinos run at either 5.0 or 3.3 volts.

The maximum value that VoltageSensor1 will contain is just over 3300 (or 5000). When you truncate that as an integer value, that will easily fit in an int. There is no reason to use a long to hold the value (or the l in the format specifier).

VoltageSensor1 is a float. Casting it to a float is completely unnecessary (I almost typed stupid, but I stopped in time). Casting it to a long before storing it is not necessary, either.

Since the output from the analogRead() function is an int, ranging from 0 to 1023, there is no way that VoltageSensor1 can be negative. After stripping off the whole portion, there is no way for the fraction to become negative, so using unsigned variables to hold the fraction is not necessary, either.

PaulS:
When one posts code that has fundamental flaws, those flaws need to be fixed, first. Then, if there is still a problem, we can address that problem.

The value in the square brackets defines how many elements the array has. You are trying to write some characters into that array. You need to determine the maximum number of characters that will be written before the decimal point and after the decimal point. Add two more for the decimal point and the terminating NULL. That is how big the three arrays need to be. Actually, you only need one array. Reuse it, and save space.

That said, some of your other code is questionable. Which Arduino do you have that is running at 3.0 volts? Arduinos run at either 5.0 or 3.3 volts.

The maximum value that VoltageSensor1 will contain is just over 3300 (or 5000). When you truncate that as an integer value, that will easily fit in an int. There is no reason to use a long to hold the value (or the l in the format specifier).

VoltageSensor1 is a float. Casting it to a float is completely unnecessary (I almost typed stupid, but I stopped in time). Casting it to a long before storing it is not necessary, either.

Since the output from the analogRead() function is an int, ranging from 0 to 1023, there is no way that VoltageSensor1 can be negative. After stripping off the whole portion, there is no way for the fraction to become negative, so using unsigned variables to hold the fraction is not necessary, either.

I suspected there might be some issues with my code, thats why I posted an inquiry with my code. I did some research and found some O2 custom made computers use a float voltage to represent the whole and decimal number of the voltage output, so I used this as a means of representing the output which seems to work great with my older Alphanumeric LCD, I only ran into this trouble when I tried to rewrite the code to replace the LCD with a color O'LED. I used a aref_voltage input of 3.0 volts because the sensors I'm using output in milli volts, this is then amplified with a circuit and sent to my analog inputs where its converted from 0-1023 to an actual voltage of 0-1.75 which represents the partial pressure of O2 with respect to your current depth.

Thank you again for clarifying the array and how they function. I'm excited to try and make those changes and see how they work!
Dave

concretefreak:

PaulS:
When one posts code that has fundamental flaws, those flaws need to be fixed, first. Then, if there is still a problem, we can address that problem.

The value in the square brackets defines how many elements the array has. You are trying to write some characters into that array. You need to determine the maximum number of characters that will be written before the decimal point and after the decimal point. Add two more for the decimal point and the terminating NULL. That is how big the three arrays need to be. Actually, you only need one array. Reuse it, and save space.

That said, some of your other code is questionable. Which Arduino do you have that is running at 3.0 volts? Arduinos run at either 5.0 or 3.3 volts.

The maximum value that VoltageSensor1 will contain is just over 3300 (or 5000). When you truncate that as an integer value, that will easily fit in an int. There is no reason to use a long to hold the value (or the l in the format specifier).

VoltageSensor1 is a float. Casting it to a float is completely unnecessary (I almost typed stupid, but I stopped in time). Casting it to a long before storing it is not necessary, either.

Since the output from the analogRead() function is an int, ranging from 0 to 1023, there is no way that VoltageSensor1 can be negative. After stripping off the whole portion, there is no way for the fraction to become negative, so using unsigned variables to hold the fraction is not necessary, either.

I suspected there might be some issues with my code, thats why I posted an inquiry with my code. I did some research and found some O2 custom made computers use a float voltage to represent the whole and decimal number of the voltage output, so I used this as a means of representing the output which seems to work great with my older Alphanumeric LCD, I only ran into this trouble when I tried to rewrite the code to replace the LCD with a color O'LED. I used a aref_voltage input of 3.0 volts because the sensors I'm using output in milli volts, this is then amplified with a circuit and sent to my analog inputs where its converted from 0-1023 to an actual voltage of 0-1.75 which represents the partial pressure of O2 with respect to your current depth.

Thank you again for clarifying the array and how they function. I'm excited to try and make those changes and see how they work!
Dave

When I have time I'll post some pics and a video in operation.

Start by declaring the 3 arrays as globals with enough elements to hold the strings as Paul has explained. Within reason a larger array than necessary is OK. Then move on to fix the other issues with data types.

PaulS:
When one posts code that has fundamental flaws, those flaws need to be fixed, first. Then, if there is still a problem, we can address that problem.

The value in the square brackets defines how many elements the array has. You are trying to write some characters into that array. You need to determine the maximum number of characters that will be written before the decimal point and after the decimal point. Add two more for the decimal point and the terminating NULL. That is how big the three arrays need to be. Actually, you only need one array. Reuse it, and save space.

That said, some of your other code is questionable. Which Arduino do you have that is running at 3.0 volts? Arduinos run at either 5.0 or 3.3 volts.

The maximum value that VoltageSensor1 will contain is just over 3300 (or 5000). When you truncate that as an integer value, that will easily fit in an int. There is no reason to use a long to hold the value (or the l in the format specifier).

VoltageSensor1 is a float. Casting it to a float is completely unnecessary (I almost typed stupid, but I stopped in time). Casting it to a long before storing it is not necessary, either.

Since the output from the analogRead() function is an int, ranging from 0 to 1023, there is no way that VoltageSensor1 can be negative. After stripping off the whole portion, there is no way for the fraction to become negative, so using unsigned variables to hold the fraction is not necessary, either.

Hi Paul,
Thank you for your input, and I appreciate your recommendations but it appears my code works perfectly except for a very small change in the Loop. I experimented a little with delays and changing the "TRANS" to "FILL" in order to prevent re-write distortion. I was shocked to see the whole system working great!! When I have a chance I'll post some pics and a video.

Thank you again!

UKHeliBob:
Start by declaring the 3 arrays as globals with enough elements to hold the strings as Paul has explained. Within reason a larger array than necessary is OK. Then move on to fix the other issues with data types.

Hi Bob,
Thank you for your input, and I appreciate your recommendations but it appears my code works perfectly except for a very small change in the Loop. I experimented a little with delays and changing the "TRANS" to "FILL" in order to prevent re-write distortion. I was shocked to see the whole system working great!! When I have a chance I'll post some pics and a video.

Thank you again!

I am glad that your code runs, but if you have not allocated more space for the print buffers then I can't help feeling that you are heading for a fall at some time as you don't know what is being overwritten when you put more data into an array than its declared size.

UKHeliBob:
I am glad that your code runs, but if you have not allocated more space for the print buffers then I can't help feeling that you are heading for a fall at some time as you don't know what is being overwritten when you put more data into an array than its declared size.

Ah, yes i see....thank you again! I'll make sure to follow your advice and allocate more space. I'll be sure to send you a video soon.

Thank you!!