String inputString = "";
bool stringComplete = false;
bool readmore = false;
uint32_t curr_millis;
const int NumPumps = 5;
typedef struct {
uint8_t PIN; // PIN
bool Status = false; // Status: An: 1, Aus 0
uint32_t Menge = 0;
uint32_t start_millis;
} StructPumps_T;
int PumpNum;
StructPumps_T Pumpe[NumPumps];
int PumpenAn = 0;
int Mode = 0;
// 0 = Seriell - Char S
// 1 = Parallel - Char P
void Split(String& input, char Sep) {
int count = 0;
int pos = input.indexOf(Sep);
String tmpstr;
tmpstr = input.substring(0,pos);
if ( tmpstr == "P" ) {
Mode = 1;
} else {
Mode = 0;
}
input = input.substring(pos+1);
pos = input.indexOf(Sep);
tmpstr = input.substring(0,pos);
PumpenAn = tmpstr.toInt();
input = input.substring(pos+1);
pos = input.indexOf(Sep);
for ( uint8_t z = 0; z < PumpenAn; z++ ) {
tmpstr = input.substring(0,pos);
input = input.substring(pos+1);
pos = input.indexOf(Sep);
PumpNum = tmpstr.toInt();
tmpstr = input.substring(0,pos);
input = input.substring(pos+1);
pos = input.indexOf(Sep);
Pumpe[PumpNum].Menge = tmpstr.toInt();
}
inputString = "";
}
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Type something and press Enter:");
}
void loop() {
if (stringComplete) {
Serial.print("You entered: ");
Serial.println(inputString);
Split(inputString,';');
// PARALLEL
for ( uint8_t z = 0; z < NumPumps; z++ ) {
if ( Pumpe[z].Menge > 0 && Pumpe[z].Status == false) {
Pumpe[z].Status = true;
digitalWrite(Pumpe[z].PIN, HIGH);
Pumpe[z].start_millis = millis();
}
}
for ( uint8_t z = 0; z < NumPumps; z++ ) {
if ( Pumpe[z].Status == true && curr_millis - Pumpe[z].start_millis >= Pumpe[z].Menge*1000 ) {
Pumpe[z].Status = false;
digitalWrite(Pumpe[z].PIN, LOW);
Pumpe[z].start_millis = 0;
PumpenAn--;
}
}
if ( PumpenAn == 0 ) {
stringComplete = false;
Serial.println("fertig");
}
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == 'S') {
inputString = "";
readmore = true;
} else {
if ( readmore ) {
if (inChar == '\n' || inChar == 'E' ){
stringComplete = true;
readmore = false;
} else {
inputString += inChar;
}
}
}
}
}