I want to create a generic code, that depending on the parameters that I pass with #define , I declare some variables or others, I think it is something like this, but the answer is that the variables are not defined, Main code:
#include "ESP_VarDef.h"
#define VAR_V00 true
#define VAR_X00 true
#define VAR_X01 true
void setup(){
Serial.begin(115200);
}
void loop(){
Serial.println("----------");
Serial.println(VAR_V00A);
Serial.println(VAR_X00A);
Serial.println(VAR_X01A);
Serial.println("----------");
}
secondary code:
#if defined(VAR_V00)
int VAR_V00A = 0 ;//device 0: Minute day
bool VAR_V01A = 0 ;//device 0: Status led
#endif
#if defined(VAR_X00)
int VAR_X00A = 0 ;//device 1: Time mode
int VAR_X00B = 0 ;//device 1: Time start
int VAR_X00C = 0 ;//device 1: Time end
int VAR_X00D = 0 ;//device 1: Time mode
int VAR_X00E = 0 ;//device 1: Time start
int VAR_X00F = 0 ;//device 1: Time end
#endif
#if defined(VAR_X01)
bool VAR_X01A = 0 ;//device 2: RGB state
String VAR_X01B = "000000" ;//device 2: RGB color
#endif
how could i do it?
J-M-L
February 12, 2023, 11:48am
2
How is the "secondary code" injected in the .ino ?
the .ino cannot print the variables if they do not exist, so your print() should also have an #ifdef
Yes, I am aware, when I copied it, a line remained...
It's already corrected, sry
J-M-L
February 12, 2023, 11:56am
5
you could try something like this
Run IoT and embedded projects in your browser: ESP32, Arduino, Pi Pico, and more. No installation required!
with a proper .h and .cpp
and have functions that are abstracting your configuration in the .cpp
#if defined(VAR_V00)
Because you have #defined VAR_V00 as true the compiler will see
if defined(true)
which will return true because the value of true is #defined as part of the behind the scenes processing in Arduino
Even if you #defined VAR_V00 as false then
if defined(false)
will also return true for the same reason
gcjr
February 12, 2023, 12:03pm
7
can you describe your application where you have a varying # of variables as well as varying logic that operations on those variables?
or do you just have sub-sets of variables/logic?
When I put this code in the arduino ide, it tells me that Serial is not defined, I don't understand it...
J-M-L
February 12, 2023, 12:33pm
9
Post thé 3 files as you created them in the IDE and the file names
Which arduino are you using?
Arduino IDE 1.8.19
demo.ino
#include "demo.h"
void setup() {
Serial.begin(115200);
printAllVariables();
}
void loop() {}
demo.h
#ifndef _SECONDARY_H_
#define _SECONDARY_H_
#include <Arduino.h>
#define VAR_V00 true
#define VAR_X00 true
#define VAR_X01 true
void printAllVariables();
#endif
demo.cpp
#if VAR_V00
int VAR_V00A = 0 ;//device 0: Minute day
bool VAR_V01A = 0 ;//device 0: Status led
#endif
#if VAR_X00
int VAR_X00A = 0 ;//device 1: Time mode
int VAR_X00B = 0 ;//device 1: Time start
int VAR_X00C = 0 ;//device 1: Time end
int VAR_X00D = 0 ;//device 1: Time mode
int VAR_X00E = 0 ;//device 1: Time start
int VAR_X00F = 0 ;//device 1: Time end
#endif
#if VAR_X01
bool VAR_X01A = 0 ;//device 2: RGB state
String VAR_X01B = "000000" ;//device 2: RGB color
#endif
void printAllVariables() {
Serial.println("----------");
#if VAR_V00
Serial.println(VAR_V00A);
#endif
#if VAR_X00
Serial.println(VAR_X00A);
#endif
#if VAR_X01
Serial.println(VAR_X01A);
#endif
Serial.println("----------");
}
gfvalvo
February 12, 2023, 12:52pm
11
Post the actual and complete error message, not your interpretation of it.
C:\Users\Arixt\Desktop\demo\demo.cpp: In function 'void printAllVariables()':
demo.cpp:21:3: error: 'Serial' was not declared in this scope
21 | Serial.println("----------");
| ^~~~~~
exit status 1
'Serial' was not declared in this scope
J-M-L
February 12, 2023, 1:46pm
14
my secondary.cpp was including
#include "secondary.h"
this is how I indirectly include Arduino.h which ensures the usual Arduino functions and classes are known - such as Serial
I have managed to get my code to compile without errors, but the result in the terminal is this:
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
the files are, "ESP_VarDef.ino":
#include <ESP_VarDef.h>
#define VAR_A01XX true
#define VAR_A02XX true
#define VAR_A03XX true
void setup(){
Serial.begin(115200);
}
void loop(){
ESP_VarPrint();
delay(1000);
}
"ESP_VarDef.h":
#ifndef _ESP_VarDef_H_
#define _ESP_VarDef_H_
#include <Arduino.h>
void ESP_VarPrint();
#endif
"ESP_VarDef.cpp":
#include <ESP_VarDef.h>
#if VAR_A01XX
int VAR_A0100 = 0 ;//device 1: Minute day
bool VAR_A0101 = 0 ;//device 1: Status led
#endif
#if VAR_A02XX
int VAR_A0200 = 0 ;//device 1: Time mode
int VAR_A0201 = 0 ;//device 1: Time start
int VAR_A0202 = 0 ;//device 1: Time end
int VAR_A0203 = 0 ;//device 1: Time mode
int VAR_A0204 = 0 ;//device 1: Time start
int VAR_A0205 = 0 ;//device 1: Time end
#endif
#if VAR_A03XX
bool VAR_A0300 = 0 ;//device 2: RGB state
String VAR_A0301 = "000000" ;//device 2: RGB color
#endif
void ESP_VarPrint(){
Serial.println("--------------------");
#if VAR_A01XX
Serial.println("VAR_A0100" + String(VAR_A0100));
Serial.println("VAR_A0101" + String(VAR_A0101));
#endif
#if VAR_A02XX
Serial.println("VAR_A0200" + String(VAR_A0200));
Serial.println("VAR_A0201" + String(VAR_A0201));
Serial.println("VAR_A0202" + String(VAR_A0202));
Serial.println("VAR_A0203" + String(VAR_A0203));
Serial.println("VAR_A0204" + String(VAR_A0204));
Serial.println("VAR_A0205" + String(VAR_A0205));
#endif
#if VAR_A03XX
Serial.println("VAR_A0300" + String(VAR_A0300));
Serial.println("VAR_A0301" + String(VAR_A0301));
#endif
}
gfvalvo
February 12, 2023, 3:26pm
16
arixtoteles09:
I have managed to get my code to compile without errors, but the result in the terminal is this:
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
You have these #defines in the .ino file so they won't be seen when ESP_VarDef.cpp is compiled. Put them in ESP_VarDef.h.
#define VAR_A01XX true
#define VAR_A02XX true
#define VAR_A03XX true
but that would force me to edit the .h for each case, couldn't they be redirected in some way? that is to say, give the .ino the value and that the .h use it?
J-M-L
February 12, 2023, 4:46pm
19
You could have a config.h that has the #define and the ESP_VarDef.h. would include that config.h
config.h
#define VAR_A01XX true
#define VAR_A02XX true
#define VAR_A03XX true
ESP_VarDef.h
#ifndef _ESP_VarDef_H_
#define _ESP_VarDef_H_
#include <Arduino.h>
#include "config.h"
void ESP_VarPrint();
#endif
Then you would have to edit that configuration file, it may seem more natural
ok, that's a good idea, but if the Config.h file is in the program folder and the library is in its own folder (...\Documents\Arduino\libraries\ESP_VarDef), how can I reference the file?