joining 2 codes together

I have done the first code to calculate 2 different water flows, (2 tubes parallel piping) tested last week, and it was ok. I calibrated G1/2" flow sensor to get the curves with these very sofisticated equipments


then, I have proceeded with temperature readings (4 x LM35) and I have tested too.

But when I tried joining both codes today, it just did not work and I got no debug information. The serial monitor stays the same over and over again.

the 2 codes works properly alone, but when I join it does not work. Why is this happening?
ps: I am new to this, sorry for my beginners structures, but I tried do to my best.

int analogPin0 = 0; // lm35 está ligado a entrada analogica 0
int analogPin1 = 1; 
int analogPin3 = 3;
int analogPin5 = 5;
int valAnalog0, valAnalog1, valAnalog3, valAnalog5 ; // variavel para armazenar o valor analogico lido
float T0, T1, T3, T5;
int tmp0, tmp1, tmp3, tmp5;
volatile int frequencia1;   //measuring the rising edges of the signal 1 
volatile int frequencia2;
float Q1, Q2;                               
int fluxo1 = 2;    //The pin location of the sensors
int fluxo2 = 3;

void Hz1 ()     //This is the function that the interrupt calls
{ 
  frequencia1++;  //This function measures the rising and falling edge of the hall effect sensors signal
} 
void Hz2 ()
{ 
  frequencia2++;  
} 

void setup()
{
 Serial.begin(9600); // programa a serial para comunicação em 9600 bps
 analogReference(INTERNAL); // passando de 5,0V para1 1,1V o sistema pra aumentar a resolução
 pinMode(fluxo1, INPUT); //initializes digital pin 2 as an input
 pinMode(fluxo2, INPUT);
 attachInterrupt(0, Hz1, RISING); //and the interrupt is attached (for UNO R3, digital port, int.0 = pin2)
 attachInterrupt(1, Hz2, RISING);//for UNO R3, digital port, int.1 = pin3
}


void loop()
{
 tmp0 = 0; 
 for(int x = 0; x <100; x++){ // o loop faz 100 leituras para diminuir os erros
 valAnalog0 = analogRead(analogPin0); // Le o pino de entrada analogica 0
 tmp0 = tmp0 + valAnalog0;
 }
 tmp1=0; 
 for(int x = 0; x <100; x++){ // o loop faz 100 leituras para diminuir os erros
 valAnalog1 = analogRead(analogPin1); // Le o pino de entrada analogica 0
 tmp1 = tmp1 + valAnalog1;
 }
 tmp3=0;
 for(int x = 0; x <100; x++){ // o loop faz 100 leituras para diminuir os erros
 valAnalog3 = analogRead(analogPin3); // Le o pino de entrada analogica 0
 tmp3 = tmp3 + valAnalog3;
 }
 tmp5=0;
 for(int x = 0; x <100; x++){ // o loop faz 100 leituras para diminuir os erros
 valAnalog5 = analogRead(analogPin5); // Le o pino de entrada analogica 0
 tmp5 = tmp5 + valAnalog5;
 }

T0= (( 1.1 * tmp0 * 100.00) / 1024) / 100 ; // calcula a temperatura
T1= (( 1.1 * tmp1 * 100.00) / 1024) / 100;
T3= (( 1.1 * tmp3 * 100.00) / 1024) / 100;
T5= (( 1.1 * tmp5 * 100.00) / 1024) / 100;

frequencia1 = 0;	//Set frequencia1 
  sei();		//Enables interrupts
  delay (3000);	        //3 seconds sinal acquisition
  cli();		//Disable interrupts
  
  Q1 = -7.0295319E-03 + 3.92917841E-02*frequencia1; 
  //equation have been linearized under that condition: 
  //3000ms acquisition time, (0,4 - 12,0)L/min. range, horizontal position sensor.
  // fluid water. temp. 21C.
  if (Q1<0)  {
    Q1=0;
  }
frequencia2 = 0;	//Set frequencia2 
  sei();		//Enables interrupts
  delay (3000);	        //3 seconds sinal acquisition
  cli();		//Disable interrupts
  Q2 = -7.02295319E-03 + 3.92917841E-02*frequencia2;
  if (Q2<0){
    Q2=0;
  }
  
  
Serial.print("Thi= ");
Serial.print(T0,3); // Envia os dados pela porta serial
Serial.print(" [C]    ");
Serial.print("Tho= ");
Serial.print(T1,3);
Serial.print(" [C]        ");
Serial.print("Tci= ");
Serial.print(T3,3); // Envia os dados pela porta serial
Serial.print(" [C]    ");
Serial.print("Tco= ");
Serial.print(T5,3);
Serial.print(" [C]        ");
Serial.print("Qh = ");
Serial.print(Q1, 3); //Prints the number calculated in the equation
Serial.print(" [L/min.]    "); //Prints "L/min." 
Serial.print("Qc= ");
Serial.print(Q2, 3); //Prints the number calculated above
Serial.println (" [L/min.]"); //Prints "L/min" and returns a new line 

delay(500);


}
frequencia2 = 0;	//Set frequencia2 
  sei();		//Enables interrupts
  delay (3000);	        //3 seconds sinal acquisition
  cli();		//Disable interrupts
  Q2 = -7.02295319E-03 + 3.92917841E-02*frequencia2;
  if (Q2<0){
    Q2=0;
  }

