#include <U8g2lib.h>
#include <Wire.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); // Define U8g2 display object
const int dataSize = 128; // Number of data points for each graph
byte data1[dataSize]; // Initialize the data array for the first graph
byte data2[dataSize]; // Initialize the data array for the second graph
byte currentIndex = 0; // Index to keep track of the current data point
int potValue1 = 0;
int potValue2 = 0;
int verticalDiv = 30; // Adjusted vertical division position
const unsigned char montains_alt [] PROGMEM = {
0x01, 0x10, 0x97, 0x00, 0x03, 0x38, 0xD4, 0x01, 0x47, 0x7C, 0x94, 0x00,
0xEC, 0x10, 0x97, 0x00, 0x99, 0x11, 0x95, 0x00, 0x32, 0x13, 0x95, 0x00,
0x64, 0x06, 0x95, 0x00, 0xC8, 0x0C, 0xB7, 0x00
};
const unsigned char montains_press [] PROGMEM = {
0x01, 0x88, 0xB8, 0x01, 0x03, 0x88, 0xA0, 0x00, 0x07, 0x88, 0xA0, 0x00,
0x6C, 0xBE, 0xBB, 0x00, 0xD9, 0x9C, 0xAA, 0x00, 0xB2, 0x89, 0xAA, 0x00,
0x64, 0x83, 0xAA, 0x00, 0xC8, 0x86, 0xBB, 0x00
};
void setup() {
u8g2.begin(); // Initialize the display
u8g2.setBitmapMode(1);
u8g2.setColorIndex(1);
}
void loop() {
// Read potentiometer values
potValue1 = analogRead(A0); // Read the value from the first potentiometer
potValue2 = analogRead(A1); // Read the value from the second potentiometer
// Update the data arrays with the potentiometer values
data1[currentIndex] = map(potValue1, 0, 1023, 0, u8g2.getHeight() / 2 - 1); // Divide height by 2 for the first graph
data2[currentIndex] = map(potValue2, 0, 1023, 0, u8g2.getHeight() / 2 - 1); // Divide height by 2 for the second graph
// Increment currentIndex and wrap around if necessary
currentIndex = (currentIndex + 1) % dataSize;
// Clear the display buffer
u8g2.clearBuffer();
drawPotentiometerValues(potValue1, potValue2);
// Draw the first graph
drawGraph(data1, u8g2.getHeight() / 2, 0, verticalDiv);
// Draw the second graph
drawGraph(data2, u8g2.getHeight() / 2, u8g2.getHeight() / 2, verticalDiv);
// Send the buffer to the display
u8g2.sendBuffer();
// Delay to control the graph update rate
delay(1000);
}
void drawGraph(byte* data, int graphHeight, int yOffset, int xOffset) {
// Define the graph area
int graphWidth = u8g2.getWidth() - xOffset;
int graphY = yOffset;
// Calculate scaling factors
float scaleX = (float)graphWidth / (dataSize - 1);
float scaleY = (float)graphHeight / 32.0; // Adjusted to accommodate larger range
// Plot the data points
for (int i = 0; i < dataSize - 1; i++) {
int x1 = i * scaleX + xOffset;
int y1 = graphY + graphHeight - data[(currentIndex + i) % dataSize] * scaleY;
int x2 = (i + 1) * scaleX + xOffset;
int y2 = graphY + graphHeight - data[(currentIndex + i + 1) % dataSize] * scaleY;
u8g2.drawLine(x1, y1, x2, y2);
}
}
void drawPotentiometerValues(int value1, int value2) {
u8g2.setFont(u8g2_font_6x10_tr);
//Lines
u8g2.drawLine(verticalDiv, 0, verticalDiv, u8g2.getHeight());
//u8g2.drawLine(verticalDiv + 1, 0, verticalDiv + 1, u8g2.getHeight());
u8g2.drawLine(0, u8g2.getHeight()/2, u8g2.getWidth(), u8g2.getHeight()/ 2);
//u8g2.drawLine(0, (u8g2.getHeight()/2) + 1, u8g2.getWidth(), (u8g2.getHeight()/ 2) + 1 );
// Alt value
u8g2.drawXBMP( 0, 0, 25, 8, montains_alt);
u8g2.setCursor(0, 19); // Position for the first potentiometer value
u8g2.print(value1);
u8g2.drawStr(0,27,"m"); // Position for the alt unit
// Pressure value
u8g2.drawXBMP( 0, 36, 25, 8, montains_press);
u8g2.setCursor(0, 54); // Position for the second potentiometer value
u8g2.print(value2);
u8g2.drawStr(0,63,"mBar"); // Position for the pressure unit
}