09-04-2019, 11:14
Witam,
mam problem z przerobieniem kodu menu na buttony. chodzi o to by do sterowania IR dodać sterowanie przyciskami tylko by były użyte 4 przyciski "back, next, ok, prev".
mam problem z przerobieniem kodu menu na buttony. chodzi o to by do sterowania IR dodać sterowanie przyciskami tylko by były użyte 4 przyciski "back, next, ok, prev".
Kod:
#include "DHT.h"
#include <IRremote.h>
#include <IRremoteInt.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define VERSION "v2"
LiquidCrystal_I2C lcd(0x27, 2,1,0,4,5,6,7);
#define WATERPUMP 10
#define BACKLIGHT_PIN 3
#define RECV_PIN 8
IRrecv irrecv(RECV_PIN);
decode_results results;
// pilot codes
#define CH1 0xE318261B
#define CH 0x511DBB
#define CH2 0xEE886D7F
#define EQ 0xE5CFBD7F
#define PLAY 0x20FE4DBB
#define BTN8 0x1BC0157B
#define BPREV 0x52A3D41F
#define BNEXT 0xD7E84B1B
#define BMINUS 0xF076C13B
#define BPLUS 0xA3C8EDDB
// oswietlenie
#define LEDS 9
// temperatura
#define TEMPERATURE 13
#define DHTPIN 8 //Czujnik podłączony do cyfrowego pinu 8
#define DHTTYPE DHT11 //Definicja typu czujnika
DHT dht(DHTPIN, DHTTYPE); //Definicja czujnika, typ i numer pinu
// wilgotnosc
int wilgotnosc = 51;
int jasnosc = 90;
bool swiatlo;
// minimalne temperatury od ktorych zaczyna dzialac zraszacz i grzalka
int mintemp = 20;
int minwilg = 49;
int stats() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Aktualne nastawy:");
delay(2000);
lcd.setCursor(0, 0);
lcd.print("Min. temp: "); lcd.print(mintemp); lcd.print(" C ");
lcd.setCursor(0, 1);
lcd.print("Min. wilg: "); lcd.print(minwilg); lcd.print(" % ");
delay(4000);
}
int heater(int type) {
if (type == 0) {
// tutaj wylacz grzanie
return 0;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Niska temperatura");
lcd.setCursor(0, 1);
lcd.print("Nagrzewnica wl.");
delay(2500);
// tutaj wlacz grzanie :)
return 0;
}
int waterpump(int type) {
int i = 1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Zraszanie");
lcd.setCursor(0, 1);
lcd.print("Zaczekaj chwile...");
Serial.println("Zraszanie rozpoczÄte.");
delay(50);
digitalWrite(WATERPUMP, HIGH);
delay(5000);
digitalWrite(WATERPUMP, LOW);
delay(1);
wilgotnosc+=25;
}
int lights(int type) {
if (type == 1) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Oswietlenie wl.");
digitalWrite(LEDS, jasnosc);
swiatlo = !swiatlo;
// wlaczone1
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Oswietlenie wyl.");
digitalWrite(LEDS, LOW);
swiatlo = !swiatlo;
// wylaczone
}
delay(1000);
return 0;
}
void setup() {
lcd.begin(16, 2);
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(HIGH);
lcd.display();
lcd.print("TERRARIUM v1");
lcd.setCursor(0, 1);
for (int i = 1; i <= 10; i+=1) {
lcd.setCursor(0, 1);lcd.print("Inicjalizacja.");
delay(150);
lcd.setCursor(0, 1);lcd.print("Inicjalizacja..");
delay(150);
lcd.setCursor(0, 1);lcd.print("Inicjalizacja...");
delay(150);
lcd.setCursor(0, 1);lcd.print(" ");
}
irrecv.enableIRIn();
pinMode(WATERPUMP, OUTPUT);
pinMode(LEDS, OUTPUT);
Serial.begin(9600);
dht.begin();
lcd.clear();
}
int menu() {
int opcja = 1;
String menustr[7] = { "NULL", "Oswietlenie", "Jasnosc", "Grzanie", "Wiglotnosc", "Zraszacz ", "Wyjdz " };
lcd.clear();
lcd.setCursor(0, 0); lcd.print(">");
lcd.setCursor(2, 0); lcd.print(menustr[1]);
lcd.setCursor(2, 1); lcd.print(menustr[2]);
if (opcja == 1 || opcja == 2) {
lcd.setCursor(14, 0); if (swiatlo) lcd.print("ON"); else lcd.print("OF");
lcd.setCursor(14, 1); lcd.print(jasnosc);
}
for (;;) {
if (irrecv.decode(&results)) {
delay(50);
if (results.value == EQ) return 1;
if (results.value == BNEXT) {
opcja+=1;
lcd.setCursor(0, 0); lcd.print(" ");
lcd.setCursor(0, 1); lcd.print(">");
if (opcja == 1 || opcja == 2) {
lcd.setCursor(14, 0); if (swiatlo) lcd.print("ON"); else lcd.print("OF");
lcd.setCursor(13, 1); lcd.print(jasnosc);
}
if (opcja == 3 || opcja == 5) {
lcd.clear();
lcd.setCursor(0, 0); lcd.print(">");
lcd.setCursor(2, 0); lcd.print(menustr[opcja]);
lcd.setCursor(2, 1); lcd.print(menustr[opcja+1]);
if (opcja == 3 || opcja == 4) {
lcd.setCursor(14, 0); lcd.print(mintemp);
lcd.setCursor(14, 1); lcd.print(minwilg);
}
}
if (opcja >= 6) opcja = 6;
}
if (results.value == BPREV) {
opcja-=1;
lcd.setCursor(0, 0); lcd.print(">");
lcd.setCursor(0, 1); lcd.print(" ");
if (opcja == 1 || opcja == 2) {
lcd.setCursor(14, 0); if (swiatlo) lcd.print("ON"); else lcd.print("OF");
lcd.setCursor(13, 1); lcd.print(jasnosc);
}
if (opcja == 2 || opcja == 4) {
lcd.clear();
lcd.setCursor(0, 1); lcd.print(">");
lcd.setCursor(2, 0); lcd.print(menustr[opcja-1]);
lcd.setCursor(2, 1); lcd.print(menustr[opcja]);
if (opcja == 3 || opcja == 4) {
lcd.setCursor(14, 0); lcd.print(mintemp);
lcd.setCursor(14, 1); lcd.print(minwilg);
};
if (opcja == 1 || opcja == 2) {
lcd.setCursor(14, 0); if (swiatlo) lcd.print("ON"); else lcd.print("OF");
lcd.setCursor(13, 1); lcd.print(jasnosc);
}
}
if (opcja <= 1) opcja = 1;
}
if (results.value == BPLUS && opcja == 2) {
jasnosc+=5;
if (jasnosc >= 255) jasnosc = 255;
lcd.setCursor(13, 1); lcd.print(jasnosc); lcd.print(" ");
analogWrite(LEDS, jasnosc);
}
if (results.value == BMINUS && opcja == 2) {
jasnosc-=5;
if (jasnosc <= 5) jasnosc = 5;
lcd.setCursor(13, 1); lcd.print(jasnosc); lcd.print(" ");
analogWrite(LEDS, jasnosc);
}
if (results.value == BPLUS && opcja == 3) {
mintemp+=1;
if (mintemp >= 60) mintemp = 60;
lcd.setCursor(14, 0); lcd.print(mintemp);
}
if (results.value == BMINUS && opcja == 3) {
mintemp-=1;
if (mintemp <= 1) mintemp = 1;
lcd.setCursor(14, 0); lcd.print(mintemp);
}
if (results.value == BMINUS && opcja == 4) {
minwilg-=1;
if (minwilg <= 1) minwilg = 1;
lcd.setCursor(14, 1); lcd.print(minwilg);
}
if (results.value == BPLUS && opcja == 4) {
minwilg+=1;
if (minwilg >= 90) minwilg = 90;
lcd.setCursor(14, 1); lcd.print(minwilg);
}
if (results.value == PLAY) {
Serial.print("OPCJA: "); Serial.println(opcja);
switch (opcja) {
case 1: { if (swiatlo) lights(0); else lights(1); return 0; break; }
case 5: { waterpump(1); return 0; break; }
case 6: { return 0; break; }
}
}
irrecv.resume();
} // if
} //for
}
void info() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TERRARIUM "); lcd.print(VERSION);
lcd.setCursor(0, 1);
lcd.print("by Solaris");
delay(2000);
}
void loop() {
dht.readTemperature();
if (irrecv.decode(&results))
{
//Serial.println(results.value, HEX);
switch (results.value) {
case CH: { menu(); break; }
case EQ: { stats(); break; }
case BTN8: { info(); break; }
}
irrecv.resume(); // Receive the next value
}
delay(1);
// losowe, na chwile :)
//if (random(10) == 3) wilgotnosc = random(49,52);
lcd.setCursor(0, 0);
lcd.print("Temperatura:"); lcd.print(dht.readTemperature(0),1); lcd.print(" C ");
lcd.setCursor(0, 1);
lcd.print("Wilgotnosc:"); lcd.print(dht.readHumidity(0),1); lcd.print("% ");
// nastawy
if (dht.readTemperature(0) < mintemp) heater(1); else heater(0);
if (dht.readHumidity(0) < minwilg) waterpump(1);
delay(1);
}