Hey Everyone....I need a little help on this code I'm a newbie but using AI to help me. I'v noticed that the AI's arent always the smartest. What i am needing this code to do is to use a dht22 temp sensor to measure temp and display (st7789 display) it in a gauge that fills the middle of the screen, with an arrow that is animated moving every 1 sec and creating a long tick mark in the color of green for unchanged, red for rise and blue for drop. Then I am using an ultrasonic sensor to measure distance starting at 12 inchs until touching the sensor. I want it in 1 inch increments and i am using timerfreetone to give me a low tone at 12 and increasing in pitch the lower it goes. I also having it display the distance on the screen. The problem is that i want the tones to be smooth and steady as they go up or down and continue to play as long as there is a reading in my parameters. Can anyone help me and please dont judge me for using AI
here is the full code:
multiple
lines
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <DHT.h>
#include <NewPing.h>
#include <TimerFreeTone.h>
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
#define DHTPIN 2
#define DHTTYPE DHT22
#define TRIGGER_PIN 6
#define ECHO_PIN 7
#define PIEZO_PIN 5
#define MAX_DISTANCE 72
DHT dht(DHTPIN, DHTTYPE);
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
float lastTemperature = 0;
float smoothedDistance = 0.0;
float smoothingFactor = 0.85;
int lastDistance = -1; // Track previous distance
#define COLOR_INCREASING ST77XX_RED
#define COLOR_DECREASING ST77XX_BLUE
#define COLOR_STEADY ST77XX_GREEN
void setup() {
Serial.begin(9600);
dht.begin();
tft.init(240, 320);
tft.setRotation(2);
tft.fillScreen(ST77XX_BLACK);
displayTitle();
}
void loop() {
float temperature = dht.readTemperature(true);
uint16_t trendColor = getTrendColor(temperature);
if (!isnan(temperature)) {
displayTemperatureGauge(temperature, trendColor);
displayTemperatureValue(temperature, trendColor);
}
updateDistanceAndTone();
}
void displayTitle() {
tft.setTextSize(2);
tft.setTextColor(ST77XX_GREEN);
int16_t x1, y1;
uint16_t w, h;
tft.getTextBounds("Ghost Temp Detector", 0, 0, &x1, &y1, &w, &h);
tft.setCursor((240 - w) / 2, 10);
tft.print("Ghost Temp Detector");
tft.getTextBounds("and RemPod", 0, 0, &x1, &y1, &w, &h);
tft.setCursor((240 - w) / 2, 40);
tft.print("and RemPod");
}
uint16_t getTrendColor(float temperature) {
uint16_t trendColor = COLOR_STEADY;
if (!isnan(temperature)) {
if (temperature > lastTemperature) {
trendColor = COLOR_INCREASING;
} else if (temperature < lastTemperature) {
trendColor = COLOR_DECREASING;
}
lastTemperature = temperature;
}
return trendColor;
}
void displayTemperatureGauge(float temperature, uint16_t trendColor) {
int centerX = 120;
int centerY = 200;
int radius = 60;
int angleStart = 0;
int angleEnd = 180;
int needleAngle = map(temperature, 50, 90, angleStart, angleEnd);
needleAngle = (needleAngle + 180) % 360;
for (int angle = angleStart; angle <= angleEnd; angle += 10) {
int rotatedAngle = (angle + 180) % 360;
int tickColor = (rotatedAngle < needleAngle) ? COLOR_INCREASING : COLOR_DECREASING;
int xStart = centerX + (radius - 10) * cos(radians(rotatedAngle));
int yStart = centerY + (radius - 10) * sin(radians(rotatedAngle));
int xEnd = centerX + radius * cos(radians(rotatedAngle));
int yEnd = centerY + radius * sin(radians(rotatedAngle));
tft.drawLine(xStart, yStart, xEnd, yEnd, tickColor);
}
int needleX = centerX + (radius - 15) * cos(radians(needleAngle));
int needleY = centerY + (radius - 15) * sin(radians(needleAngle));
tft.drawLine(centerX, centerY, needleX, needleY, trendColor);
tft.fillCircle(centerX, centerY, 3, ST77XX_BLACK);
tft.drawCircle(centerX, centerY, 4, trendColor);
}
void displayTemperatureValue(float temperature, uint16_t trendColor) {
tft.fillRect(80, 105, 80, 20, ST77XX_BLACK);
tft.setTextColor(trendColor);
tft.setTextSize(3);
tft.setCursor(90, 105);
tft.print(temperature, 1);
tft.print("F");
}
void updateDistanceAndTone() {
int rawDistance = sonar.ping_in();
if (rawDistance > 0 && rawDistance <= 12) {
smoothedDistance = (smoothingFactor * smoothedDistance) + ((1 - smoothingFactor) * rawDistance);
}
int roundedDistance = (int)smoothedDistance;
if (roundedDistance != lastDistance) {
tft.fillRect(10, 280, 220, 20, ST77XX_BLACK);
tft.setTextColor(ST77XX_YELLOW);
tft.setTextSize(2);
tft.setCursor(10, 280);
if (roundedDistance == 0 || roundedDistance > 12) {
tft.print("Out of Range");
TimerFreeTone(PIEZO_PIN, 0, 0);
} else {
tft.print("Distance: ");
tft.print(roundedDistance);
tft.print(" in");
int toneFreq = map(roundedDistance, 12, 1, 300, 1200);
TimerFreeTone(PIEZO_PIN, toneFreq, 1000);
}
lastDistance = roundedDistance;
}
delay(5);
}