Last active
February 17, 2020 19:17
-
-
Save tkalve/926e3b54a08bb03015d86e569d1ecda6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <Adafruit_ADS1015.h> | |
#include <EEPROM.h> | |
#include <RunningAverage.h> | |
#define RA_SIZE 20 | |
RunningAverage RA0(RA_SIZE); | |
RunningAverage RA1(RA_SIZE); | |
Adafruit_ADS1115 ads(0x48); | |
const int buttonPin = 2; // push button | |
double calibrationv; | |
double calibrationv2; | |
float multiplier; | |
int programState = 0; | |
int buttonState; | |
long buttonMillis = 0; | |
const long intervalButton = 2000; // 2 sec button hold to calibration | |
/* | |
Calculate MOD (Maximum Operating Depth) | |
*/ | |
float max_po1 = 1.40; | |
float max_po2 = 1.60; | |
float cal_mod (float percentage, float ppo2 = 1.4) { | |
return 10 * ( (ppo2 / (percentage / 100)) - 1 ); | |
} | |
/* Les sensor og skriv verdi til RA0 eller RA1, kalles med read_sensor(0) eller read_sensor(1) */ | |
int read_sensor(int x = 0) { | |
int16_t millivolts = 0; | |
if (x == 0) { | |
millivolts = ads.readADC_Differential_0_1(); | |
RA0.addValue(millivolts); | |
} | |
if (x == 1) { | |
millivolts = ads.readADC_Differential_2_3(); | |
RA1.addValue(millivolts); | |
} | |
} | |
void setup(void) { | |
Serial.begin(9600); | |
pinMode(buttonPin, INPUT_PULLUP); | |
ads.setGain(GAIN_TWO); | |
multiplier = 0.0625F; | |
ads.begin(); // ads1115 start | |
RA0.clear(); | |
RA1.clear(); | |
for (int cx = 0; cx <= RA_SIZE; cx++) { | |
read_sensor(0); | |
read_sensor(1); | |
} | |
calibrationv = EEPROMReadInt(0); | |
if (calibrationv < 100) calibrationv = calibrate(RA0, 0, 0); | |
calibrationv2 = EEPROMReadInt(10); | |
if (calibrationv2 < 100) calibrationv2 = calibrate(RA1, 1, 10); | |
} | |
void EEPROMWriteInt(int p_address, int p_value) { | |
byte lowByte = ((p_value >> 0) & 0xFF); | |
byte highByte = ((p_value >> 8) & 0xFF); | |
EEPROM.write(p_address, lowByte); | |
EEPROM.write(p_address + 1, highByte); | |
} | |
unsigned int EEPROMReadInt(int p_address) | |
{ | |
byte lowByte = EEPROM.read(p_address); | |
byte highByte = EEPROM.read(p_address + 1); | |
return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00); | |
} | |
int calibrate(RunningAverage ra, int sensor, int address) | |
{ | |
double result; | |
for (int cx = 0; cx <= RA_SIZE; cx++) | |
{ | |
read_sensor(sensor); | |
} | |
result = ra.getAverage(); | |
result = abs(result); | |
EEPROMWriteInt(address, result); // write to eeprom | |
delay(1000); | |
// lcd.clear(); | |
return result; | |
} | |
void analysing(RunningAverage ra, int sensor, int cal) | |
{ | |
double currentmv; | |
double result; | |
double mv = 0.0; | |
read_sensor(sensor); | |
currentmv = ra.getAverage(); | |
currentmv = abs(currentmv); | |
result = (currentmv / cal) * 20.9; | |
if (result > 99.9) result = 99.9; | |
mv = currentmv * multiplier; | |
if (mv >= 0.02) | |
{ | |
Serial.print("Celle " + (String)(sensor+1) + ": "); | |
Serial.print(result, 1); | |
Serial.println("% "); | |
Serial.print(mv, 1); | |
Serial.println(" mv"); | |
Serial.print("MOD "); | |
Serial.print(cal_mod(result, max_po1), 1); | |
Serial.println("m "); | |
Serial.println(); | |
} | |
} | |
void loop(void) { | |
unsigned long currentMillis = millis(); | |
buttonState = digitalRead(buttonPin); | |
if (buttonState == LOW && programState == 0) { | |
buttonMillis = currentMillis; | |
programState = 1; | |
} else if (programState == 1 && buttonState == HIGH) { | |
programState = 0; | |
} | |
if (currentMillis - buttonMillis > intervalButton && programState == 1) { | |
calibrationv = calibrate(RA0, 0, 0); | |
calibrationv2 = calibrate(RA1, 1, 10); | |
programState = 1; | |
} | |
analysing(RA0, 0, calibrationv); | |
delay(500); | |
analysing(RA1, 1, calibrationv2); | |
delay(500); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment