Search code examples
firebasefirebase-realtime-databasearduinoarduino-esp8266

Arduino wifi > firebase and more than 1 LED control


Can you please help me with below code. It works with 1 LED but not wit 2 LEDs / pins. On pin 3 works, but not on pin 5. Any suggestions?

I tried many different things but am stuck. Something isn't right inside loop. I think that will help many persons here on forum.

Snippet correction would be great or just tell me what to do for solving that problem.

Arduino code:

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

// Set these to run example.
#define FIREBASE_HOST "test839785093353.firebaseio.com"
#define FIREBASE_AUTH "685g4d65d4g65d4g654TESTsf354s6f531sf531s"

//Change line with your WiFi router name and password
#define WIFI_SSID "Internet"  
#define WIFI_PASSWORD "password123123"


void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);

pinMode(5, OUTPUT);

// connect to 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);

// ob povezavi inicializiras status
Firebase.set("LED_STATUS", 0);
Firebase.set("LED2_STATUS", 0);
}
int led1 = 0;
int led2 = 0;
// int led3 = 0;


void loop() {
// get value
led1 = Firebase.getInt("LED_STATUS");

led2 = Firebase.getInt("LED2_STATUS");

// handle error
if (led1==1) {
Serial.println("LED 1 ON");
digitalWrite(3,HIGH);  
return;
delay(10);
}
else {
Serial.println("LED 1 OFF");
digitalWrite(3,LOW);  
return;
}

   // se en if..else stavek
if (led2==1) {
Serial.println("LED 2 ON");
digitalWrite(5,HIGH);  
return;
delay(10);
}
else {
Serial.println("LED 2 OFF");
digitalWrite(5,LOW);  
return;
}


}

Solution

  • The reason for this is the if else seatement that you have used for LED1. If led1 is 1, it turns on led1 and exits the loop() function. If led1 is 0, it turns off led1 and exits the loop() function.

    The code never reaches led2.

    Try this change

    if (led1==1) {
    Serial.println("LED 1 ON");
    digitalWrite(3,HIGH);  
    delay(10);
    }
    else {
    Serial.println("LED 1 OFF");
    digitalWrite(3,LOW);  
    }
    
       // se en if..else stavek
    if (led2==1) {
    Serial.println("LED 2 ON");
    digitalWrite(5,HIGH);  
    delay(10);
    }
    else {
    Serial.println("LED 2 OFF");
    digitalWrite(5,LOW); 
    }