#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_FT6206.h>
#include "HX711.h"
// Pin configuration
#define TFT_CS 15
#define TFT_RST 4
#define TFT_DC 2
#define LOADCELL_DOUT_PIN 17
#define LOADCELL_SCK_PIN 16
// TFT Display and Touch Controller Initialization
Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ctp;
// Button Dimensions and Position
const int BUTTON_X = 10, BUTTON_Y = 10;
const int BUTTON_W = 100, BUTTON_H = 40;
const int BUTTON_TEXTSIZE = 2;
// Load Cell (HX711) Setup
HX711 loadCell;
// Global variables to store exponential function parameters
float a = 0, b = 0;
void setup() {
Serial.begin(115200);
Wire.setPins(10, 8);
tft.begin();
if (!ctp.begin(40)) {
Serial.println("Touchscreen initialization failed!");
while (1);
}
tft.fillScreen(ILI9341_BLACK);
drawButton(false);
loadCell.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
delay(1000);
static bool isLoadCellReady = false;
if (loadCell.is_ready()) {
if (!isLoadCellReady) {
initializeLoadCell();
isLoadCellReady = true;
}
// displayLoadCellReading();
performExponentialRegression();
} else {
Serial.println("HX711 not found.");
isLoadCellReady = false;
}
}
void loop() {
if (checkTouch()) {
toggleButton();
plotTimeSeriesCurve();
}
}
void initializeLoadCell() {
loadCell.set_scale();
Serial.println("Tare... remove any applied pressure.");
loadCell.tare();
Serial.println("Tare done.");
}
void displayLoadCellReading() {
long reading = loadCell.get_units(10);
tft.setCursor(BUTTON_X + 10, BUTTON_Y + 200);
tft.print("Result: ");
tft.setCursor(BUTTON_X + 10, BUTTON_Y + 260);
tft.print(reading);
Serial.println(reading);
}
void drawButton(bool inverted) {
tft.fillRect(BUTTON_X, BUTTON_Y, BUTTON_W, BUTTON_H, inverted ? ILI9341_WHITE : ILI9341_BLUE);
tft.setTextColor(inverted ? ILI9341_BLUE : ILI9341_WHITE);
tft.setTextSize(BUTTON_TEXTSIZE);
tft.setCursor(BUTTON_X + 10, BUTTON_Y + 10);
tft.print("PLOT");
}
bool checkTouch() {
if (!ctp.touched()) return false;
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
return (p.x >= BUTTON_X && p.x <= BUTTON_X + BUTTON_W &&
p.y >= BUTTON_Y && p.y <= BUTTON_Y + BUTTON_H);
}
void toggleButton() {
drawButton(true);
delay(100); // Debounce
drawButton(false);
}
void plotTimeSeriesCurve() {
const float frequency = 1.0;
const float amplitude = 30.0;
const float offset = 120;
tft.fillRect(0, 80, 320, 160, ILI9341_BLACK);
drawButton(false);
for (int i = 0; i < tft.width(); i++) {
float y = offset + amplitude * sin(2 * PI * frequency * i / tft.width());
tft.drawPixel(i, y, ILI9341_YELLOW);
}
}
void plotExponentialCurve() {
if (a == 0 && b == 0) return; // Check if a and b have been set
tft.fillRect(0, 80, 320, 160, ILI9341_BLACK); // Clear the area for drawing
drawButton(false);
float xPixel, yPixel, yValue;
for (int i = 0; i < tft.width(); i++) {
xPixel = i;
yValue = a * exp(b * xPixel / tft.width()); // Calculate y-value using the exponential function
yPixel = map(yValue, 0, a, 160, 80); // Map y-value to screen coordinates
tft.drawPixel(xPixel, yPixel, ILI9341_YELLOW);
}
}
// Perform Exponential Regression
void performExponentialRegression() {
const int sampleSize = 50; // Number of samples
const long interval = 100; // Interval between samples in ms (5 seconds total)
float sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;
float x, y;
tft.setCursor(BUTTON_X + 10, BUTTON_Y + 200);
tft.print("Estimating... ");
// Collect Data
for (int i = 0; i < sampleSize; i++) {
x = i * (5.0 / sampleSize); // Scale x to 5 seconds
y = log(loadCell.get_units()); // Natural log of the reading
// Summations for linear regression
sumX += x;
sumY += y;
sumXY += x * y;
sumX2 += x * x;
delay(interval);
}
// Calculate a and b for y = a * exp(b * x)
b = (sampleSize * sumXY - sumX * sumY) / (sampleSize * sumX2 - sumX * sumX);
a = exp((sumY - b * sumX) / sampleSize);
// Display or use the calculated a and b values
Serial.print("a: ");
Serial.print(a);
Serial.print(", b: ");
Serial.println(b);
// Call function to plot the curve
plotExponentialCurve();
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1
Loading
ili9341-cap-touch
ili9341-cap-touch