Notice that you left the interrupts disabled. Not only will this mess with the Serial stuff, but the delay(500) will never come back. Timer interrupts aren’t happening.

Why don’t you add an sei() statement shortly after you’re done using frequencia2?

 tmp0 = 0; 
 for(int x = 0; x <100; x++){ // o loop faz 100 leituras para diminuir os erros
 valAnalog0 = analogRead(analogPin0); // Le o pino de entrada analogica 0
 tmp0 = tmp0 + valAnalog0;
 }

analogRead() returns a value between 0 and 1023. Adding 100 of those will result in a value between 0 and 102300. The upper limit is far beyond the range that fits in an int.

Why is valAnalog0 global?

T0= (( 1.1 * tmp0 * 100.00) / 1024) / 100 ; // calcula a temperatura
T1= (( 1.1 * tmp1 * 100.00) / 1024) / 100;
T3= (( 1.1 * tmp3 * 100.00) / 1024) / 100;
T5= (( 1.1 * tmp5 * 100.00) / 1024) / 100;

I’d prefer NOT to see mixed mode arithmetic here. Use 1024.0 and 100.0, instead.

Assuming you are using an Atmega328, try replacing:

sei() with EIMSK = 3;

and

cli() with EIMSK = 0;

That way you only disable the external interrupts, rather than the all of the other many interrupts like those used for delay() and Serial.print()

thank you for the answers. I realize how boring should be understand what I tried to do for. It seems interrupts problem is going on my sketch. I will try replacing the interrups at first Tom like you have said and I will post the results.

Paul, I did not understand. When I add 100 readings, the mV signal doesn’t get stored in the variable temp0? . Can you explain that to me?

I have set global variable TO, T1, T3 and T5 because I don’t know a smarter way to do that, but I know this is not the better choice. I will keep trying here and I will post the results

TanHadron:

frequencia2 = 0;	//Set frequencia2 

sei(); //Enables interrupts
  delay (3000);         //3 seconds sinal acquisition
  cli(); //Disable interrupts
  Q2 = -7.02295319E-03 + 3.92917841E-02*frequencia2;
  if (Q2<0){
    Q2=0;
  }




Notice that you left the interrupts disabled. Not only will this mess with the Serial stuff, but the delay(500) will never come back. Timer interrupts aren't happening.

Why don't you add an sei() statement shortly after you're done using frequencia2?

I will try! Should be like this?

frequencia2 = 0;	//Set frequencia2 
   delay (3000);	        //3 seconds sinal acquisition
  cli();		//Disable interrupts
  Q2 = -7.02295319E-03 + 3.92917841E-02*frequencia2;
  if (Q2<0){
    Q2=0;
  }
sei();

you’re right. I am using ATMEGA 328

When I add 100 readings, the mV signal doesn't get stored in the variable temp0? . Can you explain that to me?

Your tmp0 variable is defined as an int. An (Arduino) int can hold a number between ?32,768 and 32,767. You read an analogue value 100 times and add it to tmp0. At its highest the analogue value could be 1023. Will 100 * 1023 fit in an int ?

UKHeliBob:

When I add 100 readings, the mV signal doesn't get stored in the variable temp0? . Can you explain that to me?

Your tmp0 variable is defined as an int. An (Arduino) int can hold a number between ?32,768 and 32,767. You read an analogue value 100 times and add it to tmp0. At its highest the analogue value could be 1023. Will 100 * 1023 fit in an int ?

hum, that's make sense to me now. I have thought that only valAnalog couldn't overflow 1023 bites. So, variable tmp0 follows the same range.

I have thought that only valAnalog couldn’t overflow 1023 bites

It can’t, because there are no 1023 bite-sized variables in the Arduino set (or C++, for that matter).

Any single value returned by analogRead can not be less than 0 or greater than 1023.

But, if you add up 100 values that are greater than 328, you will get a total that exceeds 32,800, which exceeds the range of an int.

At a minimum, you need to use a long for the total variables.

I have done a beer homemade heat exchanger for the last 3 months until here and I have built the system to get its data. I am open minded to share experiences if anyone gets interested to. or talking about beer homebrewing too lol. I am facing some troubles with arduino language and I couldn’t do any improvements besides my attemps to control the proccess.

that’s the last one I have done:

