GiGA shield analog display updates

Hello :slight_smile:

I have an Arduino GIGA with the GIGA display shield to capture an analog voltage of a capacitor in a bank of capacitors for an electromagnetic accelerator which is working perfectly. The issue I am having is I want the display on my GIGA Display shield to always show the voltage of the capacitor without having to blank out and redisplay the results every 3 seconds as per my delay. I would just like to have the voltage constantly updating in real time on the Giga Display shield for each capacitor bank. I have the code written for just one capacitor to test things out as shown by my provided code. Programming is not my expertise unfortunately.

Also there may be a bug in my code as it is written as after 40 to 50 refreshes of the GIGA dimply shield to show the voltage, the screen goes blank and the LED flashes until I reset the GIGA R1.

Here is my code for you to evaluate :slight_smile:



// 460 Volt measuring program

#include "Arduino_GigaDisplay_GFX.h"
GigaDisplay_GFX display;  // create the object
#define BLUE 0x00ff



const float arduinoVCC = 3.3;  //Your Arduino voltage
unsigned long ValueR1 = 3000;
unsigned long ValueR2 = 449000;
double Voltage_Source = 450;
const int alanogPin = A0;          //the pin connecting the voltage.
const int inputResolution = 1023;  //works with most Arduino boards
const float average_of = 500;      //Average of 500 readings

float voltage;
void setup() {
  Serial.begin(115200);
  display.setRotation(1);  //Set GIGA Display orientation to landscape mode (Values are 0 - 3)
  //delay(500);
}

void loop() {

  readVoltage();


  Serial.print("Voltage of Cap 1: ");
  //Serial.print(voltage);
  //Serial.print("V Avg: ");
  Serial.print(getVoltageAverage());
  Serial.println(" Volts");

  //-------------------------------------------------

  display.begin();  //init library for GIGA Display
  display.fillScreen(BLUE);
  display.setCursor(10, 10);           //x,y
  display.setTextSize(4);              //adjust text size
  display.print("Capacitor 1 is ");    //print
  display.print(getVoltageAverage());  //print
  display.print(" volts");             //print

  //------------------------------------------------

  delay(3000);  // delay for displaying voltage reading to make it easier to read

}  //loop end


void readVoltage() {


  int A0Value = analogRead(alanogPin);
  float voltage_sensed = A0Value * (arduinoVCC / (float)inputResolution);
  //  Serial.print("voltage_sensed:");
  //  Serial.print(voltage_sensed);
  voltage = voltage_sensed * (1 + ((float)ValueR2 / (float)ValueR1));

}  //readVoltage()


float getVoltageAverage() {

  float voltage_temp_average = 0;
  for (int i = 0; i < average_of; i++) {
    readVoltage();
    voltage_temp_average += voltage;
  }

  return voltage_temp_average / average_of;



}  //getVoltageAverage

Hi @adirks

Use the technique that is described here:

https://learn.adafruit.com/adafruit-gfx-graphics-library/minimizing-redraw-flicker#overwriting-text-with-the-built-in-font-3132149

The "Arduino_GigaDisplay_GFX" library you are using is based on the "Adafruit GFX Library" of that tutorial, so the information from that tutorial can be used with the "Arduino_GigaDisplay_GFX" library. Give it a try and if you get stuck or have problems, post your updated code here and we'll take a look.

It is caused by calling display.begin() repeatedly:

Move that line to your setup function so that it is only called once. After that, the sketch should no longer crash the GIGA R1 WiFi board.

1 Like

Sorry, I noticed this posting a day or so ago and meant to respond.

Nice writeup, was going to suggest several of the techniques mentioned in it.

If the format of the screen is with fixed data such as this, you can potentially move a
a lot of the static data to either setup or some function called from setup,
like all of the headers.

Note: anytime you call something like fillScreen and then redraw stuff you are almost
guaranteed to have flashing. Except if you do it all off screen using canvas and simply update the screen from the canvas.

But maybe a quick and dirty way to not see this flashing, is to move these lines out
of loop and into setup and setup to use Opaque text output. That is
where it will fill in the pixels with the background text color,

  display.begin();  //init library for GIGA Display
  display.fillScreen(BLUE);
  display.setTextColor(0xffff, BLUE); // assuming default white(0xff) text?  

Then in loop, you can do the rest... like

  display.setCursor(10, 10);           //x,y
  display.setTextSize(4);              //adjust text size
  display.print("Capacitor 1 is ");    //print
  display.print(getVoltageAverage());  //print
  display.print(" volts    ");             //print

Note: I put in 4 blanks after volts, so if number is shorter than previous, hopefully
it will erase the rest of the data.

There are other tricks as well, like you
could do a fillRect after the end of the displaying of your text to either the end of
the previous text or to the end of the line...

