How to use "references" to use lower in the sketch

Hello,

My personal programming skills are like zero, but have been making alot of arduino stuff with the IDE and having fun, (I just copy/paste some frankenstein code together without fully understanding it lol :slight_smile: )

I have a question for the more experienced people here (and is a stupid one, but I am not sure how to do it)

I am currently making multiple arduino's that use DS18B20 temp send it with MQTT to Home assistant and show up , and thats all working fine.

But I want to finetune the process of making the same one's (since my brother keeps breaking them :slight_smile: ) but with only changes in the stuff like sensor name, and arduino MQTT client name. (like for instance I have arduino RED, and it has the sensor's Blue, Yellow, ...)

can I define in some way at the start of the sketch for instance "Sensor1" = Blue
and than in the bottom of the sketch just use "Sensor1" but if I than make a new arduino , I just have to change the Blue at the start to for instance RED, and automaticaly everywhere it uses the "Sensor1" it now say's Blue instead of Red (like for the Serial print, for the MQTT topic, ....

That way I only have to change it one time, and not like 15 times in the sketch,

I know there is the "float" and "int" and stuff, but I cant find information anywhere to what I should use for simple text ??? (those int and stuff seems to be for numbers and calculations)

So my stupid question is, what command should I use to define some text at the start so I only have to change that next time , without messing any calculations or something up.

Any help, tips is appreciated,
and yes, I have been reading in the programming guides on the arduino site, but I think my question is so simple/stupid straitforward , that its not in there ??

thank you

There are several ways to do this such as

  1. Use a variable declared as an array of chars

Example

char* name = "RED";

void setup()
{
    Serial.begin(115200);
    Serial.println(name);
}

void loop()
{
}

or
2. #define the name

#define name "RED"

void setup()
{
    Serial.begin(115200);
    Serial.println(name);
}

void loop()
{
}

You can

# define RED 0xff0000

and use RED like

   strip.setPixelColor(43, RED);

You can also

const byte nPatterns = 700:

That's two ways to make symbols stand for what they stand for.

a7

Thanks alot, that define methode is what im gona use I think :slight_smile:

Also thank you alto777 , the colors are not realy the colors but the name (its how I name the sensors who have that color),

but its something else I learned :slight_smile: thank you

I wouldn't. Nowadays, it's considered a best practice to only use preprocessor macros when absolutely necessary. Better to use:

const char name[] = "RED";

oh ok,

do you have an example for me how to use that ?
Do I than put name[] in the code where ever in this example needs the text "RED" to come ? or do I have to put something in the brackets ? (im sorry im just learning)

do you also have a quick explenation why it is better this way ? (pro's and con's ?)

Thanks alot,

   Serial.println(name);

Pre-processor macros are simple text substitutions with no error checking. Defining a variable (in this case a const variable) brings the full power of the compiler's datatype system and error checking to bear. This can help you spot errors in your code.

Oh ok :slight_smile:

Thanks for all the info,

Hans

I have a question: what's the difference between
const char name[] = "RED";
and
char* name = "RED";
?

Very little. In theory, the latter could have caused creation of an extra pointer variable. But, it's just as likely the compiler optimized it away. Also, for this application, the latter should probably have been written as:

const char * const name = "RED";

I see, thank you.

Sorry to bother you guys again,
I was only now able to try this out (so much to do , so little time :slight_smile: )

And im probably beeing stupid, but can not get it to work,

can you put this in my own example how I should do this, so I am trying to get this done (small example)

I am putting this at the start of my sketch

const char * const maintopic = "Wit";
const char * sensor01 = "Wit";

And later in the sketch in the loop, I would like to get whatever I put above (so in this case I want it to be Wit/Wit)

client.publish("(maintopic)/(sensor01)", String(temp1).c_str(),true);

however, my MQTT server just receives (maintopic)/(sensor01) instead of Wit/Wit

I tried all different versions from the post here, but nothing works ??

Can you guys format it the correct way for me and tell me where I am beeing an idiote :slight_smile:

thank you,

Hans

Why are you choosing that syntax rather than the more straightforward and easier to understand array syntax?

  const char maintopic[] = "Wit";
  const char sensor01[] = "Wit";

And, why are you defining two constants with the same value?

I first tried it like

const char maintopic[] = "Wit";
  const char sensor01[] = "Wit";

but that also dit not work :frowning:

In this case they are both the same (its for arduino topics) its to colour my temperature sensors also per arduino (first colour is arduino colour, second color is the particular sensor colour, I have 8 sensors per arduino)

could the problem be that I am referencing them in my loop ??
Am I using them correctly here in my loop ?

client.publish("(maintopic)/(sensor01)", String(temp1).c_str(),true);

because it just gets send as "maintopic/sensor01" , and it should be send as "wit/wit"

You can not just put a variable name inside a string and hope it magically gets replaced with the contents of the variable. C/C++ does not work that way. You can use strcpy()/strcat() to build up your string, sprintf() to build your string or the String class which knows how to concatenate things to build up your string.

const char maintopic[] = "Wit";
const char sensor01[] = "Wit";
String topic = String("(") + String(maintopic) + ")/(" + String(sensor01) + String(")");
client.publish(topic.c_str(), String(temp1).c_str(),true);

i think using sprintf may be easier to see.
but another approach is to simply define topic/sensor strings (e.g. topciSensor) instead of doing so at runtime.

const char topic   [] = "wit";
const char sensor1 [] = "wit";

const char topicSensor1 [] = "wit/wit";

char     s [90];

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    sprintf (s, "%s/%s", topic, sensor1);
    Serial.println (s);

    Serial.println (topicSensor1);
}

void loop () { }

but i thought it would have made more sense to have unique topic names for each Arduino and common sensor names. You would only edit the topic, the name of the Arduino.

and another approach is to actually write some code to save/retieve the Arduino name in EEPROM so that the code is actually indentical on multiple Arduinos

Hmm, im even more confused now :frowning: I just have zero programming skills (am bad with languages as is, so programming is just another language im bad with :frowning: )

I am just trying to save some time when I have to make some extra temperature sensor arduino's that I dont always have to change it everywhere in my code what the name for the new arduino is (like you say, colours I could keep the same) 10 times

I was hoping there was some way in the IDE that I could put at the start for instance

"xxx" = Blue

and than I could put in my code later down "xxx" and at compile time the code would just be compiled as Blue and not xxx :frowning:

But I gues there is no easy / reliable way of doing this ?
Because those methode's you are posting just means alot more programming and in my case that can all go wrong very easy :slight_smile:

I also have other stuff (with thermocouples instead of DS18B20 sensors) and I am trying to get some basic code I can than edit out as easy as possible for other uses, so defenatly dont want to make it more complicated :frowning:

Thanks alot for all the trying you guys are doing, but I think im to stupid to make it easyer , so gues ill keep doing it the hard way :frowning:

you can always pre-define the string at compile time instead of creating them at run time.

const char *Sensor1 = "topic/sensor1";
const char *Sensor2 = "topic/sensor2";
const char *Sensor3 = "topic/sensor3";

or an array of strings

const char *Sensor [] = { "topic/sensor1", "topic/sensor2", "topic/sensor3" };

For some reason that is not working,

And it kinda works at setup and stuff, because my wifi settings is done with the const char * and it works,

do I have to put that also in the loop itself ? because everything in the loop seems to be ignored ??