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
}