So. Ich habe den Code jetzt mal auf das wesentliche verkleinert um euch den ganzen Prozess zu zeigen um den es mir geht. Der Rest was drum herum passiert ist dafür irrelevant.
Kann mir jemand sagen, ob das so passen würde? (Ich kann es zur Zeit noch nicht testen, da die Teile erst bestellt wurden und auf dem weg sind, deswegen möchte ich euch Erfahrene mal fragen
)
bool PROCESS, SINGLE;
int outp_speed;
int process_speed;
int process_tank1;
bool cont1_1, cont1_2, cont1_3;
int single_status;
#define pump1 2 //Pump PWM output pin pump container 1
#define pump2 3 //Pump PWM output pin pump container 2
#define pump3 4 //Pump PWM output pin pump container 3
#define valve1 34 //Valve DIGITAL output pin magnetic valve container 1
#define valve2 35 //Valve DIGITAL output pin magnetic valve container 2
#define valve3 36 //Valve DIGITAL output pin magnetic valve container 3
#define flow1 37
#define flow2 38
#define flow3 39
void setup() {
pinMode(pump1, OUTPUT);
pinMode(pump2, OUTPUT);
pinMode(pump3, OUTPUT);
pinMode(valve1, OUTPUT);
pinMode(valve2, OUTPUT);
pinMode(valve3, OUTPUT);
pinMode(flow1, INPUT);
pinMode(flow2, INPUT);
pinMode(flow3, INPUT);
}
void loop() {
if (PROCESS == true){ //Wird im Menü ausgewählt. Wenn TRUE dann wird Dosiert
//outp_speed & cont1_1, cont 1_2, cont1_3 werden in einem Menü festgelegt
if (outp_speed == 1) process_speed = 60; //Dosing speed = slow. Zeit in ms bis pwm hochzählt
if (outp_speed == 2) process_speed = 40; //Dosing speed = normal. Zeit in ms bis pwm hochzählt
if (outp_speed == 3) process_speed = 20; //Dosing speed = fast. Zeit in ms bis pwm hochzählt
if (cont1_1 == true) process_tank1 = 1;
if (cont1_2 == true) process_tank1 = 2;
if (cont1_3 == true) process_tank1 = 3;
if (SINGLE == true){ //Single Betrieb: Nur 1 Behälter wird dosiert, Wird im Menü festgelegt
single_status = SingleProcess(process_tank1, outp_amount, process_speed);
if (single_status >= 100){ //wenn dosierprozess fertig (100%)
SINGLE = false;
}
}
}
int SingleProcess(int tank, int amount, int speed){
int dosing = amount;
if (tank == 1) status = startPump1(dosing, speed, pump1, valve1, flow1); //wenn Tank 1 ausgewählt, soll Pumpe 1 starten bis 'dosing' also der Sollwert erreicht ist
if (tank == 2) status = startPump2(dosing, speed, pump2, valve2, flow2);
if (tank == 3) status = startPump3(dosing, speed, pump2, valve3, flow3);
return status //Bringt dauerhaft den aktuellen status des Dosierprozesses von 0-100% zurück
}
int startPump1(int target_flow, int pwm_pause, int pinPump, int pinValve, int pinFlow){
int pwm;
int x = 1;
int status = 0;
unsigned long prevmillis = 0;
digitalWrite(pinValve, HIGH); //Magnetventil auf
for (pwm = 1; pwm >= 0; pwm += x){ //for schleife wird beendet, sobald pwm auf 0 ist (Pumpe aus)
if (x == 1 && pwm == 50) pwm_pause = pwm_pause / 2; //Verringert die Dauer die es braucht um den pwm wert zu ändern
if (x == -1 && pwm == 50) pwm_pause = pwm_pause * 2; //Erhöht die Dauer die es braucht um den pwm wert zu ändern
if (millis()- prevmillis >= pwm_pause){
analogWrite(pinPump, pwm);
actual_flow = FlowSensor1(pinFlow); //holt sich die aktuelle bisher Dosierte Menge zurück
if (pwm == 255 || pwm == 0) x = 0; //Pumpe bleibt auf 100% bis die Hälfte des SOllwerts erreicht ist
if ((target_flow/2) <= actual_flow) x = -1; //wenn hälfte von Sllwert erreicht fährt pumpe runter
prevmillis = millis();
status = (actual_flow / target_flow) * 100;
}
}
digitalWrite(pinValve, LOW); //magnetventil zu
return status; //gibt den Status des Dosierprozesses von 0-100% zurück
}
//FlowSensor gibt Impulse raus, wenn Wasser durch ihn durchströmt. 3800 Impulse pro 1L
int FlowSensor1(int pinFlow){
int amount;
int counter;
static int prevValue;
int currValue = digitalRead(pinFlow);
if (currentValue != prevValue){ //wird ein Impuls festgestellt, zählt der counter +1 hoch
counter++;
amount = (counter - 0) * (1000 - 0) / (3800 - 0) + 0; //Normierung der Impulse auf 0-1000ml
prevValue = currentValue;
return amount; //gibt bisherige Dosierung in ml zurück
}
}
Die Variablen process_speed & process_tank werden noch im MIX Process verwendet, mit dem man 2 Behälter mischen kann. Deswegen stehen die im Loop.