Hi, I have been working on this project where I am monitoring and controlling humidity/temperature for a humidor I am going to build. I am using an OLED screen and right now i've been designing the menu/ UI. I know that the code right now isnt the best but I have just been trying to make it work then finalise the workings and details once its fully working. (sorry im not a big commentor in my code). I am using a rotary encoder to navigate the menu.
if (value == 0){ //Encoder Position
Menu();
display.setTextSize(1);
display.setCursor(0,15);
display.print(">");
display.display();
if(buttonState== LOW){ //If encoder is pressed
value = 6;
while (value == 6){
MainDisplay(t,h);
}
}
}
This is the problem I have, I want to call the MainDisplay() function which displays the temperature and humidity easily to read, I have tried to use a while loop while runs the function while the encoder value is equal to 6, but when I try to loop this function the OLED screen freezes and so does the serial readout in the IDE. This function calls fine when I don't try to loop it, however that isn't very useful because the screen will only display the temp & humidity from when the function is called and remains stationary.
I'm pretty new to arduino code and this is my first project, so any help or advice would be very much appreciated. Once again, I know the other parts of the code may not be the most efficient but I plan to fix this when I get the code to work and I will tidy it up.
I will attach an image of the main display showing the temperature and humidity, and an image of the options menu which hopefully will make more sense and visualisation of what the code is doing during the display() lines of code.
Here is the full code.
#include <ClickEncoder.h>
#include <TimerOne.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
#define DHTPIN 12 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define Pa 2 //Encoder Pin A
#define Pb 3 //Encoder Pin B
#define SW A1 //Encoder Button
ClickEncoder *encoder;
int16_t last, value;
const int buttonPin = SW; //Encoder Button
int buttonState; // variable for reading the pushbutton status
int standby = 15;
int leftnumber;
float t;
float h;
float highT;
float lowT = 50;
float highH;
float lowH = 100;
void timerIsr() {
encoder->service();
}
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
encoder = new ClickEncoder(Pa, Pb, SW);
Timer1.initialize(1000);
Timer1.attachInterrupt(timerIsr);
last = -1;
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP);
Menu();
}
void MainDisplay(float t, float h){
//RESET
//delay(100);
display.clearDisplay();
//TEMP1
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(t);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
//HUMIDITY1
display.setTextSize(1);
display.setCursor(0, 35);
display.print("Humidity: ");
display.setTextSize(2);
display.setCursor(0, 45);
display.print(h);
display.print(" %");
//ICON-*Flashes*
display.fillRoundRect(100, 10, 20, 44, 2, WHITE);
display.setCursor(105,15);
display.setTextColor(BLACK);
display.print("J");
display.setCursor(105,35);
display.print("B");
display.display();
//delay(20);
//display.fillRoundRect(100, 10, 20, 44, 2, WHITE); //FLASH (copy Temp1 & Humidity1)
}
void Menu(){
//TITLE
display.clearDisplay();
delay(50);
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(40,0);
display.print("MENU");
//MENU1 Temp
display.setTextSize(1);
display.setCursor(10,25);
display.print("Temperature Set");
//MENU2 Humid
display.setCursor(10,35);
display.print("Humidity Set");
//MENU3 Setting
display.setCursor(10,45);
display.print("Settings");
//24HR Min/Max
display.setCursor(10,55);
display.print("Daily Min/Max");
//RETURN
display.setCursor(10,15);
display.print("Main Display");
display.setCursor(115,0);
display.print(value);
display.display();
}
void TempMenu(){ //Set Temperature
value = 6;
//ActualTEMP
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.setCursor(0,0);
display.print("Set Temperature =");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
display.display();
}
void HumMenu(){
}
void SetMenu(){
}
void MaxMenu(){ //Max/Min Temp&Hum
value = 6;
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(2);
display.setCursor(30,0);
display.print("MIN/MAX");
display.setTextSize(1);
display.setCursor(0,25);
display.print("Max Temperature=");
display.print(highT);
display.setCursor(0,35);
display.print("Min Temperature=");
display.print(lowT);
display.setCursor(0,45);
display.print("Max Humidity =");
display.print(highH);
display.setCursor(0,55);
display.print("Min Humidity =");
display.print(lowH);
display.display();
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
float f = dht.readTemperature(true);
if (t > highT){
highT = t; //Highest Temperature
}
if (t < lowT){
lowT = t; //Lowest Temperature
}
if (h > highH){
highH = h; //Highest Humidity
}
if (h < lowH){
lowH = h; //Lowest Humidity
}
value += encoder->getValue();
if (value != last) {
last = value;
Serial.print("Encoder Value: ");
Serial.println(value);
}
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is LOW:
if(value < 0){
value = 0; // Doesnt allow encoder to go below 0
}
if (value == 0){ //Encoder Position
Menu();
display.setTextSize(1);
display.setCursor(0,15);
display.print(">");
display.display();
if(buttonState== LOW){ //If encoder is pressed
value = 6;
while (value == 6){
MainDisplay(t,h);
}
}
}
if (value == 1){ //Encoder Position
Menu();
display.setTextSize(1);
display.setCursor(0,25);
display.print(">");
display.display();
if (buttonState == LOW){ //If encoder is pressed
TempMenu();
}
}
if (value == 2){ //Encoder Position
Menu();
display.setTextSize(1);
display.setCursor(0,35);
display.print(">");
display.display();
if (buttonState == LOW){ //If encoder is pressed
HumMenu();
}
}
if (value == 3){ //Encoder Position
Menu();
display.setTextSize(1);
display.setCursor(0,45);
display.print(">");
display.display();
if (buttonState == LOW){ //If encoder is pressed
SetMenu();
}
}
if (value == 4){ //Encoder Position
Menu();
display.setTextSize(1);
display.setCursor(0,55);
display.print(">");
display.display();
if (buttonState == LOW){ //If encoder is pressed
MaxMenu();
}
}
}