#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Servo.h>
Servo myservo;
#define LEDpin 13
#define LDRpin A0
#define POTpin A1
#define SWpin 8
const float GAMMA = 0.7;
const float RL10 = 50;
boolean State = LOW;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
myservo.attach(5);
myservo.write(0);
pinMode(LEDpin, OUTPUT);
pinMode(SWpin, INPUT);
}
void loop() {
//--------------------------- Switch ----------------------------
if (digitalRead(SWpin) == LOW) {
State = !State;
}
//--------------------------- LDR ----------------------------
int LDRval = analogRead(LDRpin);
float voltage = LDRval / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
//--------------------------- POT ----------------------------
int POTval = map(analogRead(POTpin),0,1023,0,180);
if (State == HIGH) {
//--------------------------- Serial Monitor -----------------
Serial.print("Light : ");
Serial.print(long(lux));
Serial.print("\t POT Value : ");
Serial.println(POTval);
Serial.print("SW state : ");
Serial.println(State);
//--------------------------- LED ----------------------------
if (long(lux) <= 300) {
digitalWrite(LEDpin, HIGH);
}
else {
digitalWrite(LEDpin, LOW);
}
myservo.write(POTval);
//--------------------------- LCD ----------------------------
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("TEST SKILL START");
lcd.setCursor(0, 1);
lcd.print("Light : " + String(long(lux)) + " Lux");
lcd.setCursor(0, 2);
lcd.print("Servo : " + String(POTval) + " Degree");
delay(2000);
}
else {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("TEST SKILL STOP");
delay(2000);
}
}