// LCD1602 to Arduino Uno connection example
#include <Servo.h>
#include <LiquidCrystal.h>
#define SPEAKER_PIN 5
const float BETA = 3950;
int pos=180;
bool isFin=false;
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
Servo myservo;
void setup() {
lcd.begin(16, 2);
myservo.attach(6);
Serial.begin(9600);
pinMode(SPEAKER_PIN, OUTPUT);
}
void loop() {
const float GAMMA = 0.7;
const float RL10 = 50;
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(0, 0);
lcd.print("Status:");
if(lux>=50){
lcd.print("Lighting");
}else{
lcd.print("Dark");
}
lcd.setCursor(0, 1);
lcd.print("Lux:");
lcd.print(lux);
if(pos>0&&lux>=50){
isFin=false;
for(pos = 180; pos >= 0; pos -=1){
myservo.write(pos);
if(pos%20==0){
tone(SPEAKER_PIN, 2000);}
delay(15);
noTone(SPEAKER_PIN);
}
isFin=true;
}
else if(pos<=0&&lux<50){
isFin=false;
for(pos = 0; pos <= 180; pos +=1){
myservo.write(pos);
if(pos%20==0){
tone(SPEAKER_PIN, 2000);}
delay(15);
noTone(SPEAKER_PIN);
}
isFin=true;
}
delay(500);
lcd.clear();
}