You can also make the text maybe more deterministic in size, for example if you, do
display.print(getVoltageAverage(), 2);
It will print out the floating point with 2 decimal points... like form the Serial.print Arduino reference page:

  • Serial.print(1.23456, 2) gives "1.23"

As I mentioned, there are additional things you can try, but hopefully this helps some.

thank you for your very helpful information. The GIGA display does not crash anymore :slight_smile: Thank you for taking the time to reply to my post as programming is not my strongest skill but it is getting better with your support!

Best Regards

Alex

1 Like

The solution you provided of putting those 3 lines of code in the setup portion of my code worked perfectly the first time out! Now I get a consistent updated GIGA display shield of the capacitor bank voltage without the screen flashing and the voltage updating in real time! I learn something each time you help me with the code for this project and I very much appreciate that you take the time to provide actual code suggestions to help fix my issues. I learn a lot from that!

Best Regards,

Alex

1 Like

Hey yall,

I am a college student and I really have no clue how to fix this!

I have a Giga Display Touch Screen Sheild and I am trying to get the "load" measured by a load cell to display on our main screen. I have gotten it to update the entire screen before but it was too slow; chatGTP has suggested a I make an "updatescreen" function to only replace that specific section of the screen but I cant figure out how to make it work. I have tried to follow that Adafruit link but I think I'm lost haha.

The version of the code I am pasting only shows the block that the reading is updating and deletes the rest of the IU. Do y'all think we need to restart the code? (keep in mind the block should only update on the "main screen" not the "temp screen"

#include "Arduino_H7_Video.h"           // Include the library for controlling the display
#include "ArduinoGraphics.h"            // Include the graphics library for drawing on the display
#include "Arduino_GigaDisplayTouch.h"   // Include the touchscreen library
#include "HX711.h"                      // Include the HX711 library for load cell reading

// Define HX711 pins
#define DOUT 4
#define CLK 5

// Initialize display with 800x480 resolution and GigaDisplayShield
Arduino_H7_Video Display(800, 480, GigaDisplayShield); 
// Initialize touch screen on Wire1 (I2C communication)
Arduino_GigaDisplayTouch Touch(Wire1);

// Initialize HX711 for load cell
HX711 scale;
float loadValue = 0.0;
const float calibrationFactor = -18397.84;

// Global variable to track screen state (whether we are on the main screen or temperature screen)
bool newScreenActive = false;

// Function prototypes for drawing screens
void drawScreen();
void drawTemperatureScreen();

void setup() {
    // Initialize serial communication for debugging
    Serial.begin(9600);
    delay(3000);  // Delay to ensure proper initialization

    // Debug messages
    Serial.println("Initializing Display...");
    Display.begin();  // Initialize the display
    Touch.begin();    // Initialize the touchscreen
    Serial.println("Display and Touch Initialized");

    // Initialize HX711
    Serial.println("Initializing HX711...");
    scale.begin(DOUT, CLK);  // Initialize HX711 with defined pins
    scale.set_scale(calibrationFactor);  // Set calibration factor for your load cell
    scale.tare(); // Tare (zero) the scale to remove any initial weight
    Serial.println("HX711 Initialized");

    // Draw the main UI layout
    drawScreen(); 
}

unsigned long lastUpdateTime = 0;  
const unsigned long updateInterval = 100;  

void loop() {
    if (scale.is_ready()) {
        loadValue = scale.get_units();  // Read weight

        if (millis() - lastUpdateTime >= updateInterval) {
            lastUpdateTime = millis();  // Reset timer
            updateLoadDisplay(loadValue);  // ONLY update weight text
        }

        Serial.print("Weight: ");
        Serial.print(loadValue);
        Serial.println(" g");
    }

    uint8_t contacts;
    GDTpoint_t points[5];

    contacts = Touch.getTouchPoints(points); // Check for touch input
    if (contacts > 0) {
        for (uint8_t i = 0; i < contacts; i++) {
            int x = points[i].y;
            int y = points[i].x;

            Serial.print("Touch Detected at X: ");
            Serial.print(x);
            Serial.print(", Y: ");
            Serial.println(y);

            if (newScreenActive) { 
                // Returning to Main Screen
                if (x > 750 && x < 800 && y > 430 && y < 480) {
                    Serial.println("Returning to Main Screen");
                    newScreenActive = false;
                    drawScreen();  // Redraw full main screen
                }
                // Heating ON/OFF buttons
                else if (x > 200 && x < 350 && y > 375 && y < 450) {
                    Serial.println("Heating pad ON");
                }
                else if (x > 450 && x < 600 && y > 375 && y < 450) {
                    Serial.println("Heating pad OFF");
                }
            } else {
                // Switch to Temperature Screen
                if (x > 0 && x < 400 && y > 240 && y < 480) {
                    Serial.println("Switching to Temperature Screen");
                    newScreenActive = true;
                    drawTemperatureScreen();
                }
                // Scissor Lift Buttons
                else if (x > 420 && x < 780 && y > 250 && y < 350) {
                    Serial.println("Raise button pressed!");
                }
                else if (x > 420 && x < 780 && y > 360 && y < 460) {
                    Serial.println("Lower button pressed!");
                }
            }
        }
    }
}

