/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/587688b0-9cc0-4a08-a55b-81d7196ed3be
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
String textStatus;
float lux;
int analogValue;
int lampStatus;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include "thingProperties.h"
//Inisialisasi Pin dan Variabel
int led = 16;
int ldrpin = 34;
// Deklarasikan variabel resistance, voltage, dan
//variabel untuk menyimpan status lampu (0 = off, 1 = on)
float resistance;
float voltage;
//inisialisasi LCD (ALAMAT, KOLOM, BARIS)
LiquidCrystal_I2C lcd(0x27, 20, 4);
// PWM for LED, Brightness value (0-255)
int ledBrightness = 0; // Brightness value (0-255)
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
pinMode(led, OUTPUT);
pinMode(ldrpin, INPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(7, 0);
lcd.print("RAIHAN");
lcd.setCursor(2, 1);
lcd.print("RAMANDHA SAPUTRA");
lcd.setCursor(4, 3);
lcd.print("41422110039");
delay(3000);
lcd.clear();
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}
void loop() {
ArduinoCloud.update();
// Your code here
// Default by table
const float GAMMA = 0.7;
const float RL10 = 50;
int analogValue = analogRead(ldrpin);
Serial.print("Analog Value : ");
Serial.println(analogValue);
// Hitung nilai
voltage = analogValue * (3.3 / 4096);
resistance = 2000 * voltage / (1 - voltage / 3.3);
// Convert the analog value into lux value:
int light = analogRead(ldrpin);
float voltage = light / 4096. * 5;
float resistance = 2000 * voltage / (1- voltage /5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
// Tampilkan nilai lux pada Serial Monitor dengan dua desimal
Serial.print("Lux : ");
Serial.println(lux, 2);
lcd.setCursor(4, 0);
lcd.print("STATUS LAMPU");
// Variabel untuk status teks (On/Off)
String textStatus;
//Penghitungan dan Pengaturan Status Lampu
//Lampu Menyala 50%
if (analogValue >= 928 && analogValue < 3092)
{
int nyala = map(light, 0, 4096, 0,128);
ledBrightness = 128; // 50% brightness
analogWrite(led, nyala); // Atur kecerahan LED
lcd.setCursor(4, 2);
lcd.print("Cahaya Redup");
lcd.setCursor(1, 3);
lcd.print("Lampu menyala: 50%");
delay(2000);
lcd.clear();
Serial.print("Status Cahaya : ");
Serial.println("Cahaya Redup");
lampStatus = 50;
Serial.print("Lamp Status : ");
Serial.print(lampStatus);
Serial.println("%");
textStatus = "On"; // Status teks untuk lampu 50% menyala
Serial.print("Status Lampu : ");
Serial.println(textStatus);
Serial.print("-----------------------------");
Serial.println();
}
// Kondisi lampu menyala 100%
else if (analogValue >= 3092)
{
int nyala = map(light, 0, 4096, 0,255);
ledBrightness = 255; // 100% brightness
analogWrite(led, ledBrightness); // Atur kecerahan LED
lcd.setCursor(4, 2);
lcd.print("Cahaya Gelap");
lcd.setCursor(1, 3);
lcd.print("Lampu menyala:100%");
Serial.print("Status Cahaya : ");
Serial.println("Cahaya Gelap");
lampStatus = 100;
Serial.print("Lamp Status : ");
Serial.print(lampStatus);
Serial.println("%");
// Status teks untuk lampu 100% menyala
textStatus = "On";
Serial.print("Status Lampu : ");
Serial.println(textStatus);
Serial.print("-----------------------------");
Serial.println();
delay(2000);
lcd.clear();
}
// Kondisi lampu mati
else
{
ledBrightness = 0; // Lampu mati
analogWrite(led, ledBrightness); // Matikan LED
lcd.setCursor(4, 2);
lcd.print("Cahaya Terang");
lcd.setCursor(5, 3);
lcd.print("Lampu Mati");
lampStatus = 0;
Serial.print("Lamp Status : ");
Serial.print(lampStatus);
Serial.println("%");
Serial.print("Status Cahaya : ");
Serial.println("Cahaya Terang");
textStatus = "Off"; // Status teks untuk lampu mati
Serial.print("Status Lampu : ");
Serial.println(textStatus);
Serial.print("-----------------------------");
Serial.println();
delay(2000);
lcd.clear();
}
}
/*
Since AnalogValue is READ_WRITE variable, onAnalogValueChange() is
executed every time a new value is received from IoT Cloud.
*/
void onAnalogValueChange() {
// Add your code here to act upon AnalogValue change
}
/*
Since TextStatus is READ_WRITE variable, onTextStatusChange() is
executed every time a new value is received from IoT Cloud.
*/
void onTextStatusChange() {
// Add your code here to act upon TextStatus change
}
/*
Since Lux is READ_WRITE variable, onLuxChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLuxChange() {
// Add your code here to act upon Lux change
}
/*
Since LampStatus is READ_WRITE variable, onLampStatusChange() is
executed every time a new value is received from IoT Cloud.
*/
void onLampStatusChange() {
// Add your code here to act upon LampStatus change
}