#include <EEPROM.h>
const int trig = A1;
const int echo = A0;
byte ngon_tay_cai_6 = 6;
byte ngon_tay_ut_3 = 3;
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 khoa_bao_dong = 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;
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;
void tien_sacpin (unsigned long giay)
{
unsigned long peham_sac = millis();
while((millis() - peham_sac <= giay) && (dovolt_out >= 650) && (khoa_bao_dong == 0)){
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();
}
if ((GT_button_cong_12 == 1) && (GT_button_tru_11 == 1) && (millis() - pe_nhanli_12 > 3000) && (millis() - pe_nhanli_11 > 3000)){khoa_bao_dong = 1; tat = 0;}
}
}
void tien_sacpin_nghi (unsigned long giay)
{
unsigned long peham_sac = millis();
while((millis() - peham_sac <= giay) && (dovolt_out >= 650) && (khoa_bao_dong == 0)){
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();
}
if ((GT_button_cong_12 == 1) && (GT_button_tru_11 == 1) && (millis() - pe_nhanli_12 > 3000) && (millis() - pe_nhanli_11 > 3000)){khoa_bao_dong = 1; tat = 0;}
}
}
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(ngon_tay_ut_3,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);
}
void loop()
{
if(millis() - pe_volt_battery >= 60000){
pe_volt_battery = millis();
digitalWrite(led_4,LOW);
analogWrite(ngon_tay_cai_6,0);
analogWrite(ngon_tay_ut_3,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);
analogWrite(ngon_tay_ut_3,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) or (khoa_bao_dong == 1)){
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();
}
if ((khoa_bao_dong == 1) or ((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);
analogWrite(ngon_tay_ut_3,0);
digitalWrite(led_4,HIGH);
for(int i = 0; i < 3; i++){digitalWrite(coi_chip_5,HIGH);delay(1500); digitalWrite(coi_chip_5,LOW);delay(1000);} delay(30000);
unsigned long so_giay = millis();
while(millis() - so_giay <= 10000){
delay(50);
digitalWrite(trig,0);
delayMicroseconds(2);
digitalWrite(trig,1);
delayMicroseconds(5);
digitalWrite(trig,0);
thoi_gian = pulseIn(echo,HIGH);
khoang_cach = thoi_gian/2/29.412;
tong_khoang_cach = tong_khoang_cach + khoang_cach;
so_lan_do = so_lan_do + 1;
}
khoang_cach = tong_khoang_cach / so_lan_do;
while(tat == 0){
dovolt_out = analogRead(volt_cam_sac_A2);
if(millis() - pe_volt_battery >= 60000){
pe_volt_battery = millis();
digitalWrite(led_4,LOW);
digitalWrite(chan_sac_8,LOW);
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) && (dovolt_out < 650)){digitalWrite(mosfet_9,LOW); delay(30000);}
}
if((dovolt_out >= 650) && (volt_pin <= 4.07)){digitalWrite(chan_sac_8,HIGH);}
else{digitalWrite(chan_sac_8,LOW);}
if((den1 == 0) && (millis() - pe12 >= 1500)){ digitalWrite(led_4,HIGH); den1 = 1; den2 = 0; pe11 = millis(); }
if((den2 == 0) && (millis() - pe11 >= 1500)){ digitalWrite(led_4,LOW); den2 = 1; den1 = 0; pe12 = millis(); }
delay(50);
digitalWrite(trig,0);
delayMicroseconds(2);
digitalWrite(trig,1);
delayMicroseconds(5);
digitalWrite(trig,0);
thoi_gian = pulseIn(echo,HIGH);
khoang_cach_bao_dong = thoi_gian/2/29.412;
if(khoang_cach_bao_dong >= khoang_cach){hieu_so = khoang_cach_bao_dong - khoang_cach; }
if(khoang_cach_bao_dong < khoang_cach) {hieu_so = khoang_cach - khoang_cach_bao_dong; }
if(hieu_so >= 50){ hieu_so_bd = hieu_so_bd + 1; pe_hieu_so = millis(); }
if( millis() - pe_hieu_so >= 5000){ hieu_so_bd = 0; }
if(hieu_so_bd >= 5){ unsigned long so_giay_baodong = millis();
while(millis() - so_giay_baodong <= 60000){
if((loa1 == 0) && (millis() - pe_loa2 >= 150)){pe_loa1 = millis();digitalWrite(coi_chip_5,HIGH); digitalWrite(led_4,HIGH); loa1 = 1; loa2 = 0; }
if((loa2 == 0) && (millis() - pe_loa1 >= 150)){pe_loa2 = millis();digitalWrite(coi_chip_5,LOW); digitalWrite(led_4,LOW); loa2 = 1; loa1 = 0; }
}
digitalWrite(mosfet_9,LOW); delay(30000);
}
}
}
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); analogWrite(ngon_tay_ut_3,do_rung); delay(500); analogWrite(coi_chip_5,0);
delay(5000); digitalWrite(mosfet_9,LOW); delay(30000);}
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(khoang_cach <= 130) {rung_cai = 1; rung_ut = 0; pe_rung = millis();}
if((khoang_cach > 130) && (khoang_cach <= 280)){rung_cai = 0; rung_ut = 1; pe_rung = millis();}
else{if(millis() - pe_rung >= 1000){rung_cai = 0; rung_ut = 0;}}
if(che_do_rung == 0){
if(millis() - pe_loa1 >= 50){analogWrite(coi_chip_5,0);}
if(rung_cai == 1){if((rung_cai_1 == 0) && (millis() - pe_rung_cai_2 >= 100)){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 >= 300)){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); }
if(rung_ut == 1){if((rung_ut_1 == 0) && (millis() - pe_rung_ut_2 >= 100)){pe_rung_ut_1 = millis();analogWrite(ngon_tay_ut_3,do_rung); rung_ut_1 = 1; rung_ut_2 = 0; }
if((rung_ut_2 == 0) && (millis() - pe_rung_ut_1 >= 300)){pe_rung_ut_2 = millis();analogWrite(ngon_tay_ut_3,0); rung_ut_2 = 1; rung_ut_1 = 0; }
}
else{analogWrite(ngon_tay_ut_3,0); }
}
else{
analogWrite(ngon_tay_cai_6,0);
analogWrite(ngon_tay_ut_3,0);
if(khoang_cach <= 300){ 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.