#include <TM1637TinyDisplay.h>
#include <Arduino.h>
#include <LiquidCrystal.h>
const int PT_SIG = A0;
const int LDR_AO = A1;
const int DSP_CLK = 2;
const int DSP_DIO = 3;
const int POWER_PUSH_BTN_PIN = 5;
const int RELAY_IN = 6;
bool power_status;
int threshold;
int last_ldr;
unsigned long sum_pt;
unsigned long sum_ldr;
int old_pt;
int index;
unsigned long pt_last_set_time;
unsigned long ldr_last_change_time;
bool dsp_toggle;
bool relay_status;
TM1637TinyDisplay display(DSP_CLK, DSP_DIO);
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
void setup() {
// put your setup code here, to run once:
pinMode(PT_SIG, INPUT);
pinMode(LDR_AO, INPUT);
pinMode(POWER_PUSH_BTN_PIN, INPUT);
pinMode(DSP_CLK, OUTPUT);
pinMode(DSP_DIO, OUTPUT);
pinMode(RELAY_IN, OUTPUT);
power_status = false;
threshold = analogRead(PT_SIG);
last_ldr = -1;
old_pt = -1;
dsp_toggle = false;
relay_status = false;
display.begin();
lcd.begin(16, 2);
Serial.begin(9600);
Serial.println("");
}
void clearLcd(){
for(int i=0;i<2;i++){
lcd.setCursor(0,i);
for (int i = 0; i < 16; ++i)
{
lcd.write(' ');
}
}
}
void loop() {
// put your main code here, to run repeatedly:
int powerBtnStatus = digitalRead(POWER_PUSH_BTN_PIN);
delay(20);
if(powerBtnStatus == HIGH){
power_status = ! power_status;
if(power_status) {
Serial.print("Status: Enable | Threshold: ");
Serial.println(threshold);
sum_pt = 0;
sum_ldr = 0;
index = 0;
}
else {
Serial.print("Status: Disable | Threshold: ");
Serial.println(threshold);
}
delay(200);
}
if(power_status == true) {
bool pt_changed = false;
int pt = analogRead(PT_SIG);
sum_pt += pt;
int ldr = analogRead(LDR_AO);
sum_ldr += ldr;
index ++;
if(index >= 10) {
pt = sum_pt / 10;
ldr = sum_ldr / index;
index = 0;
sum_pt = 0;
sum_ldr = 0;
if(abs(pt - threshold) > 15 ){
threshold = pt;
pt_changed = true;
pt_last_set_time = millis();
}
if(abs(ldr - last_ldr) > 15 || pt_changed){
// Serial.print(last_ldr);
// Serial.print('\t');
// Serial.print(ldr);
// Serial.print('\t');
// Serial.print(threshold);
// Serial.print('\t');
// Serial.println(pt);
if(ldr < threshold){
digitalWrite(RELAY_IN, HIGH);
relay_status = true;
}
else {
digitalWrite(RELAY_IN, LOW);
relay_status = false;
}
ldr_last_change_time = millis();
last_ldr = ldr;
}
if(millis() - pt_last_set_time < 500) {
display.showNumberDec(threshold);
display.setBrightness(7, true);
// lcd.setCursor(0,0);
// lcd.print("Threshold: ");
// lcd.print(threshold);
}
else if(millis() - pt_last_set_time < 2500) {
display.showNumberDec(threshold);
display.setBrightness(7, dsp_toggle);
delay(200);
dsp_toggle = !dsp_toggle;
}
else if(millis() - ldr_last_change_time < 1000) {
display.setBrightness(7, true);
display.showNumberDec(last_ldr);
// lcd.setCursor(0,1);
// lcd.print("LDR value: ");
// lcd.print(last_ldr);
}
else {
display.setBrightness(1, true);
display.showNumberDec(last_ldr);
}
}
clearLcd();
lcd.setCursor(0,0);
lcd.print("Threshold: ");
lcd.print(threshold);
lcd.setCursor(0,1);
lcd.print("LDR value: ");
lcd.print(last_ldr);
if(relay_status) {
lcd.setCursor(15,0);
lcd.print('*');
}
}
else{
display.clear();
clearLcd();
}
// Serial.print(pt);
// Serial.print('\t');
// Serial.println(ldr);
// delay(50);
}