#include <LiquidCrystal.h>
// تعریف پین های اتصال LCD به Arduino
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
//______________For_Timer_Variable____________
unsigned long lastMillis;
int minutes = 480;
int State = 0;
long Timestamp_Button_Pressed;
unsigned long Countdown_start;
int time_end_state = 0;
String current_time;
//_____________End_For_Timer_Variable____________
int LDR_1 = A1; // potentiometer wiper (middle terminal) connected to analog pin 3
int LDR_temp_1 = 0; // variable to store the value read
int LDR_2 = A2; // potentiometer wiper (middle terminal) connected to analog pin 3
int LDR_temp_2 = 0; // variable to store the value read
const float Rref = 10000;
const float RL10 = 50;
const float GAMMA = 0.7;
double Lux (int RawADC0) {
float Vout = RawADC0 / 1023.*5;
float R = Rref*(1/((5/Vout)-1));
float lux = pow(RL10*1e3*pow(10,GAMMA)/R, (1/GAMMA));
return lux;
}
byte Shine[8] = {
B00000,
B00100,
B10101,
B01110,
B11011,
B01110,
B10101,
B00100,
};
byte sunChar1[8] = {
B00000, B00101, B01010, B10100, B01011, B10111, B01111, B01111,
};
byte sunChar2[8] = {
B00000, B10100, B01010, B00101, B11010, B11101, B11110, B11110,
};
byte sunChar3[8] = {
B01111, B10111, B10111, B01011, B10100, B01010, B00101, B00000,
};
byte sunChar4[8] = {
B11110, B11110, B11101, B11010, B00101, B01010, B10100, B00000,
};
byte MoonChar1[] = {
B00000, B00000, B00010, B00100, B01100, B01100, B11110, B11111,
};
byte MoonChar2[] = {
B00000, B00000, B00000, B00000, B00000, B00000, B00001, B00011,
};
byte MoonChar3[] = {
B01111, B01111, B00011, B00000, B00000, B00000, B00000, B00000,
};
byte MoonChar4[] = {
B11110, B11100, B11000, B00000, B00000, B00000, B00000, B00000,
};
void setup() {
lcd.begin(16, 2); // آغاز به کار LCD با اندازه 16x2
Serial.begin(9600);
lcd.createChar(0, MoonChar1);
lcd.createChar(1, MoonChar2);
lcd.createChar(2, MoonChar3);
lcd.createChar(3, MoonChar4);
lcd.createChar(4, sunChar1); // تعریف آیکون خورشید 1
lcd.createChar(5, sunChar2); // تعریف آیکون خورشید 2
lcd.createChar(6, sunChar3); // تعریف آیکون خورشید 3
lcd.createChar(7, sunChar4); // تعریف آیکون خورشید 4
//______________For_Timer_____________
Timestamp_Button_Pressed = millis();
lastMillis = millis();
//______________End_For_Timer_________
}
void loop() {
LDR_temp_1 = analogRead(LDR_1); // read the input pin
Serial.println(LDR_temp_1);
LDR_temp_2 = analogRead(LDR_2); // read the input pin
Serial.println(LDR_temp_2);
if (LDR_temp_1 > 1000) {
Moon();
} else {
Sun();
}
lcd.setCursor(3, 0);
lcd.print(Lux(LDR_temp_2));
lcd.print(" Lux ");
lcd.setCursor(14, 0);
// lcd.write(byte(8));
//______________For_Timer_____________
if (minutes * 60000 > (millis() - Countdown_start))
{
current_time = TimeLeft((minutes * 60000 - (millis() - Countdown_start)));
Serial.println(current_time);
printTime(current_time);
time_end_state = 1;
} else {
time_end_state = 0;
}
if (time_end_state == 0) {
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
call_timer();
}
//______________For_Timer_____________
Serial.println("EndLoop");
}
void Moon() {
lcd.setCursor(0, 0);
lcd.write(byte(0));
lcd.write(byte(1));
lcd.setCursor(0, 1);
lcd.write(byte(2));
lcd.write(byte(3));
}
void Sun() {
lcd.setCursor(0, 0);
lcd.write(byte(4));
lcd.write(byte(5));
lcd.setCursor(0, 1);
lcd.write(byte(6));
lcd.write(byte(7));
}
void printTime(String Time) {
lcd.setCursor(3, 1);
lcd.print(Time);
lcd.print(" S");
}
void call_timer() {
if (time_end_state == 0) {
if (millis() - Timestamp_Button_Pressed > 200) {
if (State == 0) {
Countdown_start = millis();
}
}
}
}
String TimeLeft(unsigned long MsLeft) {
String Result;
int M;
int S;
M = (long)MsLeft / 60000;
if (M < 10) Result = (String)"0" + M + ":"; else Result = (String)M + ":";
S = (long)((MsLeft - M * 60000) / 1000);
if (S < 10) Result = (String)Result + "0" + S ; else Result = (String)Result + S;
return Result;
}
//______________For_Timer_Function_____________