Good morning everyone, I am trying to create a software that allows me to control 5 solenoid valves by reading the flowmeters, the problem is when I assign the number of liters that will be used for any of the valves the program begins to show the amount of 65 on the serial monitor, the problem is that I must be able to use all at the same time or only one and assigning a different quantity of liters to each one, for that I use different pins on the arduino corresponding to the interruptions.
The code is as follows, also attached a picture of the interface in c #:
volatile int pulsos1 = 0;
volatile int pulsos2 = 0;
volatile int pulsos3 = 0;
volatile int pulsos4 = 0;
volatile int pulsos5 = 0;
float litros_seg1;
float litros_seg2;
float litros_seg3;
float litros_seg4;
float litros_seg5;
unsigned long tiempoAnterior = 0; // Variable para calcular el tiempo transcurrido
unsigned long pulsos_Acumulados1 = 0;
unsigned long pulsos_Acumulados2 = 0;
unsigned long pulsos_Acumulados3 = 0;
unsigned long pulsos_Acumulados4 = 0;
unsigned long pulsos_Acumulados5 = 0;
float litros1;
float litros2;
float litros3;
float litros4;
float litros5;
int hallsensor1 = 2;
int hallsensor2 = 3;
int hallsensor3 = 18;
int hallsensor4 = 19;
int hallsensor5 = 20;
int pinRele1=8;
int pinRele2=9;
int pinRele3=10;
int pinRele4=11;
int pinRele5=12;
float caudalTope1;
float caudalTope2;
float caudalTope3;
float caudalTope4;
float caudalTope5;
const byte numChars = 64;
char receivedChars[numChars];
char tempChars[numChars];
boolean newData = false;
void rpm1 () {
pulsos1++;
}
void rpm2 () {
pulsos2++;
}
void rpm3 () {
pulsos3++;
}
void rpm4 () {
pulsos4++;
}
void rpm5 () {
pulsos5++;
}
void setup() {
Serial.begin(9600);
pinMode(pinRele1, OUTPUT);
pinMode(pinRele2, OUTPUT);
pinMode(pinRele3, OUTPUT);
pinMode(pinRele4, OUTPUT);
pinMode(pinRele5, OUTPUT);
sei();
attachInterrupt(digitalPinToInterrupt(hallsensor1), rpm1, RISING);
attachInterrupt(digitalPinToInterrupt(hallsensor2), rpm2, RISING);
attachInterrupt(digitalPinToInterrupt(hallsensor3), rpm3, RISING);
attachInterrupt(digitalPinToInterrupt(hallsensor4), rpm4, RISING);
attachInterrupt(digitalPinToInterrupt(hallsensor5), rpm5, RISING);
tiempoAnterior = millis();
}
void loop () {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
newData = false;
}
if (millis() - tiempoAnterior > 1000) {
while(litros1 < caudalTope1) {
pulsos_Acumulados1 += pulsos1;
litros_seg1 = litros_seg1 + ((pulsos1 / 7.5) / 60);
litros1 = pulsos_Acumulados1*1000/450.0;
digitalWrite(pinRele1,LOW);
Serial.println(litros_seg1 + 'A');
}
while(litros2 < caudalTope2) {
pulsos_Acumulados2 += pulsos2;
litros_seg2 = litros_seg2 + ((pulsos2 / 7.5) / 60);
litros2 = pulsos_Acumulados2*1000/450.0;
digitalWrite(pinRele2,LOW);
Serial.println(litros_seg2+ 'B');
}
while(litros3 < caudalTope3) {
pulsos_Acumulados3 += pulsos3;
litros_seg3 = litros_seg3 + ((pulsos3 / 7.5) / 60);
litros3 = pulsos_Acumulados3*1000/450.0;
digitalWrite(pinRele3,LOW);
Serial.println(litros_seg3+ 'C');
}
while(litros4 < caudalTope4) {
pulsos_Acumulados4 += pulsos4;
litros_seg4 = litros_seg4 + ((pulsos4 / 7.5) / 60);
litros4 = pulsos_Acumulados4*1000/450.0;
digitalWrite(pinRele4,LOW);
Serial.println(litros_seg4+ 'D');
}
while(litros5 < caudalTope5) {
pulsos_Acumulados5 += pulsos5;
litros_seg5 = litros_seg5 + ((pulsos5 / 7.5) / 60);
litros5 = pulsos_Acumulados5*1000/450.0;
digitalWrite(pinRele5,LOW);
Serial.println(litros_seg5+ 'E');
}
digitalWrite(pinRele1,HIGH);
digitalWrite(pinRele2,HIGH);
digitalWrite(pinRele3,HIGH);
digitalWrite(pinRele4,HIGH);
digitalWrite(pinRele5,HIGH);
litros1 = 0;
litros2 = 0;
litros3 = 0;
litros4 = 0;
litros5 = 0;
litros_seg1 = 0.0;
litros_seg2 = 0.0;
litros_seg3 = 0.0;
litros_seg4 = 0.0;
litros_seg5 = 0.0;
pulsos_Acumulados1 = 0;
pulsos_Acumulados2 = 0;
pulsos_Acumulados3 = 0;
pulsos_Acumulados4 = 0;
pulsos_Acumulados5 = 0;
pulsos1 = 0;
pulsos2 = 0;
pulsos3 = 0;
pulsos4 = 0;
pulsos5 = 0;
tiempoAnterior = millis();
}
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0';
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData() {
char * strtokIndx;
strtokIndx = strtok(tempChars,"a");
caudalTope1 = atoi(strtokIndx) * 1000;
strtokIndx = strtok(tempChars, "b");
caudalTope2 = atoi(strtokIndx) * 1000;
strtokIndx = strtok(tempChars, "c");
caudalTope3 = atoi(strtokIndx) * 1000;
strtokIndx = strtok(tempChars, "d");
caudalTope4 = atoi(strtokIndx) * 1000;
strtokIndx = strtok(tempChars, "e");
caudalTope5 = atoi(strtokIndx) * 1000;
}