int analogPin0 = 0; // lm35 está ligado a entrada analogica 0
int analogPin1 = 1;
int analogPin2 = 2;
int analogPin3 = 3;
int valAnalog0, valAnalog1, valAnalog2, valAnalog3 ; // variavel para armazenar o valor analogico lido
float T0, T1, T2, T3;
int tmp0, tmp1, tmp2, tmp3;
volatile int frequencia1;
volatile int frequencia2;   //measuring the rising edges of the signal
float Q1, Q2;                               
int fluxo1 = 2;    //The pin location of the sensor
int fluxo2 = 3;


void Hz1 ()     //This is the function that the interupt calls
{ 
  frequencia1++;  //This function measures the rising and falling edge of the hall effect sensors signal
} 
void Hz2 ()
{ 
  frequencia2++;  //This function measures the rising and falling edge of the hall effect sensors signal
} 
void setup()
{
Serial.begin(9600); // programa a serial para comunicação em 9600 bps
analogReference(INTERNAL); // passando de 5,0V para1 1,1V o sistema pra aumentar a resolução
pinMode(fluxo1, INPUT); //initializes digital pin 2 as an input
pinMode(fluxo2, INPUT);
attachInterrupt(0, Hz1, RISING); //and the interrupt is attached
attachInterrupt(1, Hz2, RISING);
}


void loop()
{
 tmp0 = 0; 
 for(int x = 0; x <10; x++){ // o loop faz 100 leituras para diminuir os erros
 valAnalog0 = analogRead(analogPin0); // Le o pino de entrada analogica 0
 tmp0 = tmp0 + valAnalog0;
 }
 tmp1=0; 
 for(int x = 0; x <10; x++){ // o loop faz 100 leituras para diminuir os erros
 valAnalog1 = analogRead(analogPin1); // Le o pino de entrada analogica 0
 tmp1 = tmp1 + valAnalog1;
 }
 tmp2=0;
 for(int x = 0; x <10; x++){ // o loop faz 100 leituras para diminuir os erros
 valAnalog2 = analogRead(analogPin2); // Le o pino de entrada analogica 0
 tmp2 = tmp2 + valAnalog2;
 }
 tmp3=0;
 for(int x = 0; x <10; x++){ // o loop faz 100 leituras para diminuir os erros
 valAnalog3 = analogRead(analogPin3); // Le o pino de entrada analogica 0
 tmp3 = tmp3 + valAnalog3;
 }

T0= (( 1.1 * tmp0 * 100.00) / 1024) / 10 ; // better to change readings to 2, 3, 5, 10, 15..
T1= (( 1.1 * tmp1 * 100.00) / 1024) / 10;
T2= (( 1.1 * tmp2 * 100.00) / 1024) / 10;
T3= (( 1.1 * tmp3 * 100.00) / 1024) / 10;

 frequencia1 = 0;	//Set frequencia1 to 0 ready for calculations
  EIMSK = 3;		//Enables interrupts
  delay (3000);	        //Wait 3 seconds
  EIMSK = 0;		//Disable interrupts
  Q1 = -7.0295319E-03 + 3.92917841E-02*frequencia1; // this equation have been linearized on that conditions: 3000ms acquisition time, (0,4 - 12,0)L/min. range, horizontal position sensor. fluid water. temp. 21C.
  if (Q1<0)  {
    Q1=0;
  }

  frequencia2 = 0;	//Set frequencia2 to 0 ready for calculations
  EIMSK = 3;		//Enables interrupts
  delay (3000);	        //Wait 3 seconds
  EIMSK = 0;		//Disable interrupts
  Q2 = -7.02295319E-03 + 3.92917841E-02*frequencia2;
  if (Q2<0){
    Q2=0;
  }

Serial.print ("Qh= ");
Serial.print (Q1, 3); //Prints the number calculated above
Serial.print (" [L/min.]   "); //Prints "L/min." 
Serial.print ("Qc= ");
Serial.print (Q2, 3); //Prints the number calculated above
Serial.print (" [L/min.]        "); 
Serial.print("Thi= ");
Serial.print(T0,3); // Envia os dados pela porta serial
Serial.print(" [C]    ");
Serial.print("Tho= ");
Serial.print(T2,3); // Envia os dados pela porta serial
Serial.print(" [C]        ");
Serial.print("Tci= ");
Serial.print(T3,3);
Serial.print(" [C]   ");
Serial.print("Tco= ");
Serial.print(T1,3);
Serial.println(" [C]    ");






delay(500);


}

I have tried lowering readings number in analogic pin to not exceed analogic range number, like PaulS have said to me. My readings came down to 10 now with a reasonable accuracy for the job.
After that, I have changed sei() and cli() by EIMSK = 3 and EIMSK = 0 like a friend have said too.

but now, when the code gets started, I can see only hot water and cold water flows. I am very confusing with interrupts, I didn’t understand the hole idea yet, when do I need to use interrupts, or sei, or EIMSK. I find it hard handling that command itself.

Where am I doing it wrong?!