Compare commits

..

No commits in common. "5f3e8cc10c2c4c246ba8e1764bbf3a5e6ab8c137" and "9e539682e8b7fc3680591d47841cd6def206fe96" have entirely different histories.

5 changed files with 34 additions and 152 deletions

View File

@ -4,8 +4,12 @@
#include <Arduino.h>
#define EC_PIN_RELAY_PROBE 27
#define EC_PIN_RELAY_CALIBRATION 26
#define EC_PIN_RELAY_RANGE 25
#define EC_CALIBRATION_RESISTOR_NC 100000
#define EC_CALIBRATION_RESISTOR_NO 1000
#define EC_PIN_ADC 4
#define EC_PIN_FREQ 5
@ -13,155 +17,37 @@
#define EC_RESOLUTION 8
#define EC_FREQUENCY 5000
#define EC_ARRAY_SIZE 128
uint16_t ec_array_rangeLow[EC_ARRAY_SIZE];
uint16_t ec_array_rangeHigh[EC_ARRAY_SIZE];
uint16_t ec_array_pos=EC_ARRAY_SIZE*2;
#define EC_MEASUREMENT_INTERVAL 10000 //complete filtered measurement every x ms
//One filtered measurement takes EC_READ_INTERVAL*EC_ARRAY_SIZE*2
#define EC_READ_INTERVAL 5 //interval of reading adc value inside a measurement
#define EC_RELAY_SWITCH_SETTLETIME 500 //time until voltage of ec circuit has settled
unsigned long ec_last_change_relay=0; //millis of last relay change
enum ECState{IDLE,MEASURE};
ECState ecstate=IDLE;
bool ec_measurementReady();
void ec_startMeasurement();
void ec_setRange(bool);
void ec_connectProbe(bool);
void ec_releaseRelay();
#define EC_ARRAY_SIZE 1024
uint16_t ec_array[EC_ARRAY_SIZE];
uint16_t ec_array_pos=0;
unsigned long last_read_ec=0;
#define EC_READ_INTERVAL 1
void ec_setup() {
pinMode(EC_PIN_ADC,INPUT);
ledcSetup(EC_PWM_CH, EC_FREQUENCY, EC_RESOLUTION);
ledcAttachPin(EC_PIN_FREQ, EC_PWM_CH);
ledcWrite(EC_PWM_CH, 127); //50% duty cycle
ledcWrite(EC_PWM_CH, 127);
pinMode(EC_PIN_RELAY_PROBE,OUTPUT); //LOW=Calibration/idle, HIGH=Probe connected
pinMode(EC_PIN_RELAY_CALIBRATION,OUTPUT); //LOW=NC Calibration Resistor, HIGH=NO Calib. Res.
pinMode(EC_PIN_RELAY_RANGE,OUTPUT); //LOW=NC Range Resistor, HIGH=NO Range Resistor
ec_releaseRelay();
}
void ec_loop(unsigned long loopmillis) {
static unsigned long last_measurement_ec=0;
static unsigned long last_read_ec=0;
switch (ecstate) {
case IDLE:
if (loopmillis>last_measurement_ec+EC_MEASUREMENT_INTERVAL && ecstate==IDLE) { //start measurement if idle
last_measurement_ec=loopmillis;
ec_startMeasurement();
ec_connectProbe(true);
ecstate=MEASURE;
Serial.println("EC Take Measurement");
}
break;
case MEASURE:
if (ec_measurementReady()) {
ec_releaseRelay();
float adc_rangelow=getMean(ec_array_rangeLow,EC_ARRAY_SIZE);
float adc_rangehigh=getMean(ec_array_rangeHigh,EC_ARRAY_SIZE);
Serial.print("EC ADC: Low="); Serial.print(adc_rangelow); Serial.print(" High="); Serial.println(adc_rangehigh);
ecstate=IDLE;
}
break;
}
if (loopmillis>last_read_ec+EC_READ_INTERVAL && ec_array_pos/2<EC_ARRAY_SIZE) { //take reading into array if measurement running
last_read_ec=loopmillis;
//flag_print= ec_array_pos==EC_ARRAY_SIZE;
//ec_array_pos%=EC_ARRAY_SIZE;
if (ec_array_pos<EC_ARRAY_SIZE){ //low range
ec_setRange(LOW);
}else{ //high range
ec_setRange(HIGH);
}
if (loopmillis>ec_last_change_relay+EC_RELAY_SWITCH_SETTLETIME) { //values have settled
uint16_t value=analogRead(EC_PIN_ADC);
if (ec_array_pos<EC_ARRAY_SIZE){ //low range
ec_array_rangeLow[ec_array_pos%EC_ARRAY_SIZE]=value;
}else{ //high range
ec_array_rangeHigh[ec_array_pos%EC_ARRAY_SIZE]=value;
}
/*
if (ec_array_pos==0) {
Serial.println(""); Serial.print("Lowrange:");
}
if (ec_array_pos==EC_ARRAY_SIZE) {
Serial.println(""); Serial.print("Highrange:");
}
Serial.print(value); Serial.print(" ");
if (ec_array_pos==EC_ARRAY_SIZE*2-1) {
Serial.println("");
}
*/
ec_array_pos++;
}
}
}
void ec_startMeasurement() {
ec_array_pos=0;
}
bool ec_measurementReady(){
if (ec_array_pos>=EC_ARRAY_SIZE*2) { //reached end of both arrays
return true;
}else{
return false;
}
}
void ec_setRange(bool range) {
//range low means low resistor value -> NO -> relay High
bool val=digitalRead(EC_PIN_RELAY_RANGE);
if (val!=!range) { //write only if different
digitalWrite(EC_PIN_RELAY_RANGE,!range);
ec_last_change_relay=millis();
}
}
void ec_connectProbe(bool relay) {
bool val=digitalRead(EC_PIN_RELAY_PROBE);
if (val!=relay) { //write only if different
digitalWrite(EC_PIN_RELAY_PROBE,relay);
ec_last_change_relay=millis();
}
}
void ec_releaseRelay() {
digitalWrite(EC_PIN_RELAY_PROBE,LOW);
digitalWrite(EC_PIN_RELAY_CALIBRATION,LOW);
digitalWrite(EC_PIN_RELAY_RANGE,LOW);
ec_last_change_relay=millis();
}
void ec_loop(unsigned long loopmillis, unsigned long pInterval) {
if (loopmillis>last_read_ec+pInterval) {
last_read_ec=loopmillis;
ec_array_pos++;
flag_print= ec_array_pos==EC_ARRAY_SIZE;
ec_array_pos%=EC_ARRAY_SIZE;
ec_array[ec_array_pos]=analogRead(EC_PIN_ADC);
//Serial.print(ec_array[ec_array_pos]); Serial.print(" ");
}
}
#endif