void drawScreen() {
    // Start drawing on the display
    Display.beginDraw(); 

    // Set background and stroke colors
    Display.background(255, 255, 255); // White background
    Display.fill(255, 255, 255); // Fill color (white)
    Display.stroke(0, 0, 0);  // Stroke color (black)

    int boxWidth = Display.width() / 2;
    int boxHeight = Display.height() / 2;

    // Draw the "Temperature" box (Top left)
    Display.fill(255, 255, 255);
    Display.rect(0, 0, boxWidth, boxHeight);
    Display.fill(0, 0, 0);
    Display.textSize(9);
    Display.text("Temp", 110, 30);  // Label for temperature box
    Display.textSize(7);
    Display.text("--.- C", 95, 110);  // Placeholder for temperature value
    Display.textSize(7);
    Display.text("<status>", 60, 180);  // Placeholder for temperature status

    // Draw the "Load" box (Top right)
    Display.fill(255, 255, 255);
    Display.rect(boxWidth, 0, boxWidth, boxHeight);
    Display.fill(0, 0, 0);
    Display.textSize(9);
    Display.text("Load", boxWidth + 110, 30);
    Display.textSize(8);

    // Display load cell reading
    char loadText[10];
    snprintf(loadText, sizeof(loadText), "%.2f g", loadValue);
    Display.text(loadText, boxWidth + 90, 110);  // Display actual load value

    // Draw the "E-Stim" box (Bottom left)
    Display.fill(255, 255, 255);
    Display.rect(0, boxHeight, boxWidth, boxHeight);
    Display.fill(0, 0, 0);
    Display.textSize(9);
    Display.text("E-Stim", 70, boxHeight + 30);

    // Draw the "Scissor Lift Controls" (Bottom right)
    Display.fill(255, 255, 255);
    Display.rect(boxWidth, boxHeight, boxWidth, boxHeight);
    Display.stroke(0, 0, 0);
    Display.rect(420, 250, 360, 100);  // "Raise" button
    Display.rect(420, 360, 360, 100);  // "Lower" button
    Display.textSize(7);
    Display.text("Raise", 515, 280);  // Label for Raise button
    Display.text("Lower", 515, 390);  // Label for Lower button

    Display.endDraw();  // Finish drawing the screen
}

void updateLoadDisplay(float load) {
    Display.beginDraw();
    Display.background(255, 255, 255);  // White background
    Display.fill(0, 0, 0);  // Black text
    Display.textSize(8);

    char loadText[10];
    snprintf(loadText, sizeof(loadText), "%.2f g", load);
    Display.text(loadText, Display.width()/2 + 90, 110);  // Adjust position

    Display.endDraw();
}


void drawTemperatureScreen() {
    // Start drawing on the temperature screen
    Display.beginDraw();
    Display.clear();  // Clears the screen completely
    Display.background(255, 255, 255);  // White background for the temperature screen

    // Title for the temperature screen
    Display.fill(0, 0, 0);
    Display.textSize(9);
    Display.text("Temperature", 150, 25);

    // Draw the "X" button to return to the main screen
    Display.stroke(0, 0, 0);
    Display.fill(255, 255, 255);  // White background for the button
    Display.rect(750, 0, 50, 50);  // Button dimensions

    // Draw "X" inside the button
    Display.fill(0, 0, 0);  // Black color for text
    Display.textSize(6);
    Display.text("X", 767, 5);  // Label for "X" button

    // Data Display (Heating Pad, Bath, and Status)
    Display.fill(0, 0, 0);
    Display.textSize(6);
    Display.text("Heating Pad: ", 55, 150);
    Display.text("Bath: ", 55, 225);
    Display.text("Heating Status: ", 55, 300);

    // Placeholder values (to be replaced later with actual data)
    Display.text("__._ C", 570, 150);  // Heating pad temperature
    Display.text("__._ C", 570, 225);  // Bath temperature
    Display.text("<status>", 520, 300); // Heating status

    // Buttons to control the heating pad (ON/OFF)
    Display.fill(255, 255, 255);
    Display.stroke(0, 0, 0);
    Display.rect(200, 375, 150, 75);  // "ON" button
    Display.rect(450, 375, 150, 75);  // "OFF" button
    Display.fill(0, 0, 0);
    Display.textSize(5);
    Display.text("ON", 250, 400);  // Label for ON button
    Display.text("OFF", 490, 400);  // Label for OFF button

    Display.endDraw();  // Finish drawing the temperature screen
}