#include <LiquidCrystal.h>
#define photoresistance A0
#define THRESHOLD 100
#define PERIOD 15
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));
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
bool etat_precedant;
bool etat_actuel;
void setup()
{
lcd.begin(16,2);
//lcd.print(lux);
//Serial.begin(9600);
}
void loop()
{
etat_actuel = get_ldr();
if(!etat_actuel && etat_precedant)
{
print_byte(get_byte());
}
etat_precedant = etat_actuel;
}
bool get_ldr()
{
int voltage = analogRead(resistance);
return voltage > THRESHOLD ? true : false;
}
char get_byte()
{
char ret = 0;
delay(PERIOD*1.5);
for(int i = 0; i < 8; i++)
{
ret = ret | get_ldr() << i;
delay(PERIOD);
}
return ret;
}
void print_byte(char my_byte)
{
char buff[2];
sprintf(buff, "%c", my_byte);
lcd.print(buff);
}