/*
wihokมาตรฐานวิชาชีพ62
*/
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 1; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
#define LDR_PIN 2
// LDR Characteristics
const float GAMMA = 0.7;
const float RL10 = 50;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(LDR_PIN, INPUT);
pinMode(13, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop() {
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
lcd.setCursor(2, 0);
lcd.print("Room: ");
if (lux > 50) {
lcd.print("Light!");
} else {
lcd.print("Dark ");
}
lcd.setCursor(0, 1);
lcd.print("Light:");
lcd.print(lux);
lcd.print(" lux");
if(lux < 300){
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);
}
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
delay(20);
}