Water flow counter start in 0

Hi

I'm using a waterflow sensor to turn on/off relays. The pulses to turn on and off relays depends of wich button is pressed.

The problem is that the flow counter (contagem_total) have to start with value 0 and for some reason the counter is setting to 0 every second and cannot do counting.

Can someone help me detecting the problem? Thanks in advance.

#include <Math.h>
#include <Wire.h>

int pinbotao = analogRead(A0);
int rele_enchimento = 7;
int rele_fim = 3;
int rele_natural = 4;
int rele_fria = 5;


// conta litros
volatile int flow_frequency; // Measures flow sensor pulses
unsigned char flowsensor = 2; // Sensor Input
float contagem_total = 0;



//Variáveis
int last_saida = 0;
int last_fluxo = 0;
int fluxoA = 300;
int fluxoB = 1000;
int last_botao = 0;



void flow () // Interrupt function
{
  flow_frequency++;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);
  pinMode(rele_enchimento, OUTPUT);
  pinMode(rele_fim, OUTPUT);
  pinMode(rele_natural, OUTPUT);
  pinMode(rele_fria, OUTPUT);
  pinMode(flowsensor, INPUT);
  digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
  Serial.begin(9600);
  attachInterrupt(0, flow, RISING); // Setup Interrupt
  sei(); // Enable interrupts

}

void loop() {

  digitalWrite(rele_enchimento, LOW);
  digitalWrite(rele_fim, LOW);
  digitalWrite(rele_natural, LOW);
  digitalWrite(rele_fria, LOW);


  int botao = analogRead(A0);
  if (botao > 500)    {last_botao = 1;}
  if (botao > 300)    {last_botao = 2;}




  if (last_botao == 1) {
    contagem_total = 0;
 {   contagem_pulsos();

    if (contagem_total < fluxoA - 80) {
      digitalWrite(rele_enchimento, HIGH);
      digitalWrite(rele_fim, LOW);
      digitalWrite(rele_natural, HIGH);
    }
    else if (contagem_total < fluxoA) {
      digitalWrite(rele_enchimento, LOW);
      digitalWrite(rele_fim, HIGH);
    }
    else if (contagem_total == fluxoA || contagem_total < fluxoA) {
      digitalWrite(rele_enchimento, LOW);
      digitalWrite(rele_fim, LOW);
      digitalWrite(rele_natural, LOW);
      last_botao = 0;
    }
}
  }




    if (last_botao == 2) {

    contagem_pulsos();

    if (contagem_total < fluxoB - 80) {
      digitalWrite(rele_enchimento, HIGH);
      digitalWrite(rele_fim, LOW);
      digitalWrite(rele_natural, HIGH);
    }
    else if (contagem_total < fluxoB) {
      digitalWrite(rele_enchimento, LOW);
      digitalWrite(rele_fim, HIGH);
    }
    else if (contagem_total == fluxoB || contagem_total < fluxoB) {
      digitalWrite(rele_enchimento, LOW);
      digitalWrite(rele_fim, LOW);
      digitalWrite(rele_natural, LOW);
      last_botao = 0;
      contagem_total = 0;
    }
  }


  Serial.println(botao);
  Serial.println(contagem_total);
}





//contagem de litros
void contagem_pulsos() {

  contagem_total += flow_frequency;
  flow_frequency = 0; // Reset Counter

}

Your loop is running fast (which of course it should do)
The serial output should fill your screen quickly

I don't understand why this code sets to zero once a second.

You should use

attachInterrupt(digitalPinToInterrupt(flowsensor), flow, RISING);

to make sure that the right interrupt gets attached

for using an inputpin with internal pullup you code

  pinMode(flowsensor, INPUT_PULLUP);

and not

  pinMode(flowsensor, INPUT);
  digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up

thre is one pair of curly brackets that are not needed

#include <Math.h>
#include <Wire.h>

int pinbotao = analogRead(A0);
int rele_enchimento = 7;
int rele_fim = 3;
int rele_natural = 4;
int rele_fria = 5;

// conta litros
volatile int flow_frequency; // Measures flow sensor pulses
unsigned char flowsensor = 2; // Sensor Input
float contagem_total = 0;

//Variáveis
int last_saida = 0;
int last_fluxo = 0;
int fluxoA = 300;
int fluxoB = 1000;
int last_botao = 0;

void flow () { // Interrupt function
  flow_frequency++;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin (9600);
  pinMode(rele_enchimento, OUTPUT);
  pinMode(rele_fim, OUTPUT);
  pinMode(rele_natural, OUTPUT);
  pinMode(rele_fria, OUTPUT);
  pinMode(flowsensor, INPUT);
  digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
  Serial.begin(9600);
  attachInterrupt(0, flow, RISING); // Setup Interrupt
  sei(); // Enable interrupts
}

void loop() {

  digitalWrite(rele_enchimento, LOW);
  digitalWrite(rele_fim, LOW);
  digitalWrite(rele_natural, LOW);
  digitalWrite(rele_fria, LOW);

  int botao = analogRead(A0);
  if (botao > 500)    {
    last_botao = 1;
  }
  if (botao > 300)    {
    last_botao = 2;
  }

  if (last_botao == 1) {
    contagem_total = 0;
    { contagem_pulsos(); // this curly bracket is too  much

      if (contagem_total < fluxoA - 80) {
        digitalWrite(rele_enchimento, HIGH);
        digitalWrite(rele_fim, LOW);
        digitalWrite(rele_natural, HIGH);
      }
      else if (contagem_total < fluxoA) {
        digitalWrite(rele_enchimento, LOW);
        digitalWrite(rele_fim, HIGH);
      }
      else if (contagem_total == fluxoA || contagem_total < fluxoA) {
        digitalWrite(rele_enchimento, LOW);
        digitalWrite(rele_fim, LOW);
        digitalWrite(rele_natural, LOW);
        last_botao = 0;
      }
    }
  }

  if (last_botao == 2) {
    contagem_pulsos();

    if (contagem_total < fluxoB - 80) {
      digitalWrite(rele_enchimento, HIGH);
      digitalWrite(rele_fim, LOW);
      digitalWrite(rele_natural, HIGH);
    }
    else if (contagem_total < fluxoB) {
      digitalWrite(rele_enchimento, LOW);
      digitalWrite(rele_fim, HIGH);
    }
    else if (contagem_total == fluxoB || contagem_total < fluxoB) {
      digitalWrite(rele_enchimento, LOW);
      digitalWrite(rele_fim, LOW);
      digitalWrite(rele_natural, LOW);
      last_botao = 0;
      contagem_total = 0;
    }
  }
  Serial.println(botao);
  Serial.println(contagem_total);
}

//contagem de litros
void contagem_pulsos() {
  contagem_total += flow_frequency;
  flow_frequency = 0; // Reset Counter
}

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.