#include "U8glib.h"
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
byte row; // just for menu purposes
bool menuLock;// just for menu purposes
float xMax;
const byte maxArrayLength = 10; // this example supports up to 10 readings max for 128 and 20 for 256
// --------------------------------------- USER VARIABLES -------------------------------------------------------
char titles[4][12] = {"Temperature", "Humidity", "PH", "EC"};
float temperature[maxArrayLength] = {21.6, 21.4, 20.8, 20.2, 20.5, 24.1, 25.5, 27.2, 18.7, 16.5};
byte humidity[maxArrayLength] = {44, 47, 43, 43, 44, 42, 36, 32, 49, 52};
float ph[maxArrayLength] = {6.42, 5.29, 6.51, 5.54, 4.59, 3.66, 4.73, 4.84, 4.87, 4.92};
float ec[maxArrayLength] = {0, 2.2, 3.7, 5.9, 6.0, 4.3, 4.8, 3.2, 4.1, 5.4};
float tempArray[maxArrayLength];
const byte downButton_Pin = A2;
const byte selectButton_Pin = A1;
const byte upButton_Pin = A0;
void setup() {
pinMode(downButton_Pin, INPUT);
pinMode(upButton_Pin, INPUT);
pinMode(selectButton_Pin, INPUT);
u8g.setColorIndex(1);
u8g.setFont(u8g_font_fur11);
u8g.setFontPosTop();
}
void loop() {
buttons();
loadPage();
}
void buttons() {
if (digitalRead(upButton_Pin) == HIGH) {
if (row < 3 && !menuLock) {
row++;
delay(200);
}
}
else if (digitalRead(downButton_Pin) == HIGH) {
if (row > 0 && !menuLock) {
row--;
delay(200);
}
}
else if (digitalRead(selectButton_Pin) == HIGH) {
menuLock = !menuLock;
delay(200);
}
}
void loadPage() {
u8g.firstPage();
do {
drawPage();
}
while (u8g.nextPage());
}
void drawPage() {
if (!menuLock) {
printTitle(row, 24); //(byte titleNumber, byte y)
}
else if (row == 0) {
drawGraph(temperature, maxArrayLength, false);
}
else if (row == 1) {
adjustArrayToFloat(humidity, 6, false); // should be maxArrayLength instead of 6, but just to show you the graph does adjust accordingly
}
else if (row == 2) {
drawGraph(ph, maxArrayLength, false);
}
else if (row == 3) {
drawGraph(ec, maxArrayLength, false);
}
}
void adjustArrayToFloat(byte *dataB, byte arrayLenght, bool displayQuadrant) {
for (byte i = 0; i < arrayLenght; i++) {
tempArray[i] = dataB[i];
}
drawGraph(tempArray, arrayLenght, displayQuadrant);
}
void drawGraph (float *data, byte arrayLenght, bool displayQuadrant) {
//printTitle(row, 0);
for (byte i = 0 ; i < arrayLenght; i++) {
if (data[i] > xMax) {
xMax = data[i];
}
}
// work out the x position to start the graph at, based on the width on the x axis numbers.
byte graphXstartPosition;
if (xMax > 10) {
graphXstartPosition = 17;
}
else {
graphXstartPosition = 10;
}
// draw x line and numbers
float xIncrement = xMax / 4;
byte xLablePosition = 0;
float tempXmax = xMax;
for (byte i = 0 ; i < 5; i++) {
if (tempXmax > 10 || graphXstartPosition == 10) {
u8g.setPrintPos(0, xLablePosition);
}
else {
u8g.setPrintPos(8, xLablePosition);
}
u8g.print(tempXmax, 0);
xLablePosition += 13;
tempXmax -= xIncrement;
}
u8g.drawLine(graphXstartPosition, 0, graphXstartPosition, 51);
// draw y line and numbers
byte yLablePosition = graphXstartPosition + 1;
for (byte i = 1 ; i < arrayLenght + 1; i++) {
u8g.setPrintPos(yLablePosition, 52);
u8g.print(i);
yLablePosition += 10;
}
byte endOfcursorX = u8g.getPrintCol();
u8g.drawLine(graphXstartPosition, 51, endOfcursorX, 51);
// draw line graph
float xGap = 51 / xMax;
byte yGap = graphXstartPosition + 5;
float plotXstartPosition;
float plotXendPosition;
for (byte i = 0 ; i < arrayLenght - 1; i++) { // note its one less than the array size as we are adding 1 to i.
plotXstartPosition = 51 - xGap * data[i];
plotXendPosition = 51 - xGap * data[i + 1];
u8g.drawLine(yGap, plotXstartPosition, yGap + 10, plotXendPosition);
if (displayQuadrant) {
u8g.drawDisc(yGap, plotXstartPosition, 2);
}
yGap += 11;
}
if (displayQuadrant) {
u8g.drawDisc(yGap, plotXendPosition, 2);
}
xMax = 0;
}
void printTitle (byte titleNumber, byte y) {
byte center = ((u8g.getWidth() - u8g.getStrWidth(titles[titleNumber])) / 2);
u8g.setPrintPos(center, y);
u8g.print(titles[titleNumber]);
}