#include <LiquidCrystal_I2C.h>
#include <math.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const float GAMMA = 0.7;
const float RL10 = 50;
byte L_E[] = {
B01111,
B10000,
B10000,
B11111,
B10000,
B10000,
B01111,
B00000
};
byte L_N[] = {
B10001,
B10001,
B10001,
B11111,
B10001,
B10001,
B10001,
B00000
};
int ledPin = 5; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
lcd.begin(16, 2);
lcd.createChar(0, L_E);
lcd.createChar(1, L_N);
lcd.setCursor(0,0);
lcd.print(" Lab 3");
lcd.setCursor(0,1);
lcd.print("17:");
lcd.print("MYCI");
lcd.write(0);
lcd.write(1);
lcd.print("KO I.C.");
delay(2000);
lcd.clear();
lcd.setCursor(0,0);
}
void loop() {
// put your main code here, to run repeatedly:
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.print("LUX:");
lcd.print(lux);
lcd.setCursor(0,1);
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
lcd.print("Motion DET");
// We only want to print on the output change, not state
pirState = HIGH;
delay(1000);
}
delay(1000);
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
lcd.print("no motion");
// We only want to print on the output change, not state
pirState = LOW;
delay(1000);
}
delay(1000);
}
lcd.clear();
}