I need to perform a function that sets the color depending on the path, that is, to have a single function instead of calling bring up void red, void green, void blue. I understand that I must pass the route and the value of each color as parameters (String route, int color) but I don't know how to do it.
#include<WiFi.h>
#include<FirebaseESP32.h>
#include <Adafruit_NeoPixel.h>
#define PIN 21
#define NUM_LEDS 20
#define FIREBASE_HOST "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
#define FIREBASE_AUTH "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
const char* ssid = "XXXXXXXXXXXX";
const char* password = "XXXXXXXXXXXX";
FirebaseData firebaseData;
Adafruit_NeoPixel leds(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
// Current color values
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(".......");
Serial.println("WiFi Connected....IP Address:");
Serial.println(WiFi.localIP());
leds.begin();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
Firebase.reconnectWiFi(true);
}
void setLedColor() {
for (int i=0; i < NUM_LEDS; i++)
leds.setPixelColor(i, leds.Color(redValue, greenValue, blueValue));
leds.show();
}
void red(String route) {
if (Firebase.getInt(firebaseData,route)) {
if (firebaseData.dataType() == "int") {
int val = firebaseData.intData();
if (val != redValue) {
redValue = val;
setLedColor();
}
}
}
}
void green(String route){
if (Firebase.getInt(firebaseData,route)) {
if (firebaseData.dataType() == "int") {
int val = firebaseData.intData();
if (val != greenValue) {
greenValue = val;
setLedColor();
}
}
}
}
void blue(String route){
if (Firebase.getInt(firebaseData,route)) {
if (firebaseData.dataType() == "int") {
int val = firebaseData.intData();
if (val != blueValue) {
blueValue = val;
setLedColor();
}
}
}
}
void init(){
red("/lampara4/red");
green("/lampara4/green");
blue("/lampara4/blue");
}
void loop() {
init();
}
If I understand your question correctly you can do it by passing the color variable you want to set as a call by reference parameter, like so:
void setRGB(String route, int &colorValue) {
if (Firebase.getInt(firebaseData,route)) {
if (firebaseData.dataType() == "int") {
int val = firebaseData.intData();
if (val != colorValue) {
colorValue = val;
setLedColor();
}
}
}
}
Then you can call setRGB to set redValue etc. like this:
setRGB("/lampara4/red", redValue);
setRGB("/lampara4/green", greenValue);
setRGB("/lampara4/blue", blueValue);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.