View File

@ -19,8 +19,8 @@ void flow_setup() {
attachInterrupt(FLOW_PIN, isr_flow, CHANGE);
}
void flow_loop(unsigned long loopmillis) {
if (loopmillis>=last_read_flow+READINTERVAL_FLOW) {
void flow_loop(unsigned long loopmillis, unsigned long pInterval) {
if (loopmillis>=last_read_flow+pInterval) {
flow=flow_counter*1000.0/(loopmillis-last_read_flow)/2.0; //Frequency [Hz]
flow/=flow_factor; //[L/min]

View File

@ -78,12 +78,12 @@ void temperature_setup() {
}
void temperature_loop(unsigned long loopmillis) {
void temperature_loop(unsigned long loopmillis, unsigned long pInterval) {
static unsigned long last_read_ds18b20;
static bool flag_requestTemperatures=false;
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20) {
if (loopmillis>last_read_ds18b20+READINTERVAL_DS18B20*10) { //timeout
if (loopmillis>last_read_ds18b20+pInterval) {
if (loopmillis>last_read_ds18b20+pInterval*10) { //timeout
Serial.println("Warn: Request Temperatures Timeout!");
flag_requestTemperatures=false;
}

View File

@ -24,10 +24,10 @@ void waterlevel_setup() {
}
}
void waterlevel_loop(unsigned long loopmillis) {
void waterlevel_loop(unsigned long loopmillis, unsigned long pInterval) {
static unsigned long last_read_hcsr04;
if (loopmillis>=last_read_hcsr04+READINTERVAL_HCSR04) {
if (loopmillis>=last_read_hcsr04+pInterval) {
last_read_hcsr04=loopmillis;
float temperature=20.0;
if (tempC_air!=DEVICE_DISCONNECTED_C && isValueArrayOKf(tempCmean_air,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)) { //sensor ok

View File

@ -51,24 +51,22 @@ void loop() {
flag_print=false;
ec_loop(loopmillis);
ec_loop(loopmillis, EC_READ_INTERVAL);
temperature_loop(loopmillis);
temperature_loop(loopmillis, READINTERVAL_DS18B20);
waterlevel_loop(loopmillis);
waterlevel_loop(loopmillis, READINTERVAL_HCSR04);
flow_loop(loopmillis);
flow_loop(loopmillis, READINTERVAL_FLOW);
if (loopmillis>last_print+500) {
last_print=loopmillis;
/*
if (isValueArrayOK(ec_array,EC_ARRAY_SIZE,0))
{
Serial.print("EC=");
@ -78,7 +76,6 @@ void loop() {
Serial.print("Waiting for EC");
}
if (isValueArrayOKf(tempCmean_reservoir,TEMPMEAN_SIZE,DEVICE_DISCONNECTED_C)){
Serial.print("\t Treservoir="); Serial.print(getMeanf(tempCmean_reservoir,TEMPMEAN_SIZE)); Serial.print("\t Tair="); Serial.print(getMeanf(tempCmean_air,TEMPMEAN_SIZE));
}else{
@ -106,7 +103,6 @@ void loop() {
Serial.println();
*/
}