Proposed programmers: turn on and off a led with two buttons. It is not a gate

This topic is very interesting, I will explain. I am working with NodeMCU v3 + App inventor + Matrix keyboard
The objective is to turn on and off a security system (a LED), through a button in the inventor app, and thinking that the user could run out of mobile data or without battery. be able to manually activate and deactivate the alarm system.

The communication through app inventor and the database works excellent, the matrix keyboard works very well independently.

I will summarize the problem as follows. I need to turn on and off a led with two buttons. Both buttons can turn the LED on or off. according to the previous state.

I share my entire program, I hope you can help me

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <FirebaseArduino.h>

// CONEXION BASE DE DATOS Y CONEXION WIFI DEL MODULO
#define FIREBASE_HOST “XXX”
#define FIREBASE_AUTH “XXX”
#define WIFI_SSID “XXX”
#define WIFI_PASSWORD “XXX”

#define pin01 02//D2
#define pin02 05//D1 ENTRADA DE ARDUINO

void setup() {

Serial.begin(9600);

// SE CONECTA A WIFI
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print(“connecting”);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());

Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
pinMode(02, OUTPUT);
pinMode(05, INPUT);
}

char val;
int n = 0;
int deg1;
//#define pin01 02//D2
//#define pin02 01 //D1 ENTRADA DE ARDUINO

void loop() {

//…HERE IS THE PROBLEM

//The result is that the LED turns on and off, because the two states send the signal

val = digitalRead(pin02);
deg1=Firebase.getString(“deger1”).toInt();
Serial.println(val);

if(deg1==0)
{
digitalWrite(pin01,HIGH);
Firebase.setString(“ESTADOACTUAL”, “DESACTIVADO”);
delay(100);

}
if(val==LOW)
{
digitalWrite(pin01,HIGH);
Firebase.setString(“ESTADOACTUAL”, “DESACTIVADO”);
delay(100);

}

if(deg1==1)
{
digitalWrite(pin01,LOW);
Firebase.setString(“ESTADOACTUAL”, “ACTIVADO”);
delay(100);

}
if(val==HIGH)
{
digitalWrite(pin01,LOW);
Firebase.setString(“ESTADOACTUAL”, “ACTIVADO”);
delay(100);

}
if (Firebase.failed())

{
Serial.print(“setting /CONEXION NO ESTABLECIDA:”);
Serial.println(Firebase.error());
delay(100);

}

}

Please read the forum guide in the "please read" sticky post. Then modify your post above. It is breaking forum rules.

Try this logic I took from the BkinkWithout Delay sketch.
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
This can be part of the button detect or even a function.
Good Luck & Have Fun!
Gil

If what you want to do is invert the state of the LED then until recently I would have done

digitalWrite(ledPin, !digitalRead(ledPin));

However, there are some possible changes afoot that might stop that working so

ledState = !ledState;
digitalWrite(ledPin, ledState);

is an alternative

UKHeliBob:

digitalWrite(ledPin, !digitalRead(ledPin));

However, there are some possible changes afoot that might stop that working

Huh? Please elaborate!

Please elaborate!

See Breakage caused by PinStatus and PinMode types · Issue #25 · arduino/ArduinoCore-API · GitHub

UKHeliBob:
See Breakage caused by PinStatus and PinMode types · Issue #25 · arduino/ArduinoCore-API · GitHub

Oh dear... It's almost as big a tyre-fire as Brexit!

Hadn't seen this before. I absolutely get the concept and principle behind it. But the thought of the consequences gives me a squirmy feeling in my stomach. All those times I wrote

digitalWrite(pin, bitRead(v, i));

I found out about it when I suggested inverting the digitalRead() of a pin and someone pointed out the possible change to me.