#include <Arduino.h>
#include <ADS1115_WE.h> // für AD-Wandler
#include <Wire.h>
#include <U8g2lib.h> // für OLED-Display
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
constexpr byte I2C_ADDRESS {0x48};
constexpr byte NUM_CHANNELS {4};
struct ValueStorage {
const ADS1115_MUX channel; // ADC Channel
const unsigned int x; // Display x-pos
const unsigned int y; // Display y-pos
const unsigned int xOffset; // Right align padding offset in pixel
const char *prefix; // Value prefix text
float voltage; // Value
};
// Initialize struct Valuestorage
ValueStorage voltS[NUM_CHANNELS] {
{ADS1115_COMP_0_GND, 3, 20, 36, "ws = ", 0.0},
{ADS1115_COMP_1_GND, 66, 20, 36, "ge = ", 0.0},
{ADS1115_COMP_2_GND, 3, 40, 36, "gn = ", 0.0},
{ADS1115_COMP_3_GND, 66, 40, 36, "sw = ", 0.0}
};
//using OLED = U8G2_SH1106_128X64_NONAME_F_HW_I2C;
//using OLED = U8G2_SSD1306_128X64_NONAME_F_HW_I2C;
using OLED = U8G2_SSD1306_128X64_NONAME_1_HW_I2C;
OLED u8g2(U8G2_R0, U8X8_PIN_NONE); // OLED-Display einrichten
ADS1115_WE adc = ADS1115_WE(I2C_ADDRESS);
void dpPrintError(OLED &, char *);
void dpPrintItems(OLED &, const ValueStorage[], byte);
float readChannel(ADS1115_MUX channel);
void setup(void) {
Serial.begin(115200);
Wire.begin();
u8g2.begin();
//u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.setFont(u8g2_font_6x10_tr);
// Uncomment when the sensor is connected.
/*
if (!adc.init()) {
dpPrintError(u8g2,"ADS1115 nicht erkannt!");
while(1) {}
}
*/
adc.setVoltageRange_mV(ADS1115_RANGE_6144); // Eingang AD-Wandler auf 6,1V einstellen
dpPrintItems(u8g2, voltS, NUM_CHANNELS);
}
void loop() {
for (byte i = 0; i < NUM_CHANNELS; ++i) {
float voltage = readChannel(voltS[i].channel);
if (voltage != voltS[i].voltage) {
voltS[i].voltage = voltage;
dpPrintItems(u8g2, voltS, NUM_CHANNELS);
}
}
delay(2000);
}
void dpPrintError(OLED &dp, char error[]) {
u8g2.firstPage();
do {
dp.setCursor(1, 30); dp.print(error);
} while ( u8g2.nextPage() );
}
void dpPrintItems(OLED &dp, const ValueStorage vs[], byte num) {
u8g2.firstPage();
do {
for (byte i = 0; i < num; ++i) {
dp.setCursor(vs[i].x, vs[i].y); dp.print(vs[i].prefix);
if (vs[i].voltage < 10) {
dp.setCursor(vs[i].x + vs[i].xOffset, vs[i].y);
}
dp.print(vs[i].voltage, 2);
}
} while ( u8g2.nextPage() );
}
// Comment out if the sensor is connected
float readChannel(ADS1115_MUX channel) {
randomSeed(millis());
return static_cast<float>(random(1000, 12001)) / 1000;
}
// uncomment when the sensor is connected
/*
float readChannel(ADS1115_MUX channel) {
float voltage = 0.0;
adc.setCompareChannels(channel);
adc.startSingleMeasurement();
while (adc.isBusy()) {}
voltage = adc.getResult_V(); // alternative: getResult_mV for Millivolt
return voltage;
}
*/