#include <LiquidCrystal_I2C.h>
#include <Bounce2.h>
LiquidCrystal_I2C lcd(0x27, 20,4);
byte customChar[] = {B00100,B01110,B01110,B01110,B11111,B11111,B01110,B00000};
byte customChar1[] = {B00000,B10101,B01110,B11111,B01110,B10101,B00000,B00000};
byte customChar2[] = {B00010,B00101,B00010,B01100,B10000,B10000,B10000,B01100};
byte customChar3[] = {B00000,B00100,B01110,B11111,B00100,B00100,B00100,B00000};
byte customChar4[] = {B00100,B00100,B00100,B11111,B11111,B11111,B11111,B11111};
byte customChar5[] = {B00000,B00000,B00000,B11100,B11100,B11100,B11000,B10000};
byte customChar6[] = {B00000,B00000,B00000,B00111,B00111,B00111,B00011,B00001};
byte customChar7[] = {B01110,B11011,B10001,B11011,B01110,B00100,B01110,B10101};
byte customChar8[] = {B00000,B01010,B11111,B11111,B01110,B00100,B00000,B00000};
const int startp = A0;
const int helpp = A1;
const int contp = 10;
const int minusPin = 4;
const int plusPin = 3;
const int okPin = 5;
int light = 60;
int temp = 20;
int hum = 50;
int start = 0;
int health = 100;
int menu = 1;
int lose = 0;
int help = 0;
int next = 0;
int choose = 1;
int ichoose = 0;
int hmany = 0;
unsigned long lastUpdate = 0;
unsigned long lastUpdate2 = 0;
unsigned long lastRecordUpdate = 0;
unsigned long recordTime = 0;
unsigned long currentTime = 0;
unsigned long lastRecord = 0;
Bounce debouncer1 = Bounce(); // для startp
Bounce debouncer2 = Bounce(); // для helpp
Bounce debouncer3 = Bounce(); // для contp
Bounce debouncerPlus = Bounce(); // для plusPin
Bounce debouncerMinus = Bounce(); // для minusPin
Bounce debouncerOk = Bounce(); // для okPin
void setup() {
Serial.begin(9600);
lcd.begin(20, 4);
lcd.backlight();
//Вологість
lcd.createChar(0, customChar);
//Освітлення
lcd.createChar(1, customChar1);
//Градуси
lcd.createChar(2, customChar2);
//Стрілка для вибору
lcd.createChar(3, customChar3);
//Вазонок з квіткою
lcd.createChar(4, customChar4);
lcd.createChar(5, customChar5);
lcd.createChar(6, customChar6);
lcd.createChar(7, customChar7);
pinMode(startp, INPUT_PULLUP);
pinMode(helpp, INPUT_PULLUP);
pinMode(contp, INPUT_PULLUP);
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(okPin, INPUT_PULLUP);
pinMode(minusPin, INPUT_PULLUP);
pinMode(plusPin, INPUT_PULLUP);
debouncer1.attach(startp);
debouncer1.interval(5);
debouncer2.attach(helpp);
debouncer2.interval(5);
debouncer3.attach(contp);
debouncer3.interval(5);
debouncerPlus.attach(plusPin);
debouncerPlus.interval(5);
debouncerMinus.attach(minusPin);
debouncerMinus.interval(5);
debouncerOk.attach(okPin);
debouncerOk.interval(5);
digitalWrite(11, HIGH);
lcd.setCursor(0,1);
lcd.print("Plant Simulator");
next = 1;
}
void menuv() {
digitalWrite(11, LOW);
lcd.clear();
lcd.setCursor(7,1);
lcd.print("Start");
lcd.setCursor(7,3);
lcd.print("Help");
digitalWrite(13, HIGH);
digitalWrite(12, HIGH);
next = 0;
help = 0;
lose = 0;
health = 100;
lastRecord = recordTime;
}
void helpv() {
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Open your console");
digitalWrite(13, LOW);
digitalWrite(12, LOW);
digitalWrite(11, HIGH);
Serial.println("Використовуйте 3 кнопки під екраном щоб вибрати полив, температуру, вологість, вам потрібно ставити рекомендованні значення (Вологість 40% до 60%, Температура 15°C до 30°C, Освітлення 50% до 75%) щоб ваша рослина смогла вижити про це буде свідчити її здоров'я поцентру сверху, також ви можете використовувати кнопки зліва для деяких дій, коли світлодіоти біля них будуть підсвічені, то кнопка може бути використана, також 1. Вологість, 2. Температура, 3.Освітлення, це кінець туторіалу.");
Serial.println("");
Serial.println("Нажміть на жовту кнопку щоб повернутись в меню.");
help = 1;
next = 1;
}
void refresh(){
lcd.clear();
lcd.setCursor(7, 0);
lcd.print(health);
lcd.setCursor(0,2);
lcd.write(0);
lcd.setCursor(1,2);
lcd.print(hum);
lcd.setCursor(5,2);
lcd.write(2);
lcd.setCursor(6,2);
lcd.print(temp);
lcd.setCursor(10,2);
lcd.write(1);
lcd.setCursor(11,2);
lcd.print(light);
lcd.setCursor(16,2);
lcd.write(6);
lcd.setCursor(17,2);
lcd.write(4);
lcd.setCursor(18,2);
lcd.write(5);
lcd.setCursor(17,1);
lcd.write(7);
if(choose == 1){
lcd.setCursor(2, 3);
}
else if(choose == 2){
lcd.setCursor(7, 3);
}
else if(choose == 3){
lcd.setCursor(12, 3);
}
lcd.write(3);
}
void updateHealth() {
if (currentTime - lastUpdate2 >= 500) {
if (hum < 40 || hum > 60 || temp < 15 || temp > 30 || light < 50 || light > 75) {
health -= 5;
if (health <= 0) {
start = 0;
menu = 1;
lose = 1;
next = 1;
digitalWrite(11, HIGH);
lcd.clear();
delay(100);
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Game Over!");
lcd.setCursor(0, 2);
lcd.print(recordTime);
if(recordTime > lastRecord){
lcd.setCursor(0,3);
lcd.print("New Record!");
}
}
}
lastUpdate2 = currentTime;
}
}
void loop() {
currentTime = millis();
if(start == 0 && menu == 1){
debouncer1.update();
debouncer2.update();
debouncer3.update();
if(menu == 1 && next == 1 && debouncer3.fell()){
menuv();
}
if(lose == 1 && next == 1 && debouncer3.fell()){
menuv();
}
if(help == 0 && menu == 1 && debouncer2.fell()){
helpv();
}
if(start == 0 && menu == 1 && debouncer1.fell()){
recordTime = 0;
choose = 1;
ichoose = 0;
light = 60;
temp = 20;
hum = 50;
menu = 0;
start = 1;
lcd.clear();
lcd.setCursor(0,1);
lcd.print("Starting the game..");
delay(1000);
refresh();
lcd.setCursor(2,3);
lcd.write(3);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
lastUpdate = currentTime;
}
}
if(start == 1 && menu == 0){
debouncerPlus.update();
debouncerMinus.update();
debouncerOk.update();
debouncer3.update();
updateHealth();
if (currentTime - lastUpdate >= 1000) {
hmany -= 1;
int parameter = random(1, 4);
int change = 0;
if(hmany <= 0){
hmany = random(1,6);
change = random(0, 2);
}
switch (parameter) {
case 1: // Вологість
if (change == 0) {
hum -= 1;
} else {
hum += 1;
}
break;
case 2: // Температура
if (change == 0) {
temp -= 1;
} else {
temp += 1;
}
break;
case 3: // Освітлення
if (change == 0) {
light -= 1;
} else {
light += 1;
}
break;
}
refresh();
lastUpdate = currentTime;
}
//Вийти з вибору вологості
if(next == 1 && debouncer3.fell() && ichoose == 1){
digitalWrite(11, LOW);
ichoose = 0;
next = 0;
refresh();
if(choose == 1){
lcd.setCursor(2, 3);
}
else if(choose == 2){
lcd.setCursor(7, 3);
}
else if(choose == 3){
lcd.setCursor(12, 3);
}
lcd.write(3);
}
//Піти вправо
if (ichoose == 0 && debouncerPlus.fell()) {
if(choose < 3){
choose += 1;
}
refresh();
if (choose == 1) {
lcd.setCursor(2, 3);
} else if (choose == 2) {
lcd.setCursor(7, 3);
} else if (choose == 3) {
lcd.setCursor(12, 3);
}
lcd.write(3);
}
//Піти вліво
if (ichoose == 0 && debouncerMinus.fell()) {
if(choose > 1){
choose -= 1;
}
refresh();
if (choose == 1) {
lcd.setCursor(2, 3);
} else if (choose == 2) {
lcd.setCursor(7, 3);
} else if (choose == 3) {
lcd.setCursor(12, 3);
}
lcd.write(3);
}
//Вибрати вологість
if(debouncerOk.fell() && choose == 1){
ichoose = 1;
next = 1;
digitalWrite(11, HIGH);
}
//Відняти вологість
if(debouncerMinus.fell() && ichoose == 1 && choose == 1){
hum -= 1;
refresh();
}
//Додати вологість
if(debouncerPlus.fell() && ichoose == 1 && choose == 1){
hum += 1;
refresh();
}
//Вибрати температуру
if(debouncerOk.fell() && choose == 2){
ichoose = 1;
next = 1;
digitalWrite(11, HIGH);
}
//Відняти температуру
if(debouncerMinus.fell() && ichoose == 1 && choose == 2){
temp -= 1;
refresh();
}
//Додати температуру
if(debouncerPlus.fell() && ichoose == 1 && choose == 2){
temp += 1;
refresh();
}
//Вибрати світло
if(debouncerOk.fell() && choose == 3){
ichoose = 1;
next = 1;
digitalWrite(11, HIGH);
}
//Відняти світло
if(debouncerMinus.fell() && ichoose == 1 && choose == 3){
light -= 1;
refresh();
}
//Додати світло
if(debouncerPlus.fell() && ichoose == 1 && choose == 3){
light += 1;
refresh();
}
if (start == 1 && health > 0) {
if (currentTime - lastRecordUpdate >= 1000) {
recordTime += 1;
lastRecordUpdate = currentTime;
}
}
}
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
led2:A
led2:C
led3:A
led3:C
r1:1
r1:2
r2:1
r2:2
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
led1:A
led1:C
r3:1
r3:2
btn7:1.l
btn7:2.l
btn7:1.r
btn7:2.r
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
btn6:1.l
btn6:2.l
btn6:1.r
btn6:2.r