Reading multiple flowmeters, please help !!

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; 
}

65 is the ASCII code for 'A' - is that possibly what you are seeing?

Please post an example of what you are sending to the Arduino. The code in your function parseData() does not lot correct to me - it is not in the same style as the code in my Serial Input Basics tutorial.

...R

Of course I leave the images and it is different but I am trying to do is that every time I receive a data I can identify it according to the letter that accompanies it to be able to assign it to the corresponding flowmeter

Images from Reply #2 so we don't have to download them. See this Simple Image Posting Guide

...R

Please don't post images of text. As you can see the first one is completely unreadable. Just copy and paste the text - simpler for you and much more useful for us.

...R

One issue I can see would be the multiple attach interrupt in the following lines:

   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);

The number of interrupt pins are limited, and you must use the right ones. For example on atmega328 there are only 2 interrupt pins.
You could add some hardware to create an OR gate with 5 input and one output. The output would go to one interrupt pin. Just google for something like "or gate with multiple inputs"; I think you could do this simply with a diode coming from a sensor and have the cathode tied together that goes to the output. It might work with resistors too.
After that you read the sensors with digital read to see which one triggered.

If the extra hardware is not an option why don't use polling: just read the sensors in the loop function

Hope this helps

Hi angarcia678,

still interested in support?

If yes - what is the maximum-frequency that can occur on the flow-meters? If this frequency is not too high and the pulse-length is not too short repeated polling of each inputpin might be a solution too.

best regards

Stefan