// Combine Codes sa Station 2 final codes but not configure in ESP32 WROOM
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "DHTesp.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const int trigPin = 12;
const int echoPin =13;
long duration;
int distance;
const int DHT_PIN = 22;
DHTesp dhtSensor;
#define ONE_WIRE_BUS 14
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int SensorPin = 7;
int OutputPin = 8;
void task1(void *parameter) {
for (;;) {
long percentage = (-100 * distance) / 50;
float pulp = (100 + percentage);
digitalWrite(trigPin,LOW);
delayMicroseconds(10);
digitalWrite(trigPin,HIGH);
delayMicroseconds(30);
digitalWrite(trigPin,LOW);
duration= pulseIn(echoPin,HIGH);
distance=duration*(0.034/2);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
tft.setCursor(10, 80);
tft.print("Temp: " + String(data.temperature, 2) + "°C");
tft.setCursor(10, 40);
tft.print("Humidity: " + String(data.humidity, 1) + "%");
tft.setCursor(10, 120);
tft.print("Level:");
tft.print(pulp);
tft.print("%");
sensors.requestTemperatures();
tft.setCursor(10, 160);
tft.print("Tem1: ");
tft.print(sensors.getTempCByIndex(0));
tft.setCursor(10, 200);
tft.print("Fahr: ");
tft.print(sensors.getTempFByIndex(0));
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void task2(void *parameter) {
for (;;) {
int SensorValue = digitalRead(SensorPin);
Serial.print("SensorPin Value: ");
Serial.println(SensorValue);
if (SensorValue==LOW){ // LOW MEANS Object Detected
digitalWrite(OutputPin, HIGH);
delay(2000);
if(SensorValue==LOW){
digitalWrite(OutputPin, LOW);
delay(5000); // Edit natin delay until such time na nakalabas yung conveyor
}
}
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
sensors.begin(); //DSB18B20 BEGING
tft.begin();
tft.setRotation(3);
tft.fillScreen(ILI9341_WHITE);
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(10, 0);
tft.println("SYSTEM MONITORING");
pinMode(OutputPin, OUTPUT);
pinMode(SensorPin, INPUT);
Serial.begin(9600);
xTaskCreatePinnedToCore(task1, "Task 1", 100000, NULL, 1, NULL, CONFIG_ARDUINO_RUNNING_CORE);
xTaskCreatePinnedToCore(task2, "Task 2", 7000, NULL, 1, NULL, CONFIG_ARDUINO_RUNNING_CORE);
}
void loop() {
// Do nothing
}