#include <SPI.h>
#include <Wire.h>//alows communications with I2C devices
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>//library for Monochrome OLEDs based on SSD1306 drivers
#include "Adafruit_ILI9341.h"
#define TFT_DC 9
#define TFT_CS 10
#define trigPin 7
#define echoPin 6
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
//----------- Temperature -----------------
#include <DHT.h>
// Define the sensor type (DHT22)
#define DHT_TYPE DHT22
// Pin where your DHT11 is connected to
#define DHT_PIN 2
// Initialize the DHT sensor
DHT dht(DHT_PIN, DHT_TYPE);
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
Serial.begin(9600);// Initialize serial communication
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize the DHT sensor
dht.begin();
tft.begin();
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(500); // wait two seconds for initializing
oled.clearDisplay(); // clear display
}
void loop() {
float duration;
float distance_Km;
digitalWrite(trigPin, LOW); //PULSE ___|---|___
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance_Km = (duration/2) / 29.2;
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(2);
tft.println("Distanta parcursa");
tft.setCursor(0, 20);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setTextSize(2);
tft.println(distance_Km);
tft.setCursor(90, 20);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setTextSize(2);
tft.println("Km");
delay(500);
oled.setCursor(0,0); //oled display
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.println("Distanta parcursa");
oled.setCursor(20,10); //oled display
oled.setTextSize(1);
oled.setTextColor(WHITE);
oled.println(distance_Km);
oled.setCursor(60,10);
oled.setTextSize(1);
oled.println("Km");
delay(500);
Serial.println(distance_Km);
float temperature = dht.readTemperature();
tft.setCursor(0, 60);
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setTextSize(2);
tft.println("Temp lichid racire");
tft.setCursor(0, 80);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setTextSize(2);
tft.println(temperature);
tft.setCursor(90, 80);
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setTextSize(2);
tft.println("C");
delay(500);
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0,30); // set position to display (x,y)
oled.print("Temp lichid racire"); // set text
oled.setCursor(20,40); // set position to display (x,y)
oled.print(temperature); // set text
oled.setCursor(60,40);
oled.print("C");
oled.display(); // display on OLED
//------Temperature Code Starts Here-----------
// Read temperature and humidity data from the sensor
// float temperature = dht.readTemperature();
// float humidity = dht.readHumidity();
// Check if the data was successfully read from the sensor
if (!isnan(temperature)) {
// Print the temperature and humidity values to the serial monitor
Serial.print("Temp lichid racire (°C): ");
Serial.println(temperature);
} else {
// If there was an error reading data from the sensor, print an error message
Serial.println("Error reading from DHT sensor");
}
// Delay for a few seconds before taking the next reading
delay(500);
oled.clearDisplay();
}