#include <RunningAverage.h>
#include <EEPROM.h>
#include <Adafruit_ADS1015.h> // AD converter
#include <Adafruit_GFX.h> // Adafruit core graphics library
#include <Adafruit_ST7789.h> // Adafruit hardware-specific library for ST7789
#define TFT_CS 10 // define chip select pin ST7789 TFT module
#define TFT_DC 9 // define data/command pin ST7789 TFT module
#define TFT_RST 8 // define reset pin, or set to -1 and connect to Arduino RESET pin
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_ADS1115 ads(0x48); // analog-digital converter for oxygen and helium sensor
Adafruit_ADS1115 ads2(0x49); // analog-digital converter for CO sensor
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
// -------sensor settings-----------------
boolean enableHe = true;
boolean enableCO = true;
const byte calibrationCount = 10; // calibration samples
const byte readDelay = 3; // ADC read delay between samples
float oxVcal = 0; // oxygen voltage - calibration
float oxVact = 0; // oxygen voltage - actual / measured
float oxVmax = 0; // oxygen voltage - max
float oxVmin = 0; // oxygen voltage - min
float heVact = 0; // helium voltage - actual
float heVmax = 0; // helium voltage - max
float heVcal = 0; // helium voltage - calibration | turn potmeter so it reads 0mV
float carbon = 0; // ppm
float coVact = 0; // carbonmonoxide voltage - actual / measured
float gain = 0.03125; // gain ADS1115
unsigned long time; // runtime
unsigned long menuCounter=0; // runtime
float MODcalc = 1.4; // start with 1.4 for MOD calculation
const byte bLeft = 2; // pin D2 connected to pushbutton
const byte bRight = 3; // pin D3 connected to pushbutton
volatile byte buttonStateLeft;
volatile byte buttonStateRight;
byte leftclick = 0;
byte rightclick = 0;
byte action=0;
byte RAsize=20;
byte i=0;
int16_t adcHe;
int16_t adcO2;
boolean calibrateHe = false;
boolean calibrateO2 = false;
char calibrate[] = "Calibrating..."; // create a string
char calibOK[] = "Calibration OK ";
char calibReq[] = "Calibration required";
char mV[] = "mV ";
char space1[] = " ";
char ver1[] = " Nitrox / Trimix";
char ver2[] = "Analyzer software";
char warn1[] = "This analyzer";
char warn2[] = "has no brain!";
char warn3[] = "Use your own!";
char HEsensor[] = "He sensor ";
char O2sensor[] = "O2 sensor ";
char next[] = "Next";
char select[] = "Select";
RunningAverage RAhe(RAsize); // average He with 20 values
RunningAverage RAox(RAsize); // average O2 with 20 values
RunningAverage RAco(RAsize); // average CO with 20 values
float readO2sensor(){
RAox.clear();
float millivolts = 0;
for (i=0; i<=RAsize; i++) {
millivolts = ads.readADC_Differential_2_3();
RAox.addValue(millivolts);
}
millivolts = abs(RAox.getAverage()*gain);
return millivolts;
}
float readHEsensor() {
RAhe.clear();
float millivolts = 0;
for (i=0; i<=RAsize; i++) {
millivolts = ads.readADC_Differential_0_1();
RAhe.addValue(millivolts);
}
millivolts = abs(RAhe.getAverage()*gain);
return millivolts;
}
float readCOsensor() {
RAco.clear();
for (i=0; i<=RAsize; i++) {
coVact = ads2.readADC_SingleEnded(3);
RAco.addValue(coVact);
}
coVact = abs(RAco.getAverage()*0.1875)/1000 - 0.015;
float ppm = (3.125 * coVact - 1.25) * 100; //linear relationship(0.4V for 0 ppm and 2V for 500ppm)
if(ppm<0)
ppm=0;
else if(ppm>500)
ppm = 500;
return ppm;
}