I am making a project to display the parameters of an aquarium on a SSD1306 SPI 128x32 monochrome OLED LCD display. I would like to use one button to cycle through 3 different screens: a clock; parameters (outside temp, inside temp, and pH), and daily maintenance. I have it working now so on the first button press it will display the clock screen, then display the parameters screen on the next button press, display the maintenance screen on the next button press, and the turn off the screen on the next button press. This is great, but it does not update the information live while the screen is displayed. So basically when I cycle to the parameters screen it will show the temps and pH at the time it first opened the screen. I would like it display live data, so if the temperatures change, I would like to see this in real time, then when I press the button it will cycle to the next screen. I am pretty sure I need to use an interrupt to make this happen, but I am not quite sure how to accomplish this. Will the interrupt have to come from a second button, or can I use one button for everything? Any help with my code will be greatly appreciated. Here is my current sketch:
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DS1307.h>
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>
#define ONE_WIRE_BUS 4 //temp is coming from pin 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress testProbe = {
0x28, 0xD5, 0xD7, 0xAA, 0x02, 0x00, 0x00, 0xC3 };
DeviceAddress testProbe2 = {
0x28, 0x05, 0x02, 0x84, 0x04, 0x00, 0x00, 0x3F };
float tempTest;
float tempTest2;
int buttonPin = 7;
int x = 0; //this will be used to count how many times we push the button
int prevval;
int val;
#define OLED_DC 11
#define OLED_CS 12
#define OLED_CLK 10
#define OLED_MOSI 9
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//*************Main Screen Display
void mainScreen () {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor (90,0);
display.println(tempTest);
display.setCursor(0,0);
display.println("Water Temp:");
display.setCursor(0,8);
display.println("next temp: ");
display.setCursor(90,8);
display.println(tempTest2);
display.setCursor(0,16);
display.println("pH:");
display.setCursor(2,24);
display.println("Time:");
if (hour() < 10 ) {
display.setCursor(97,24);
display.println(hourFormat12());
}
if (hour() >= 10 ) {
display.setCursor(90,24);
display.println(hourFormat12());
}
display.setCursor(103,24);
display.println(":");
if (minute() < 10) {
display.setCursor(108,24);
display.println("0");
display.setCursor(115,24);
display.println(minute());
}
else {
display.setCursor(109,24);
display.println(minute());
}
display.display();
}
//**************End of Main Screen
//**************Maintenance Screen
void maintenance() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
//display.setCursor(10,0);
//display.println(weekday());
if (weekday() == 1) {
display.setCursor(0,8);
display.println("Water Change");
}
if (weekday() == 2) {
display.setCursor(0,8);
display.println("23 Drops Iron");
display.setCursor(0,16);
display.println("14 Drops Phosphorus");
display.setCursor(0,24);
display.println("14 Drops Nitrogen");
}
if (weekday() == 3) {
display.setCursor(0,8);
display.println("23 Drops Iron");
display.setCursor(0,16);
display.println("2.25mL Trace");
}
if (weekday() == 4) {
display.setCursor(0,8);
display.println("23 Drops Iron");
display.setCursor(0,16);
display.println("1.5mL Potassium");
}
if (weekday() == 5) {
//display.clearDisplay();
display.setCursor(0,0);
display.println("23 Drops Iron");
display.setCursor(0,8);
display.println("14 Drops Phosphorus");
display.setCursor(0,16);
display.println("14 Drops Nitrogen");
display.setCursor(0,24);
display.println("2.25mL Trace");
//display.display();
}
if (weekday() == 6) {
display.setCursor(45,0);
display.println("Friday");
display.setCursor(0,8);
display.println("23 Drops Iron");
display.setCursor(0,16);
display.println("1.5mL Potassium");
}
if (weekday() == 7) {
display.setCursor(0,8);
display.println("23 Drops Iron");
display.setCursor(0,16);
}
display.display();
}
//**************End of Maintenance Screen
//*****************Clock Screen
void time() {
//display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
//without this if else statement, there is a large space between the hour and the colon when hour is less than 10
if (hour() < 10 ) {
display.setCursor(57,13);
display.println(hourFormat12());
}
else {
display.setCursor(45,13);
display.println(hourFormat12());
}
display.setCursor(67,13);
display.println(":");
if (minute() < 10 ) {
display.setCursor(77,13);
display.println("0");
display.setCursor(90,13);
display.println(minute());
}
else {
display.setCursor(77,13);
display.println(minute());
}
display.display();
}
//************End of Clock Screen
void setup () {
Serial.begin (9600);
pinMode(buttonPin, INPUT);
//setTime(5,21,40,28,3,2013); //sets the time (hr,min,sec,day,month,yr); comment out when not needed
setSyncProvider(RTC.get);
display.begin(SSD1306_SWITCHCAPVCC);
display.clearDisplay();
}
void loop () {
sensors.requestTemperatures();
tempTest = (sensors.getTempF(testProbe));
tempTest2 = (sensors.getTempF(testProbe2));
val = digitalRead(buttonPin);
if (val == LOW && prevval == HIGH && x == 0) {
display.clearDisplay();
time();
prevval=val;
x++;
}
if (val == LOW && prevval == HIGH && x == 1) {
display.clearDisplay();
mainScreen();
prevval=val;
x++;
}
if (val == LOW && prevval == HIGH && x == 2) {
display.clearDisplay();
maintenance();
prevval=val;
x++;
}
if (val == LOW && prevval == HIGH && x == 3) {
display.clearDisplay();
display.display();
x=0;
}
else {
prevval=val;
}
}