/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-lm35-temperature-sensor
*/
#define ADC_VREF_mV 5000.0 // in millivolt
#define ADC_RESOLUTION 1024.0
#define PIN_LM35 A0
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Inisialisasi objek LCD
const int lm35Pin = A0; // Pin analog untuk LM35
const int button1Pin = 2; // Pin untuk tombol 1
const int button2Pin = 3; // Pin untuk tombol 2
const int button3Pin = 4; // Pin untuk tombol 3
const int buzzerPin = 10; // Pin untuk buzzer
const int ledPins[5] = {5, 6, 7, 8, 9}; // Pin untuk 5 LED
void setup() {
Serial.begin(9600); lcd.init();
lcd.backlight();
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
pinMode(button3Pin, INPUT_PULLUP);
for (int i = 0; i < 5; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buzzerPin, OUTPUT);
// Mulai serial monitor jika diperlukan
// Serial.begin(9600);
}
void loop() {
// get the ADC value from the temperature sensor
int adcVal = analogRead(PIN_LM35);
// convert the ADC value to voltage in millivolt
float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
// convert the voltage to the temperature in Celsius
float tempC = milliVolt / 10;
// convert the Celsius to Fahrenheit
float tempF = tempC * 9 / 5 + 32;
// print the temperature in the Serial Monitor:
Serial.print("Temperature: ");
Serial.print(tempC); // print the temperature in Celsius
Serial.print("°C");
Serial.print(" ~ "); // separator between Celsius and Fahrenheit
Serial.print(tempF); // print the temperature in Fahrenheit
Serial.println("°F");
int sensorValue = analogRead(lm35Pin);
float temperature = (sensorValue * 5.0 / 1024) * 100; // Konversi nilai analog ke suhu Celsius
int button1State = digitalRead(button1Pin);
int button2State = digitalRead(button2Pin);
int button3State = digitalRead(button3Pin);
lcd.setCursor(0, 0);
lcd.print("Temperature: ");
lcd.setCursor(0, 1);
lcd.print(temperature);
lcd.print(" C ");
if (temperature < 20) {
// Mengatur LED sesuai dengan rentang suhu tertentu
for (int i = 0; i < 5; i++) {
digitalWrite(ledPins[i], LOW);
}
digitalWrite(ledPins[0], HIGH); // LED pertama menyala jika suhu kurang dari 20°C
} else if (temperature >= 20 && temperature < 40) {
// Implementasikan logika LED untuk rentang suhu 20-40
// ...
} else if (temperature >= 20 && temperature < 40) {
// Implementasikan logika LED untuk rentang suhu 20-40
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], LOW);
digitalWrite(ledPins[4], LOW);
} else if (temperature >= 40 && temperature < 60) {
// Implementasikan logika LED untuk rentang suhu 40-60
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], LOW);
digitalWrite(ledPins[4], LOW);
} else if (temperature >= 60 && temperature < 80) {
// Implementasikan logika LED untuk rentang suhu 60-80
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], HIGH);
digitalWrite(ledPins[4], LOW);
} else if (temperature >= 80 && temperature < 100) {
// Implementasikan logika LED untuk rentang suhu 80-100
digitalWrite(ledPins[0], LOW);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], LOW);
digitalWrite(ledPins[4], HIGH);
// Suhu melebihi 100°C, lakukan sesuai kebutuhan
}
// Lanjutkan dengan logika LED untuk rentang suhu lainnya
if (button1State == LOW) {
// Logika untuk tombol 1
// ...
}
if (button2State == LOW) {
// Logika untuk tombol 2
// ...
}
if (button3State == LOW) {
// Logika untuk tombol 3
// ...
}
// Logika untuk mengendalikan buzzer berdasarkan suhu atau tombol
// ...
delay(1000); // Penundaan untuk menghindari pembacaan yang terlalu cepat
delay(1000);
}