#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define tr 17
#define ec 16
#define pb1 25
#define pb2 26
#define pb3 27
#define pb4 14
#define pb5 12
#define led1 33
int old1, new1, old2, new2, old3, new3, old4, new4, old5, new5;
int dis;
int work, between, a, b;
bool stop_pb1 = true, start_pb2 = false, start_pb3 = false, start_pb4 = false;
unsigned long t1, t2;
void setup() {
Serial.begin(115200);
pinMode(tr, OUTPUT);
pinMode(ec, INPUT);
pinMode(led1, OUTPUT);
pinMode(pb1, INPUT_PULLUP);
pinMode(pb2, INPUT_PULLUP);
pinMode(pb3, INPUT_PULLUP);
pinMode(pb4, INPUT_PULLUP);
pinMode(pb5, INPUT_PULLUP);
t1 = millis();
t2 = millis();
lcd.init();
lcd.backlight();
old1 = digitalRead(pb1);
old2 = digitalRead(pb2);
old3 = digitalRead(pb3);
old4 = digitalRead(pb4);
old5 = digitalRead(pb5);
digitalWrite(led1, 0);
show();
work = 100;
between = 200;
a = 1;
b = 1;
}
void loop() {
if (millis() - t1 >= 250) {
new1 = digitalRead(pb1);
new2 = digitalRead(pb2);
new3 = digitalRead(pb3);
new4 = digitalRead(pb4);
new5 = digitalRead(pb5);
lcdcount();
dis = getDitDistance(tr, ec);
lcdDist();
Countup();
if (old1 != new1 && new1 == 0 && stop_pb1 == false) {
lcd.clear();
stop_pb1 = true;
start_pb2 = false;
start_pb3 = false;
start_pb4 = false;
show();
}
if (old2 != new2 && new2 == 0 && stop_pb1 == true && start_pb2 == false && start_pb3 == false && start_pb4 == false) {
stop_pb1 = false;
start_pb2 = true;
lcd.clear();
}
if (old3 != new3 && new3 == 0 && stop_pb1 == true && start_pb2 == false && start_pb3 == false && start_pb4 == false) {
stop_pb1 = false;
start_pb3 = true;
lcd.clear();
}
if (old4 != new4 && new4 == 0 && stop_pb1 == true && start_pb2 == false && start_pb3 == false && start_pb4 == false) {
stop_pb1 = false;
start_pb4 = true;
lcd.clear();
}
if (dis >= work && dis <= between) {
digitalWrite(led1, 1);
}
else if (dis < work || dis > between) {
digitalWrite(led1, 0);
}
old1 = new1;
old2 = new2;
old3 = new3;
old4 = new4;
old5 = new5;
}
}
float getDitDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
float duration = pulseIn(echoPin, HIGH);
float dist = (duration * 0.0343) / 2;
return dist;
delay(100);
}
void show() {
lcd.setCursor(0, 0);
lcd.print("2.dist");
lcd.setCursor(7, 0);
lcd.print("3.count");
lcd.setCursor(0, 1);
lcd.print("4.Count up");
}
void lcdDist() {
if (start_pb2 == true) {
lcd.setCursor(0, 0);
lcd.print("Distance:");
lcd.setCursor(10, 0);
lcd.print(dis);
lcd.setCursor(14, 0);
lcd.print("cm");
lcd.setCursor(6, 1);
lcd.print("1.<<");
if (millis() - t2 >= 1000) {
lcd.setCursor(0, 0);
lcd.print("Distance:");
lcd.setCursor(10, 0);
lcd.print(" ");
lcd.setCursor(14, 0);
lcd.print("cm");
t2 = millis();
}
}
delay(250);
}
void lcdcount() {
unsigned long t3;
if (start_pb3 == true) {
if (old2 != new2 && new2 == 0 && work < 200) {
work += a;
}
if (old3 != new3 && new3 == 0 && work > 100) {
work -= a;
}
if (old4 != new4 && new4 == 0 && between < 400) {
between += b;
}
if (old5 != new5 && new5 == 0 && between > 200) {
between -= b;
}
lcd.setCursor(0, 0);
lcd.print("work");
lcd.setCursor(5, 0);
lcd.print(work);
lcd.setCursor(9, 0);
lcd.print("cm");
lcd.setCursor(0, 1);
lcd.print("between");
lcd.setCursor(8, 1);
lcd.print(between);
lcd.setCursor(12, 1);
lcd.print("cm");
lcd.setCursor(12, 0);
lcd.print("1.<<");
}
delay(100);
}
void Countup() {
if (start_pb4 == true) {
if (old2 != new2 && new2 == 0 && a < 5) {
a++;
}
if (old3 != new3 && new3 == 0 && a > 1) {
a--;
}
if (old4 != new4 && new4 == 0 && b < 5) {
b++;
}
if (old5 != new5 && new5 == 0 && b > 1) {
b--;
}
lcd.setCursor(0, 0);
lcd.print("work");
lcd.setCursor(5, 0);
lcd.print(a);
lcd.setCursor(0, 1);
lcd.print("between");
lcd.setCursor(8, 1);
lcd.print(b);
lcd.setCursor(12, 1);
lcd.print("1.<<");
}
delay(250);
}