Ey, I have an ARDUINO UNO with an OLED Display I2C SH1106 (128*32). I coded a OLED MENU with only one pressbutton (using oneButton Library for that)
The code is for AUTOMATIC CONTROL PLANT.
When i press "INICIAR CICLO" i verify if TYPE, FASE, and COOLER has a value different to the initial value 'N/A'. IF Yes, start a CONTROL method. If NOT,
only show an error screen.
The first time runs ok, and show ok the info page. But after x time, i refresh the info page and i lost ONLY TO SHOW the selected data (But it still runs the CONTROL method)
I need to use the selected data always till someone change it, but if change only TYPE (or one of the values), i need to reuse the first selected values for FASE and COOLER.
attached the code
#include <DHT.h>
#include <DHT_U.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <OneButton.h>
#define RELE_ONE 2
#define RELE_TWO 3
#define RELE_THREE 4
#define humidity_sensor A0
#define temperature_sensor 5
OneButton button(A1, true);
DHT dht(temperature_sensor, DHT22);
Adafruit_SH1106 display(-1);
int pic = 0;
String FASE = "N/A";
String TYPE = "N/A";
String COOLER = "N/A";
boolean start = false;
boolean reload = true;
long lastmillis = 0;
long maxtime = 5000;
int event = 0;
const int light_control_auto = 6000, light_control_reg = 8000, temperature_control_growing = 2000, temperature_control_flowering = 4000, MAX_TEMP = 22, MIN_HUM = 500, humidity_control_growing = 5000, humidity_control_flowering = 7000;
int temperature_control, humidity_control, light_control;
int temperature_measure_sensor, humidity_measure_sensor;
void setup() {
dht.begin();
display.begin(SH1106_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.clearDisplay();
button.attachDoubleClick(doubleclick);
button.attachClick(singleClick);
button.attachLongPressStart(longPressStart);
button.attachLongPressStop(longPressStop);
pinMode(humidity_sensor, INPUT);
pinMode(RELE_ONE, OUTPUT);
pinMode(RELE_TWO, OUTPUT);
pinMode(RELE_THREE, OUTPUT);
digitalWrite(RELE_ONE, HIGH);
digitalWrite(RELE_TWO, HIGH);
digitalWrite(RELE_THREE, HIGH);
}
void loop() {
button.tick();
if (reload) {
menu();
}
if (millis() >= (lastmillis + maxtime))
{
pic = 0;
}
if (start) {
control();
}
}
void control() {
unsigned long TIME = millis();
int light_event = 0;
int humidity_event = 0;
int temperature_measure_sensor = dht.readTemperature();
int humidity_measure_sensor = analogRead(humidity_sensor);
int temperature_event = 0;
boolean isCooling = false;
boolean isIrrigation = false;
//control de humedad
if (TIME - humidity_event >= humidity_control || isIrrigation) {
humidity_event = TIME;
if (humidity_measure_sensor > MIN_HUM) {
digitalWrite(RELE_TWO, LOW);
isIrrigation = true;
} else {
digitalWrite(RELE_TWO, HIGH);
isIrrigation = false;
}
}
// control de los ventiladores
if (temperature_control != 0 && (TIME - temperature_event >= temperature_control || isCooling)) {
temperature_event = TIME;
if (temperature_measure_sensor > MAX_TEMP) {
digitalWrite(RELE_ONE, LOW);
isCooling = true;
} else {
digitalWrite(RELE_ONE, HIGH);
isCooling = false;
}
}
//control de luces
if (TIME - light_event >= light_control) {
light_event = TIME;
digitalWrite(RELE_THREE, LOW);
} else {
digitalWrite(RELE_THREE, HIGH);
}
}
void singleClick() {
const int maxPics_L1 = 4;
const int maxPics_L2 = 2;
lastmillis = millis();
if (pic >= 0 && pic < 10) {
pic >= maxPics_L1 ? pic = 1 : pic++;
}
if (pic >= 10 && pic < 20) {
pic > maxPics_L2 + 10 ? pic = 11 : pic++;
}
if (pic >= 20 && pic < 30) {
pic > maxPics_L2 * 10 + maxPics_L2 ? pic = 21 : pic++;
}
if (pic >= 30 && pic < 40) {
pic >= maxPics_L2 * 10 + 10 + maxPics_L2 ? pic = 31 : pic++;
}
reload = true;
}
void doubleclick() {
lastmillis = millis();
switch (pic) {
case 11:
TYPE = "REGULAR";
pic = 1;
break;
case 12:
TYPE = "AUTO";
pic = 1;
break;
case 21:
FASE = "CRECIMIENTO";
pic = 2;
break;
case 22:
FASE = "FLORACION";
pic = 2;
break;
case 31:
COOLER = "SI";
pic = 3;
break;
case 32:
COOLER = "NO";
pic = 3;
break;
case 4:
start = false;
verify();
break;
};
}
void verify () {
if (TYPE != "N/A" && FASE != "N/A" && COOLER != "N/A") {
start = true;
temperature_control = COOLER == "SI" ? FASE == "CRECIMIENTO" ? temperature_control_growing : temperature_control_flowering : 0;
humidity_control = FASE == "CRECIMIENTO" ? humidity_control_growing : humidity_control_flowering;
light_control = TYPE == "REGULAR" ? light_control_reg : light_control_auto;
pic = 0;
}
else {
pic = 6;
}
}
void longPressStart() {
lastmillis = millis();
if (pic == 1)
{
pic = pic + 10;
} else if (pic == 2 || pic == 3) {
pic = pic * 10 + 1;
} else if (pic == 4) {
pic = 0;
}
}
void longPressStop() {
;
}
void menu()
{
if (pic == 0)
{
reload = false;
header();
body();
refresh();
}
// TIPO
if (pic == 1)
{
header();
display.setCursor(0, 11); display.print (F("> TIPO"));
display.setCursor(0, 20); display.print (F(" ETAPA"));
display.setCursor(0, 30); display.print (F(" COOLERS"));
display.setCursor(0, 50); display.print (F(" INICIAR CICLO"));
refresh();
}
// ETAPA
if (pic == 2)
{
header();
display.setCursor(0, 11); display.print (F(" TIPO"));
display.setCursor(0, 20); display.print (F("> ETAPA"));
display.setCursor(0, 30); display.print (F(" COOLERS"));
display.setCursor(0, 50); display.print (F(" INICIAR CICLO"));
refresh();
}
// VENTILADOR
if (pic == 3)
{
header();
display.setCursor(0, 11); display.print (F(" TIPO"));
display.setCursor(0, 20); display.print (F(" ETAPA"));
display.setCursor(0, 30); display.print (F("> COOLERS"));
display.setCursor(0, 50); display.print (F(" INICIAR CICLO"));
refresh();
}
//INFO
if (pic == 4)
{
header();
display.setCursor(0, 11); display.print (F(" TIPO"));
display.setCursor(0, 20); display.print (F(" ETAPA"));
display.setCursor(0, 30); display.print (F(" COOLERS"));
display.setCursor(0, 50); display.print (F("> INICIAR CICLO"));
refresh();
}
if (pic == 5)
{
header();
irrigationStart();
refresh();
}
if (pic == 6)
{
header();
fail();
refresh();
}
if (pic == 11)
{
header();
display.setCursor(0, 11); display.print (F("TIPO"));
display.setCursor(0, 20); display.print (F("> REGULAR"));
display.setCursor(0, 30); display.print (F("AUTO"));
refresh();
}
if (pic == 12)
{
header();
display.setCursor(0, 11); display.print (F("TIPO"));
display.setCursor(0, 20); display.print (F("REGULAR"));
display.setCursor(0, 30); display.print (F("> AUTO"));
refresh();
}
if (pic == 21)
{
header();
display.setCursor(0, 11); display.print (F("ETAPA"));
display.setCursor(0, 20); display.print (F("> CRECIMIENTO"));
display.setCursor(0, 30); display.print (F("FLORACION"));
refresh();
}
if (pic == 22)
{
header();
display.setCursor(0, 11); display.print (F("ETAPA"));
display.setCursor(0, 20); display.print (F("CRECIMIENTO"));
display.setCursor(0, 30); display.print (F("> FLORACION"));
refresh();
}
if (pic == 31)
{
header();
display.setCursor(0, 11); display.print (F("COOLERS"));
display.setCursor(0, 20); display.print (F("> SI"));
display.setCursor(0, 30); display.print (F("NO"));
refresh();
}
if (pic == 32)
{
header();
display.setCursor(0, 11); display.print (F("COOLERS"));
display.setCursor(0, 20); display.print (F("SI"));
display.setCursor(0, 30); display.print (F("> NO"));
refresh();
}
}
void header()
{
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(22, 0); display.print(F("SAR v0.1"));
display.drawLine (0, 9, 128, 9, WHITE);
}
void body()
{
// temperature_measure_sensor = dht.readTemperature();
// humidity_measure_sensor = analogRead(humidity_sensor);
// int humidity = map(humidity_measure_sensor , 1023, 0, 0, 100);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 11);
display.print (F("TIPO: "));
display.println(TYPE);
display.print (F("ETAPA: "));
display.println(FASE);
display.print (F("COOLERS: "));
display.println(COOLER);
// display.print (F("TEMPERATURA: "));
// display.println(temperature_measure_sensor);
// display.print (F("HUMEDAD TIERRA: "));
// display.print(humidity);
// display.println('%');
}
void irrigationStart()
{
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 11);
display.print (F("CICLO INICIADO"));
}
void fail()
{
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 11);
display.print (F("REVISE PARAMENTROS"));
}
void refresh()
{
display.display();
delay(00);
display.clearDisplay();
}