project uts (copy) — Arduino Uno simulation

An interactive Arduino Uno circuit simulation you can run free in your browser on Velxio, by s160425043.

Components

Sketch code

#include <LiquidCrystal.h>
#include <DHT.h>
#include <Keypad.h>



// Inisialisasi LCD Paralel (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(A1, A2, A3, A4, A5, 13);

// Sensor Suhu
#define DHTPIN 3
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

// Konfigurasi Pin Komponen
const int PIR_PIN = 2;       // Sensor PIR
const int BUZZER_PIN = 4;    // Buzzer
const int LED_GREEN = 5;     // LED Hijau
const int LED_RED = 6;       // LED Merah



// Konfigurasi Keypad 4x4
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {7, 8, 9, 10};
byte colPins[COLS] = {11, 12, 13, 1}; 
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

bool systemArmed = true; // Status awal sistem aktif (ARMED)

void setup() {
  lcd.begin(16, 2);
  dht.begin();
  ledcAttach(BUZZER_PIN, 2000, 8); // pin, frekuensi, resolusi
  ledcWrite(BUZZER_PIN, 0);
  
  pinMode(PIR_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_RED, OUTPUT);
  
  // Memastikan buzzer mati saat pertama kali menyala
  noTone(BUZZER_PIN);

  // Tampilan awal saat sistem dinyalakan
  lcd.setCursor(0, 0);
  lcd.print("Sistem Keamanan");
  lcd.setCursor(0, 1);
  lcd.print("Rumah Aktif...");
  delay(2000);
  lcd.clear();
}

void loop() {
  // -------------------------------------------------------------
  // 1. LOGIKA KEYPAD (Kontrol Status ARMED / DISARMED)
  // -------------------------------------------------------------
  char key = keypad.getKey();
  if (key == '*') {
    systemArmed = !systemArmed;
    lcd.clear();
    if (systemArmed) {
      lcd.print("Sistem: ARMED");
    } else {
      lcd.print("Sistem: DISARMED");
    }
    delay(1000);
    lcd.clear();
  }

  // -------------------------------------------------------------
  // 2. PEMBACAAN SENSOR
  // -------------------------------------------------------------
  float temp = dht.readTemperature();
  int motion = digitalRead(PIR_PIN);

  // Variabel penanda status alarm
  bool isFire = (!isnan(temp) && temp > 35.0);
  bool isIntruder = (systemArmed && motion == HIGH);

  // -------------------------------------------------------------
  // 3. TAMPILAN BARIS 1 LCD (Suhu)
  // -------------------------------------------------------------
  lcd.setCursor(0, 0);
  lcd.print("Suhu: ");
  if (isnan(temp)) {
    lcd.print("Error ");
  } else {
    lcd.print(temp, 1);
    lcd.print((char)223); // Simbol derajat °
    lcd.print("C   ");
  }

  // -------------------------------------------------------------
  // 4. LOGIKA PERINGATAN & INDIKATOR (DIPISAH BERDASARKAN STATUS)
  // -------------------------------------------------------------
  if (isFire) { 
    // Prioritas 1: Bahaya Kebakaran
    lcd.setCursor(0, 1);
    lcd.print("BAHAYA: FIRE!   ");
    
    digitalWrite(LED_RED, HIGH);
    digitalWrite(LED_GREEN, LOW);
    ledcWrite(BUZZER_PIN,128);
  } 
  else if (isIntruder) { 

More projects by s160425043