Stop/Start button analog readings

Hi

I connect 6 buttons using various resistances to 1 analog pin and is working fine and each button will have different behaviours.
When one button is punched the process of that button begins and i want to block readings if other buttons are pressed. Is there a way when i press a button stop analog readings and in the end of the process start analog readings again from A0 (button pin readings)?

Below is the loop code i have to the buttons. Thanks in advance

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Definição de Botões

  int botao = analogRead(A0);

  if (botao > 10) {
    if (botao > 560)    {
      delay(200);
      last_botao = 2;
    }

    else if (botao > 510)    {
      delay(200);
      last_botao = 1;
    }
  }



  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<>>>>>>>>>>ARRANQUE DO CONTADOR E ZERAR A CONTAGEM
  if (last_botao > 0)    {
    contagem_pulsos();
  }
  if (last_botao == 0)    {
    contagem_total = 0;
    flow_frequency = 0;
  }



  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>COMPORTAMENTO DOS BOTÕES

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




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

Control whether the buttons are read using a boolean variable. Only read the buttons when the boolean is true

Set the boolean to true initially

When a button press is detected set the boolean to false and don't set it back to true until you are ready to read another button press

Just don't read the analog pin while your process is running.

Usually, the problem is the other way round - people want their system to respond to a button press promptly.

Hi again

I adapt the code with a "bool botao_ocupado = false;" and put the conditions but when i press other button they are still responding.

Sorry I ask but can you tell me if i put the conditions in the right way? Thanks

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Definição de Botões

  if (botao_ocupado == false) {
    botao = analogRead(A0);
  }


  if (botao > 10) {

    if (botao > 560)    {
      delay(200);
      last_botao = 2;
    }
    else if (botao > 510)    {
      delay(200);
      last_botao = 1;
    }
  }



  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<>>>>>>>>>>ARRANQUE DO CONTADOR E ZERAR A CONTAGEM
  if (last_botao > 0)     {
    botao_ocupado = !botao_ocupado;
    contagem_pulsos();
  }
  if (last_botao == 0)    {
    contagem_total = 0;
    flow_frequency = 0;
  }



  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>COMPORTAMENTO DOS BOTÕES

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




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

Print botao_ocupado before you test it. Is the value what you expect ?

Why are you inverting the state of botao_ocupado instead of setting its value explicitly ?

I switch the values to true/false instead of inverting the state as recommended end print the result and the result is ok . However the process of the relays do not start.

if (botao_ocupado == false) {
    botao = analogRead(A0);
  }


  if (botao > 10) {

    if (botao > 560)    {
      delay(200);
      last_botao = 2;
    }
    else if (botao > 510)    {
      delay(200);
      last_botao = 1;
    }
  }



  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<>>>>>>>>>>ARRANQUE DO CONTADOR E ZERAR A CONTAGEM
  if (last_botao > 0)     {
    botao_ocupado = true;
    contagem_pulsos();
  }
  
  if (last_botao == 0)    {
    contagem_total = 0;
    flow_frequency = 0;
  }



  //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>COMPORTAMENTO DOS BOTÕES

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




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

Hi

Found the problem. The condition need to be a level below. Thanks for the time and patience.

Please stop posting snippets of code and post complete programs so that problems can be seen in context

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