a counter in firebase

Hello everyone, I have a question, I need to create a slot in firebase called " status" and I need to do an if else statement in Arduino that changes the value based on the measurement, this is my code

const int  trigPin = 5;
const int echoPin = 4;

long duration;
int distance;
char Str4[] = "Yes";
char Str5[] = "No";





#include "FirebaseESP8266.h"  // Install Firebase ESP8266 library

#include <ESP8266WiFi.h>

#define FIREBASE_HOST "toimon-fade7.firebaseio.com"
#define FIREBASE_AUTH "XYGE87yYdvfX4aHviADlrco37lceiSXIWBO4BIMX"
#define WIFI_SSID "WaitForTheQ"
#define WIFI_PASSWORD "oneincontrol"

FirebaseData firebaseData;
FirebaseJson json;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.reconnectWiFi(true);
  
  // 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());
  
 
}
 
void loop() {
 
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); 
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);  
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  
  Serial.print("Distance:");
  Serial.print(distance);
  Serial.print("CM");
  Serial.println();
  
if (distance <= 1){

}
else if (distance > 1){

}
  

  Firebase.setString(firebaseData,"/DB_NAME/FIELD_NAME",String(distance));
  
  delay(1000);
}

I am not sure how the statement within the if/ else if should!
Could anyone please help
This is what I thought of doing

if (distance <= 1){
  Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str5[]));
}
else if (distance > 1){
  Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str4[]));
}

But I get this kind of error

Arduino: 1.8.13 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200"





















C:\Users\AL-Faisal\Desktop\sketch_dec20a\sketch_dec20a.ino: In function 'void loop()':

sketch_dec20a:66:59: error: expected primary-expression before '(' token

   Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str5[]));

                                                           ^

sketch_dec20a:66:65: error: expected primary-expression before ']' token

   Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str5[]));

                                                                 ^

sketch_dec20a:68:20: error: expected ';' before '{' token

 else if (distance > 1){

                    ^

Multiple libraries were found for "SD.h"

 Used: C:\Users\AL-Faisal\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\SD

 Not used: C:\Users\AL-Faisal\Desktop\arduino-1.8.13\libraries\SD

exit status 1

expected primary-expression before '(' token

C:\Users\AL-Faisal\Desktop\sketch_dec20a\sketch_dec20a.ino: In function 'void loop()':

sketch_dec20a:66:59: error: expected primary-expression before '(' token

   Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str5[]));

                                                           ^

sketch_dec20a:66:65: error: expected primary-expression before ']' token

   Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str5[]));

                                                                 ^

sketch_dec20a:69:59: error: expected primary-expression before '(' token

   Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str4[]));

                                                           ^

sketch_dec20a:69:65: error: expected primary-expression before ']' token

   Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str4[]));

                                                                 ^

Multiple libraries were found for "SD.h"

 Used: C:\Users\AL-Faisal\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\SD

 Not used: C:\Users\AL-Faisal\Desktop\arduino-1.8.13\libraries\SD

exit status 1

expected primary-expression before '(' token

C:\Users\AL-Faisal\Desktop\sketch_dec20a\sketch_dec20a.ino: In function 'void loop()':

sketch_dec20a:66:59: error: expected primary-expression before '(' token

   Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str5[]));

                                                           ^

sketch_dec20a:66:65: error: expected primary-expression before ']' token

   Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str5[]));

                                                                 ^

sketch_dec20a:69:59: error: expected primary-expression before '(' token

   Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str4[]));

                                                           ^

sketch_dec20a:69:65: error: expected primary-expression before ']' token

   Firebase.setString(firebaseData,"/DB_NAME/Status",String(Str4[]));

                                                                 ^

Multiple libraries were found for "SD.h"

 Used: C:\Users\AL-Faisal\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.4\libraries\SD

 Not used: C:\Users\AL-Faisal\Desktop\arduino-1.8.13\libraries\SD

exit status 1

expected primary-expression before '(' token



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

First attempt to fix it would be to get rid of the [].

else if (distance > 1){

Can be

else {

sterretje:
First attempt to fix it would be to get rid of the [].

I did, would u suggest how to create a certain string that could be added to the firebase using if else

String status[2] = { "no", "yes" };

...

Firebase.setString(firebaseData,"/DB_NAME/Status", status[distance > 1]);

PaulRB:

String status[2] = { "no", "yes" };

...

Firebase.setString(firebaseData,"/DB_NAME/Status", status[distance > 1]);

PaulRB:

String status[2] = { "no", "yes" };

...

Firebase.setString(firebaseData,"/DB_NAME/Status", status[distance > 1]);

Thank you very much, it worked perfectly, I was meaning to ask you do u know how to access a certain child within the firebase, not creating a new child?
And also could you suggest me a way to create a counter for every time the status change ?

Hello everyone, thank you for being here you are the best
I have this code and in it there is a status option that changes to YES/NO, I want to create a counter inside firebase for every time status changes, until the user just deletes it
This is my code

const int  trigPin = 5;
const int echoPin = 4;

long duration;
int distance;
String status[2] = { "No", "Yes" };



#include "FirebaseESP8266.h"  // Install Firebase ESP8266 library

#include <ESP8266WiFi.h>

#define FIREBASE_HOST "toimon-fade7.firebaseio.com"
#define FIREBASE_AUTH "XYGE87yYdvfX4aHviADlrco37lceiSXIWBO4BIMX"
#define WIFI_SSID "WaitForTheQ"
#define WIFI_PASSWORD "oneincontrol"

FirebaseData firebaseData;
FirebaseJson json;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.reconnectWiFi(true);
  
  // 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());
  
 
}
 
void loop() {
 
  digitalWrite(trigPin, LOW);  
  delayMicroseconds(2); 
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);  
  digitalWrite(trigPin, LOW);
  
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;
  
  Serial.print("Distance:");
  Serial.print(distance);
  Serial.print("CM");
  Serial.println();

  Firebase.setString(firebaseData,"/DB_NAME/FIELD_NAME",String(distance));
  Firebase.setString(firebaseData,"/DB_NAME/Status", status[distance > 30]);


  
  delay(1000);
}

Sorry, I'm not familliar with Firebase or its children.

The counter you want: would that be stored in Firebase or only in a variable in your code?

Topics with a common theme merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

PaulRB:
Sorry, I'm not familliar with Firebase or its children.

The counter you want: would that be stored in Firebase or only in a variable in your code?

Yes I want it to store it in firebase same as the status

if (distance > 30 && prevDistance <= 30) {
  Firebase.setString(firebaseData,"/DB_NAME/Count", String(Firebase.getString(firebaseData,"/DB_NAME/Count").toInt() + 1));
}

Maybe? I'm guessing.

PaulRB:

if (distance > 30 && prevDistance <= 30) {

Firebase.setString(firebaseData,"/DB_NAME/Count", String(Firebase.getString(firebaseData,"/DB_NAME/Count").toInt() + 1));
}



Maybe? I'm guessing.

But is there a certain rule I should give to prevDistance ? because just initializing it doesn't work

Yes, you are correct, just initialising prevDistance will not work. You figured that out...

PaulRB:
Yes, you are correct, just initialising prevDistance will not work. You figured that out...

Sorry I am new to all this, excuse my foolishness'

I wasn't accusing you of being foolish. You were intelligent enough to figure out that something more must be done with prevDistance. Take the next step.