#include <EEPROM.h>
const int trig = A1;
const int echo = A0;
byte ngon_tay_cai_6 = 6;
byte mosfet_9 = 9;
byte coi_chip_5 = 5;
byte led_4 = 4;
byte chan_sac_8 = 8;
byte button_10 = 10;
byte button_cong_12 = 12;
byte button_tru_11 = 11;
byte do_volt_pin_A3 = A3;
byte volt_cam_sac_A2 = A2;
byte rung_cai = 0;
byte rung_ut = 0;
byte rung_cai_1 = 0;
byte rung_cai_2 = 0;
byte rung_ut_1 = 0;
byte rung_ut_2 = 0;
byte che_do_rung = 0;
byte khong_lap = 0;
byte sau_tha_nut = 0;
byte khong_lap_12 = 0;
byte sau_tha_nut_12 = 0;
byte khong_lap_11 = 0;
byte sau_tha_nut_11 = 0;
byte loa1 = 0;
byte loa2 = 0;
byte den1 = 0;
byte den2 = 0;
byte sac_1 = 0;
byte sac_2 = 0;
byte tat = 0;
byte EP_1lan1 = 0;
byte EP_1lan2 = 0;
byte EP_1lan3 = 0;
byte EP_1lan4 = 0;
byte EP_1lan5 = 0;
byte hieu_so_bd = 0;
byte EP_1lan1_khoangcach = 0;
byte EP_1lan2_khoangcach = 0;
byte EP_1lan3_khoangcach = 0;
byte dk_kc = 0;
byte cau_hinh_kc = 0;
int dovolt_out = 0;
int dovolt_pin = 0;
float volt_pin = 0;
unsigned long pe_rung = 0;
unsigned long pe_rung_cai_1 = 0;
unsigned long pe_rung_cai_2 = 0;
unsigned long pe_rung_ut_1 = 0;
unsigned long pe_rung_ut_2 = 0;
unsigned long pe_loa1 = 0;
unsigned long pe_loa2 = 0;
unsigned long pe_nhanli = 0;
unsigned long pe_nhanli_12 = 0;
unsigned long pe_nhanli_11 = 0;
unsigned long pe11 = 0;
unsigned long pe12 = 0;
unsigned long pe11_sac = 0;
unsigned long pe12_sac = 0;
unsigned long pe_volt_battery = 0;
unsigned long thoi_gian = 0;
unsigned long khoang_cach = 0;
unsigned long khoang_cach_bao_dong = 0;
unsigned long tong_khoang_cach = 0;
unsigned long so_lan_do = 0;
unsigned long hieu_so = 0;
unsigned long pe_hieu_so = 0;
int GT_button = 0;
int GT_button_000 = 0;
int GT_button_cong_12_000 = 0;
int GT_button_tru_11_000 = 0;
int time_x = 0;
int GT_button_cong_12 = 0;
int GT_button_tru_11 = 0;
int do_rung = 0;
int do_am = 0;
int n_khoang_cach = 200;
void tien_sacpin (unsigned long giay)
{
unsigned long peham_sac = millis();
while((millis() - peham_sac <= giay) && (dovolt_out >= 650)){
dovolt_out = analogRead(volt_cam_sac_A2);
if((den1 == 0) && (millis() - pe12 >= 150)){pe11 = millis(); digitalWrite(led_4,HIGH); den1 = 1; den2 = 0;}
if((den2 == 0) && (millis() - pe11 >= 150)){pe12 = millis(); digitalWrite(led_4,LOW); den2 = 1; den1 = 0; }
if((sac_1 == 0) && (millis() - pe12_sac >= 100)){pe11_sac = millis(); digitalWrite(chan_sac_8,HIGH); sac_1 = 1; sac_2 = 0;}
if((sac_2 == 0) && (millis() - pe11_sac >= 1000)){pe12_sac = millis(); digitalWrite(chan_sac_8,LOW); sac_2 = 1; sac_1 = 0; }
GT_button_cong_12 = digitalRead(button_cong_12);
GT_button_tru_11 = digitalRead(button_tru_11);
if (GT_button_cong_12 != GT_button_cong_12_000)
{ GT_button_cong_12_000 = GT_button_cong_12;
pe_nhanli_12 = millis();
}
if (GT_button_tru_11 != GT_button_tru_11_000)
{ GT_button_tru_11_000 = GT_button_tru_11;
pe_nhanli_11 = millis();
}
}
}
void tien_sacpin_nghi (unsigned long giay)
{
unsigned long peham_sac = millis();
while((millis() - peham_sac <= giay) && (dovolt_out >= 650)){
dovolt_out = analogRead(volt_cam_sac_A2);
if((den1 == 0) && (millis() - pe12 >= 100)){pe11 = millis(); digitalWrite(led_4,HIGH); den1 = 1; den2 = 0; }
if((den2 == 0) && (millis() - pe11 >= 3000)){pe12 = millis(); digitalWrite(led_4,LOW); den2 = 1; den1 = 0; }
GT_button_cong_12 = digitalRead(button_cong_12);
GT_button_tru_11 = digitalRead(button_tru_11);
if (GT_button_cong_12 != GT_button_cong_12_000)
{ GT_button_cong_12_000 = GT_button_cong_12;
pe_nhanli_12 = millis();
}
if (GT_button_tru_11 != GT_button_tru_11_000)
{ GT_button_tru_11_000 = GT_button_tru_11;
pe_nhanli_11 = millis();
}
}
}
void setup()
{
pinMode(mosfet_9,OUTPUT);
digitalWrite(mosfet_9,HIGH);
Serial.begin(9600);
pinMode(trig,OUTPUT);
pinMode(coi_chip_5,OUTPUT);
pinMode(ngon_tay_cai_6,OUTPUT);
pinMode(chan_sac_8,OUTPUT);
pinMode(led_4,OUTPUT);
pinMode(echo,INPUT);
pinMode(button_10,INPUT);
pinMode(button_tru_11,INPUT);
pinMode(button_cong_12,INPUT);
pinMode(volt_cam_sac_A2,INPUT);
pinMode(do_volt_pin_A3,INPUT);
delay(6000);
che_do_rung = EEPROM.read(6);
delay(100);
if((che_do_rung != 1) && (che_do_rung != 0)){delay(1000); EEPROM.write(6, 0); delay(1000); che_do_rung = EEPROM.read(6);}
EP_1lan1 = EEPROM.read(31);
EP_1lan2 = EEPROM.read(32);
EP_1lan3 = EEPROM.read(33);
EP_1lan4 = EEPROM.read(34);
EP_1lan5 = EEPROM.read(35);
delay(100);
if((EP_1lan1 != 111) or (EP_1lan2 != 112) or (EP_1lan3 != 113) or (EP_1lan4 != 114) or (EP_1lan5 != 115)){
delay(50); EEPROM.write(12, 0); delay(50); EEPROM.write(12, 150);
delay(50); EEPROM.write(18, 0); delay(50); EEPROM.write(18, 155);
delay(50); EEPROM.write(31, 0); delay(20); EEPROM.write(32, 0); delay(20); EEPROM.write(33, 0); delay(20); EEPROM.write(34, 0); delay(20); EEPROM.write(35, 0);
delay(50); EEPROM.write(31, 111); delay(20); EEPROM.write(32, 112); delay(20); EEPROM.write(33, 113); delay(20); EEPROM.write(34, 114); delay(20); EEPROM.write(35, 115);
delay(50);
}
do_rung = EEPROM.read(12);
do_am = EEPROM.read(18);
EP_1lan1_khoangcach = EEPROM.read(54);
EP_1lan2_khoangcach = EEPROM.read(55);
EP_1lan3_khoangcach = EEPROM.read(56);
delay(20);
if((EP_1lan1_khoangcach != 111) or (EP_1lan2_khoangcach != 112) or (EP_1lan3_khoangcach != 113))
{
delay(20); EEPROM.write(39, 0);
delay(20); EEPROM.write(54, 0); delay(20); EEPROM.write(55, 0); delay(20); EEPROM.write(56, 0);
delay(20); EEPROM.write(54, 111); delay(20); EEPROM.write(55, 112); delay(20); EEPROM.write(56, 113); delay(20);
}
dk_kc = EEPROM.read(39);
delay(20);
n_khoang_cach = n_khoang_cach + (dk_kc * 50);
}
void loop()
{
if(millis() - pe_volt_battery >= 60000){
pe_volt_battery = millis();
digitalWrite(led_4,LOW);
analogWrite(ngon_tay_cai_6,0);
delay(30);
analogWrite(coi_chip_5,0);
delay(200);
dovolt_pin = analogRead(do_volt_pin_A3);
volt_pin = dovolt_pin / 1023.0 * 5.0;
Serial.println(volt_pin);
if(volt_pin <= 3.45){digitalWrite(mosfet_9,LOW); delay(30000);}
}
dovolt_out = analogRead(volt_cam_sac_A2);
if(dovolt_out >= 650){tat = 1;
digitalWrite(coi_chip_5,LOW);
analogWrite(ngon_tay_cai_6,0);
while(tat == 1){
digitalWrite(led_4,LOW);
digitalWrite(chan_sac_8,LOW);
delay(1000);
dovolt_out = analogRead(volt_cam_sac_A2);
Serial.println(dovolt_out);
if(dovolt_out < 650){digitalWrite(mosfet_9,LOW);delay(5000);}
else{digitalWrite(mosfet_9,HIGH);}
dovolt_pin = analogRead(do_volt_pin_A3);
volt_pin = dovolt_pin / 1023.0 * 5.0;
Serial.println("pin_1");
Serial.println(volt_pin);
Serial.println("pin_2");
if(volt_pin <= 4.07){ digitalWrite(chan_sac_8,HIGH); delay(2000); tien_sacpin(58000); }
else{tien_sacpin_nghi(300000);}
}
}
if((den1 == 0) && (millis() - pe12 >= 500)){ digitalWrite(led_4,HIGH); den1 = 1; den2 = 0; pe11 = millis(); }
if((den2 == 0) && (millis() - pe11 >= 500)){ digitalWrite(led_4,LOW); den2 = 1; den1 = 0; pe12 = millis(); }
if(dovolt_out < 650){
GT_button_cong_12 = digitalRead(button_cong_12);
GT_button_tru_11 = digitalRead(button_tru_11);
if (GT_button_cong_12 != GT_button_cong_12_000)
{ GT_button_cong_12_000 = GT_button_cong_12;
pe_nhanli_12 = millis();
}
if (GT_button_tru_11 != GT_button_tru_11_000)
{ GT_button_tru_11_000 = GT_button_tru_11;
pe_nhanli_11 = millis();
}
cau_hinh_kc = 0;
if ((GT_button_cong_12 == 1) && (GT_button_tru_11 == 1) && (millis() - pe_nhanli_12 > 3000) && (millis() - pe_nhanli_11 > 3000))
{
analogWrite(ngon_tay_cai_6,0);
digitalWrite(led_4,HIGH);
for(int i = 0; i < 3; i++){digitalWrite(coi_chip_5,HIGH);delay(1000); digitalWrite(coi_chip_5,LOW);delay(1000);} delay(2000);
while(cau_hinh_kc == 0){
GT_button_cong_12 = digitalRead(button_cong_12);
GT_button_tru_11 = digitalRead(button_tru_11);
GT_button = digitalRead(button_10);
if(GT_button == 1){cau_hinh_kc = 1; delay(1000);}
if((GT_button_cong_12 == 1) && (khong_lap_12 == 0)){
dk_kc = EEPROM.read(39); khong_lap_12 = 1; sau_tha_nut_12 = 1;
if(dk_kc == 0){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 1); delay(5); n_khoang_cach = 250;
for(int i = 0; i < 1; i++){digitalWrite(coi_chip_5,HIGH);delay(600); digitalWrite(coi_chip_5,LOW);delay(1000);}}
if(dk_kc == 1){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 2); delay(5); n_khoang_cach = 300;
for(int i = 0; i < 2; i++){digitalWrite(coi_chip_5,HIGH);delay(600); digitalWrite(coi_chip_5,LOW);delay(1000);}}
if(dk_kc == 2){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 3); delay(5); n_khoang_cach = 350;
for(int i = 0; i < 3; i++){digitalWrite(coi_chip_5,HIGH);delay(600); digitalWrite(coi_chip_5,LOW);delay(1000);}}
if(dk_kc == 3){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 4); delay(5); n_khoang_cach = 400;
for(int i = 0; i < 4; i++){digitalWrite(coi_chip_5,HIGH);delay(600); digitalWrite(coi_chip_5,LOW);delay(1000);}}
if(dk_kc == 4){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 5); delay(5); n_khoang_cach = 450;
for(int i = 0; i < 5; i++){digitalWrite(coi_chip_5,HIGH);delay(600); digitalWrite(coi_chip_5,LOW);delay(1000);}}
if(dk_kc == 5){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 6); delay(5); n_khoang_cach = 500;
for(int i = 0; i < 6; i++){digitalWrite(coi_chip_5,HIGH);delay(600); digitalWrite(coi_chip_5,LOW);delay(1000);}}
}
if((GT_button_cong_12 == 0) && (sau_tha_nut_12 == 1)){khong_lap_12 = 0; sau_tha_nut_12 = 0;}
if((GT_button_tru_11 == 1) && (khong_lap_11 == 0)){
dk_kc = EEPROM.read(39); khong_lap_11 = 1; sau_tha_nut_11 = 1;
if(dk_kc == 6){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 5); delay(5); n_khoang_cach = 450;
for(int i = 0; i < 5; i++){digitalWrite(coi_chip_5,HIGH);delay(600); digitalWrite(coi_chip_5,LOW);delay(1000);}}
if(dk_kc == 5){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 4); delay(5); n_khoang_cach = 400;
for(int i = 0; i < 4; i++){digitalWrite(coi_chip_5,HIGH);delay(600); digitalWrite(coi_chip_5,LOW);delay(1000);}}
if(dk_kc == 4){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 3); delay(5); n_khoang_cach = 350;
for(int i = 0; i < 3; i++){digitalWrite(coi_chip_5,HIGH);delay(600); digitalWrite(coi_chip_5,LOW);delay(1000);}}
if(dk_kc == 3){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 2); delay(5); n_khoang_cach = 300;
for(int i = 0; i < 2; i++){digitalWrite(coi_chip_5,HIGH);delay(600); digitalWrite(coi_chip_5,LOW);delay(1000);}}
if(dk_kc == 2){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 1); delay(5); n_khoang_cach = 250;
for(int i = 0; i < 1; i++){digitalWrite(coi_chip_5,HIGH);delay(600); digitalWrite(coi_chip_5,LOW);delay(1000);}}
if(dk_kc == 1){delay(5); EEPROM.write(39, 0); delay(10); EEPROM.write(39, 0); delay(5); n_khoang_cach = 200;}
}
if((GT_button_tru_11 == 0) && (sau_tha_nut_11 == 1)){khong_lap_11 = 0; sau_tha_nut_11 = 0;}
}
}
if((GT_button_cong_12 == 1) && (khong_lap_12 == 0)){ do_rung = EEPROM.read(12); do_am = EEPROM.read(18); khong_lap_12 = 1; sau_tha_nut_12 = 1;
if((do_rung <= 240) && (che_do_rung == 0)){delay(5); EEPROM.write(12, 0); delay(10); EEPROM.write(12, do_rung + 10);}
if((do_am <= 245) && (che_do_rung == 1)){delay(5); EEPROM.write(18, 0); delay(10); EEPROM.write(18, do_am + 10);}
delay(5);
do_rung = EEPROM.read(12);
do_am = EEPROM.read(18);
}
if((GT_button_cong_12 == 0) && (sau_tha_nut_12 == 1)){khong_lap_12 = 0; sau_tha_nut_12 = 0;}
if((GT_button_tru_11 == 1) && (khong_lap_11 == 0)){ do_rung = EEPROM.read(12); do_am = EEPROM.read(18); khong_lap_11 = 1; sau_tha_nut_11 = 1;
if((do_rung >= 70) && (che_do_rung == 0)){delay(5); EEPROM.write(12, 0); delay(10); EEPROM.write(12, do_rung - 10);}
if((do_am >= 15) && (che_do_rung == 1)){delay(5); EEPROM.write(18, 0); delay(10); EEPROM.write(18, do_am - 10);}
delay(5);
do_rung = EEPROM.read(12);
do_am = EEPROM.read(18);
}
if((GT_button_tru_11 == 0) && (sau_tha_nut_11 == 1)){khong_lap_11 = 0; sau_tha_nut_11 = 0;}
GT_button = digitalRead(button_10);
if (GT_button != GT_button_000)
{ GT_button_000 = GT_button;
pe_nhanli = millis();
}
if ((GT_button == 1) && (millis() - pe_nhanli > 3000)){analogWrite(coi_chip_5,do_am); digitalWrite(led_4,HIGH);
analogWrite(ngon_tay_cai_6,do_rung); delay(500); analogWrite(coi_chip_5,0);
delay(2500); digitalWrite(mosfet_9,LOW); delay(60000);}
GT_button = digitalRead(button_10);
if((GT_button == 1) && (khong_lap == 0)){ khong_lap = 1; sau_tha_nut = 1; }
if((GT_button == 0) && (sau_tha_nut == 1)){sau_tha_nut = 0; khong_lap = 0; che_do_rung = EEPROM.read(6);delay(10);
if(che_do_rung == 1){delay(50); EEPROM.write(6, 0); delay(50);}
else{delay(50); EEPROM.write(6, 1); delay(50); }
che_do_rung = EEPROM.read(6); }
delay(50);
digitalWrite(trig,0);
delayMicroseconds(2);
digitalWrite(trig,1);
delayMicroseconds(5);
digitalWrite(trig,0);
thoi_gian = pulseIn(echo,HIGH);
khoang_cach = int(thoi_gian/2/29.412);
if(che_do_rung == 0){
if(millis() - pe_loa1 >= 50){analogWrite(coi_chip_5,0);}
if(khoang_cach <= n_khoang_cach){ time_x = (4 * khoang_cach);
if((rung_cai_1 == 0) && (millis() - pe_rung_cai_2 >= time_x)){pe_rung_cai_1 = millis();analogWrite(ngon_tay_cai_6,do_rung); rung_cai_1 = 1; rung_cai_2 = 0; }
if((rung_cai_2 == 0) && (millis() - pe_rung_cai_1 >= 100)) {pe_rung_cai_2 = millis();analogWrite(ngon_tay_cai_6,0); rung_cai_2 = 1; rung_cai_1 = 0; }
}
else{analogWrite(ngon_tay_cai_6,0); }
}
else{
analogWrite(ngon_tay_cai_6,0);
if(khoang_cach <= n_khoang_cach){ time_x = (2 * khoang_cach) + (khoang_cach / 2);
if((loa1 == 0) && (millis() - pe_loa2 >= time_x)){pe_loa1 = millis();analogWrite(coi_chip_5,do_am); loa1 = 1; loa2 = 0; }
if((loa2 == 0) && (millis() - pe_loa1 >= 50)) {pe_loa2 = millis();analogWrite(coi_chip_5,0); loa2 = 1; loa1 = 0; }
}
else{if(millis() - pe_loa1 >= 30){analogWrite(coi_chip_5,0);}}
}
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.