#include <FlowSensor.h>
#define sig_1 2
#define sig_2 3
#define level_1 4
#define level_2 5
#define level_3 6
#define txEnable 7
#define levelEnable 8
#define configSW 19
// Level sensor variables
int level;
// Pressure variables
#define PS_1 A6
#define PS_2 A7
float pressure_1;
float pressure_2;
unsigned long mpxBuff_1;
unsigned long mpxBuff_2;
long int mpxAverage_1;
long int mpxAverage_2;
const int sample = 500;
float V_1;
float V_2;
// Flow sensor variables
#define type YFS201
FlowSensor Sensor_1(type, sig_1);
FlowSensor Sensor_2(type, sig_2);
float volume_1;
float volume_2;
float flowRate_1;
float flowRate_2;
// Serial Variables
const byte dip[] = {14, 15, 16, 17, 18};
int bin;
int address;
char data[64] = {0};
int y100;
int y200;
int y300;
int commandEStop = 91;
int typeIdent = 326;
int requestFlow = 101;
int requestPressure = 102;
int requestLevel = 103;
// Timer_1 variables
int state;
unsigned int a;
int blink;
long offTime = 1000;
long onTime = 1000;
unsigned long previousMillis;
unsigned long currentMillis;
void setup() {
pinMode(dip[0], INPUT_PULLUP);
pinMode(dip[1], INPUT_PULLUP);
pinMode(dip[2], INPUT_PULLUP);
pinMode(dip[3], INPUT_PULLUP);
pinMode(dip[4], INPUT_PULLUP);
pinMode(configSW, INPUT_PULLUP);
pinMode(level_1, INPUT_PULLUP);
pinMode(level_2, INPUT_PULLUP);
pinMode(level_3, INPUT_PULLUP);
pinMode(levelEnable, OUTPUT);
pinMode(txEnable, OUTPUT);
digitalWrite(txEnable, LOW);
digitalWrite(levelEnable, LOW);
Sensor_1.begin(count_1);
Sensor_2.begin(count_2);
delay(1000);
Serial.begin(9600);
previousMillis = 0;
currentMillis = millis();
a = HIGH;
bin = 101;
if(digitalRead(dip[0]) == LOW) {
bin = bin + 1;
}
delay(10);
if(digitalRead(dip[1]) == LOW) {
bin = bin + 2;
}
delay(10);
if(digitalRead(dip[2]) == LOW) {
bin = bin + 4;
}
delay(10);
if(digitalRead(dip[3]) == LOW) {
bin = bin + 8;
}
delay(10);
if(digitalRead(dip[4]) == LOW) {
bin = bin + 16;
}
delay(10);
address = bin;
Serial.print(address);
Serial.print(",");
Serial.println(typeIdent);
y200 = 0;
volume_1 = 0;
volume_2 = 0;
mpxBuff_1 = 0;
mpxBuff_2 = 0;
}
void loop() {
while(digitalRead(configSW) == HIGH) {
timer_1();
getSerialData();
readSensors();
while(y200 == commandEStop) {
y200 = commandEStop;
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
}
while(digitalRead(configSW) == LOW) {
y200 = requestLevel;
readSensors();
Serial.print(pressure_1);
Serial.println(" PSI");
Serial.print(pressure_2);
Serial.println(" PSI");
Serial.print(volume_1);
Serial.print("L, ");
Serial.print(flowRate_1);
Serial.println("L/m");
Serial.print(volume_2);
Serial.print("L, ");
Serial.print(flowRate_2);
Serial.println("L/m");
Serial.print("Level = ");
Serial.println(level);
delay(1000);
if(digitalRead(configSW) == HIGH) {
break;
}
}
}
void count_1() {
Sensor_1.count();
}
void count_2() {
Sensor_2.count();
}
void getSerialData() {
byte n = Serial.available();
if (n != 0) {
byte m = Serial.readBytesUntil('\n', data, 64);
data[m] = '\0'; //null-byte
y100 = atoi(strtok(data, ","));
y200 = atoi(strtok(NULL, ","));
y300 = atoi(strtok(NULL, ","));
if(y100 = address) {
if(y200 == 0) {
}
else if(y200 == requestFlow) {
digitalWrite(txEnable, HIGH);
Serial.print(address);
Serial.print(",");
Serial.print(volume_1);
Serial.print(",");
Serial.print(volume_2);
Serial.print(",");
Serial.print(flowRate_1);
Serial.print(",");
Serial.println(flowRate_2);
digitalWrite(txEnable, LOW);
}
else if(y200 == requestPressure) {
digitalWrite(txEnable, HIGH);
Serial.print(address);
Serial.print(",");
Serial.print(pressure_1);
Serial.print(",");
Serial.println(pressure_2);
digitalWrite(txEnable, LOW);
}
else if(y200 == requestLevel) {
level = 0;
digitalWrite(levelEnable, HIGH);
delay(500);
if(digitalRead(level_1) == LOW) {
level = level + 1;
}
if(digitalRead(level_2) == LOW) {
level = level + 10;
}
if(digitalRead(level_3) == LOW) {
level = level + 100;
}
digitalWrite(txEnable, HIGH);
Serial.print(address);
Serial.print(",");
Serial.println(level);
digitalWrite(txEnable, LOW);
digitalWrite(levelEnable, LOW);
y200 = 0;
}
}
}
}
void timer_1() {
currentMillis = millis();
if ((a == HIGH) && (currentMillis - previousMillis >= offTime)) {
a = LOW;
previousMillis = currentMillis;
blink = 1;
}
else if((a == LOW) && (currentMillis - previousMillis >= onTime)) {
a = HIGH;
previousMillis = currentMillis;
blink = 0;
}
}
void readSensors() {
Sensor_1.read();
Sensor_2.read();
volume_1 = Sensor_1.getVolume();
volume_2 = Sensor_2.getVolume();
flowRate_1 = Sensor_1.getFlowRate_m();
flowRate_2 = Sensor_2.getFlowRate_m();
for(int i = 0; i < sample; i++) {
int analog_1;
int analog_2;
analog_1 = analogRead(PS_1);
analog_2 = analogRead(PS_2);
mpxBuff_1 = mpxBuff_1 + analog_1;
mpxBuff_2 = mpxBuff_2 + analog_2;
delay(1);
}
mpxAverage_1 = (mpxBuff_1 / sample);
mpxAverage_2 = (mpxBuff_2 / sample);
V_1 = ((mpxAverage_1 / 1023.0) * 5.0);
V_2 = ((mpxAverage_2 / 1023.0) * 5.0);
pressure_1 = (((V_1 - 0.5) / 4.0) * 100);
pressure_2 = (((V_2 - 0.5) / 4.0) * 100);
mpxBuff_1 = 0;
mpxBuff_2 = 0;
if(y200 == requestLevel) {
level = 0;
digitalWrite(levelEnable, HIGH);
delay(500);
if(digitalRead(level_1) == LOW) {
level = level + 1;
}
if(digitalRead(level_2) == LOW) {
level = level + 10;
}
if(digitalRead(level_3) == LOW) {
level = level + 100;
}
y200 = 0;
digitalWrite(levelEnable, LOW);
}
}