Disable counter like reset

Hi everyone

Someone can help explain why I can"t use my digitalWrite ledPin from bluetooth to disable my function like a example

void Contador()

{

leitura_B = digitalRead(pinoBotao_B); if (leitura_B == HIGH && ledPin == HIGH ) // Here I wanna to compare my status from led that was on over bluetooth { contador_B = contador_B++; //Incrementa o valor do contador Serial.print("Contador_B : "); Serial.println(contador_B);

if (contador_B == valor_B) { digitalWrite (flipflop_B, HIGH); // delay (500); // digitalWrite (flipflop_B, LOW);

contador_B = 0;

}

}

}

void LM35 ()

{

lcd.setCursor(1, 1); temp = analogRead (tempPin); temp = temp * 0.48828125;

lcd.print (temp); lcd.print (" C "); delay (1000);

}

void Led_Bluetooth() { while ( Serial.available() ) { // Indica que ha dados no buffer de transferencia delay( 3 ); char c = Serial.read(); readString += c; // Constroi uma string - “on” or “off”

} if ( readString.length() >0 ) { Serial.println( readString ); if ( readString == "on" ) { digitalWrite( ledPin, HIGH) ; } if ( readString == "off" ) { digitalWrite( ledPin, LOW ); } readString=""; }

}

1 Auto format your code!

  1. Repost you formatted code in code tags

  2. Post all your code!

Mark

My whole code

// Programa : Liga desliga chuveiro

int pinoBotao_B = 3; // Flip flop que é acionado pelo botão de partida
int flipflop_B = 7; // Flip flop que envia feedback de acionamentos esgotado

int leitura_B; // Armazena o valor do contador
int valor_B = 4; // Variável de comparação
int contador_B = 0; // inicia contador em 0

// Declaração do LCD e LM35 para tmperatura
#include <LiquidCrystal.h>
// Biblioteca para cristal liquido
LiquidCrystal lcd(12, 11, 17, 16, 15, 14);
float temp; // Variável futuante
int tempPin = 4; // Memória de leitura analógica

int ledPin = 4; // Pino de saída para led que indica by pass ativo
String readString;

void setup()
{
// Declaração para contador de impulsos
pinMode(pinoBotao_B, INPUT); // Entrada de pino 2 que recebe acionamento do flipflop botão de partida
pinMode(flipflop_B, OUTPUT); // Saída de pino 8 que desabilita alimentação do flipflop

// Declaração para LM35
lcd.begin(16, 2);
lcd.print(“TEMPERATURA”); // Mensagem principal

pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}

void loop()
{

Contador();
LM35 ();
Led_Bluetooth();

}

void Contador()

{

leitura_B = digitalRead(pinoBotao_B);
if (leitura_B == HIGH && ledPin == true ) // Here I wanna to compare
{
contador_B = contador_B++; //Incrementa o valor do contador
Serial.print("Contador_B : ");
Serial.println(contador_B);

if (contador_B == valor_B)
{
digitalWrite (flipflop_B, HIGH);
// delay (500);
// digitalWrite (flipflop_B, LOW);

contador_B = 0;

}

}

}

void LM35 ()

{

lcd.setCursor(1, 1);
temp = analogRead (tempPin);
temp = temp * 0.48828125;

lcd.print (temp);
lcd.print (" C ");
delay (1000);

}

void Led_Bluetooth()
{
while ( Serial.available() )
{ // Indica que ha dados no buffer de transferencia
delay( 3 );
char c = Serial.read();
readString += c; // Constroi uma string - “on” or “off”

}
if ( readString.length() >0 )
{
Serial.println( readString );
if ( readString == “on” )
{
digitalWrite( ledPin, HIGH) ;
}
if ( readString == “off” )
{
digitalWrite( ledPin, LOW );
}
readString="";
}

}

  leitura_B = digitalRead(pinoBotao_B);   
    if (leitura_B == HIGH && ledPin == HIGH )

Is ledPin the pin number or the state of the pin? You have a digitalRead for the pinoBotao, and you are comparing the result of that read to HIGH. That's good. But there's no digitalRead shown for the ledPin. And with a name like ledPin, I assume it is actually a variable holding the pin number and not the state.

Yes you'r right but my Led get on by bluetooth, so I want to compare my led status in that function as you can see

Do you know what can I do to fix it?/

You can't do this:

    contador_B = contador_B++; //Incrementa o valor do contador

The behavior of that statement is undefined which means that anything can happen.

I have a great skills PLC with,where I can use the output status to control another function

Do you have suggestion to put my code run ok?/

RicardoLadislau:
Yes you’r right but my Led get on by bluetooth, so I want to compare my led status in that function as you can see

Well, the bluetooth doesn’t change the pin number does it? So testing the pin number is pretty ridiculous.

Maybe test the state of that pin with a digitalRead? That will tell you if it is written HIGH or LOW.

   void Led_Bluetooth()
{
  while ( Serial.available() )
{    // Indica que ha dados no buffer de transferencia
    delay( 3 ); 
    char c = Serial.read();
    readString += c;         // Constroi uma string - "on" or "off"
     
}
  if ( readString.length() >0 )
   {
    Serial.println( readString );
    if ( readString == "on" )
     {
      digitalWrite( ledPin, HIGH) ;
      }
    if ( readString == "off" )
     {
      digitalWrite( ledPin, LOW );
      }
    readString="";
   }

A better solution would be in this section. Instead of directly reacting to set the pin HIGH or LOW, make a variable to hold the state you want and then set the pin to that state at the end of this section. Then you can test against that variable.

int ledPinState = LOW;    // This line should be at top of program at global scope...

   void Led_Bluetooth()
{
  while ( Serial.available() )
{    // Indica que ha dados no buffer de transferencia
    delay( 3 ); 
    char c = Serial.read();
    readString += c;         // Constroi uma string - "on" or "off"
     
}
  if ( readString.length() >0 )
   {
    Serial.println( readString );
    if ( readString == "on" )
     {
      ledPinState = HIGH;
      }
    if ( readString == "off" )
     {
      ledPinState = LOW;
      }
    readString="";
    digitalWrite(ledPin, ledPinState);
   }

Sorry I can't to do that couse have no spare Pin to put like digitaRead

in my project all Pins are busy

that's why I am using the serial resource

RicardoLadislau: Sorry I can't to do that couse have no spare Pin to put like digitaRead

None of the solutions posted require any additional pins. What are you talking about?

You know you can do a digitalRead on an output pin and it will tell you how it has been set. A pin doesn't have to be an INPUT to call digitalRead on it.

Sorry my mistakes you'r right once again I did exectaly you said

it's working good now

I'm so great full for attention

I'm gonna go on with my project

have great time