The loop () function and Firebase function doesn't work for Arduino

Hope you are well. I have a problem with my Arduino code. I have LED_Function() which is working in Loop () fine, what I want, when I send “ON” from the firebase (the Arduino has got the message) the LED_Function()
is stop working and execute the (DeviceStatus.stringData() == "ON")
conditions (continuous condition till I send OFF). When I send “OFF”, LED_Function() is stop working and the code execute (DeviceStatus.stringData() == "OFF") conditions. If I didn’t send anything, the LED_Function()
is working continuously. Please, I appreciate if you could help me in this matter. :frowning:

void loop() {
  LED_Function();

  Firebase.getString(DeviceStatus, "LED_STATUS/LED");
  Serial.print("The signal is:  ");
  Serial.println(DeviceStatus.stringData());

  if (DeviceStatus.stringData() == "ON") {
    Firebase.setFloat(DeviceStatus, "LED_STATUS/LED", 55);
    digitalWrite(led2, HIGH);
    digitalWrite(led, LOW);
    Serial.println("Led Turned ON");

  }
  if (DeviceStatus.stringData() == "OFF") {
    Firebase.setFloat(DeviceStatus, "LED_STATUS/LED", 55);
    digitalWrite(led2, LOW);
    digitalWrite(led, HIGH);
    Serial.println("Led Turned OFF");

  }
}


void LED_Function() {

  int Detect = analogRead(SensorRec);

  if (isnan(Detect)) {
    Serial.println(F("Failed to read from Laser sensor!"));
    return;
  }


  if (Detect < 300) {
    Serial.println("Laser Cut. Data is disptched to the Firebase!");

    digitalWrite(led5, LOW);
    digitalWrite(led7, LOW);
    float  val = 1.0;
    Serial.println(val);
  }

  else {
    float   val = 2.0;
    digitalWrite(led, HIGH);
    digitalWrite(led7, HIGH);
    Serial.println(val);

  }
}

the loop() loops. if you don't want to call LED_Function() at every loop, then you need to have some sort of Boolean variable (say callLedFunction) that remembers what to do or not and do if (callLedFunction) LED_Function(). the callLedFunction variable could be a global variable that you set to true or false when you get the right DeviceStatus.stringData() (I assume)

Thanks J-M-L for your reply,
However, the firebase sends only one time ON/OFF message. how to set the boolean to false or true?

I'm a bit unclear on what you do, but what I had in mind was something like this

void loop() {
  static bool callLedFunction = false; // or true, whatever initial value you want to have

  Firebase.getString(DeviceStatus, "LED_STATUS/LED");
  Serial.print(F("The signal is:  "));
  Serial.println(DeviceStatus.stringData());

  if (DeviceStatus.stringData() == "ON") {
    Firebase.setFloat(DeviceStatus, "LED_STATUS/LED", 55);
    digitalWrite(led2, HIGH);
    digitalWrite(led, LOW);
    Serial.println("Led Turned ON");
    callLedFunction = true; // or false whatever makes sense
  } else if (DeviceStatus.stringData() == "OFF") {
    Firebase.setFloat(DeviceStatus, "LED_STATUS/LED", 55);
    digitalWrite(led2, LOW);
    digitalWrite(led, HIGH);
    Serial.println("Led Turned OFF");
    callLedFunction = false; // or true whatever makes sense 
 }
  
  if (callLedFunction) LED_Function();
}

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