#include <NewPing.h>
#include "DHT.h"
#define TRIGGER_PIN_1 2 // Pin trigger sensor ultrasonik pertama
#define ECHO_PIN_1 3 // Pin echo sensor ultrasonik pertama
#define TRIGGER_PIN_2 4 // Pin trigger sensor ultrasonik kedua
#define ECHO_PIN_2 5 // Pin echo sensor ultrasonik kedua
#define MAX_DISTANCE 20 // Jarak maksimum deteksi sensor (20 cm)
#define MIN_DISTANCE 1 // Jarak minimum deteksi sensor (1 cm)
#define MAX_KUOTA 10 // Kuota maksimal ruangan
#define DHTPIN 6
#define DHTTYPE DHT22
long durasi, jarak;
const int buttonPin = 7; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int kuota_saat_ini = MAX_KUOTA; // Kuota saat ini
NewPing sonar1(TRIGGER_PIN_1, ECHO_PIN_1, MAX_DISTANCE);
NewPing sonar2(TRIGGER_PIN_2, ECHO_PIN_2, MAX_DISTANCE);
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// initialize the LED pin as an output:
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
dht.begin();
}
void loop() {
// read the state of the pushbutton value:
//digitalWrite(ledPin, LOW);
buttonState = digitalRead(buttonPin);
if(buttonState == LOW){
while(1) {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// Serial.print(F("Humidity: "));
// Serial.print(h);
// Serial.print(F("% Temperature: "));
// Serial.print(t);
// Serial.print(F("°C "));
// Serial.print(f);
// Serial.print(F("°F Heat index: "));
// Serial.print(hic);
// Serial.print(F("°C "));
// Serial.print(hif);
// Serial.println(F("°F"));
int jarak_sensor1 = sonar1.ping_cm();
int jarak_sensor2 = sonar2.ping_cm();
if (jarak_sensor1 < MIN_DISTANCE && jarak_sensor2 < MIN_DISTANCE) {
// Kedua sensor mendeteksi objek terlalu dekat
return;
}
if (jarak_sensor1 >= MIN_DISTANCE && jarak_sensor1 <= 9) {
// Sensor 1 mendeteksi objek dalam rentang yang diinginkan
kuota_saat_ini++;
Serial.print("Objek keluar. Kuota saat ini: ");
Serial.println(kuota_saat_ini);
digitalWrite(ledPin, LOW);
}
if (jarak_sensor2 >= MIN_DISTANCE && jarak_sensor2 <= 9) {
// Sensor 2 mendeteksi objek dalam rentang yang diinginkan
if (t>40){
// Kuota sudah penuh, nyalakan LED indikator
Serial.print(F("% Temperature: "));
Serial.println(t);
digitalWrite(ledPin, HIGH);
Serial.println("Dilarang Masuk");
delay(1000);
digitalWrite(ledPin, LOW);
}
else if (kuota_saat_ini >= 1) {
kuota_saat_ini--;
digitalWrite(ledPin, LOW);
Serial.print(F("% Temperature: "));
Serial.println(t);
Serial.print("boleh masuk. Kuota saat ini: ");
Serial.println(kuota_saat_ini);
}
else{
digitalWrite(ledPin, HIGH);
Serial.println("Kuota ruangan penuh!");
}
}
delay(1000); // Delay 1 detik untuk menghindari pembacaan ganda
}
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
dht1:VCC
dht1:SDA
dht1:NC
dht1:GND
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
ultrasonic2:VCC
ultrasonic2:TRIG
ultrasonic2:ECHO
ultrasonic2:GND
led1:A
led1:C
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
r1:1
r1:2