String in function

Hi,

i'm starting with Arduino coding system, I just have one question :

I would like to use string that way :

          toto = "vert_led";
          digitalWrite(toto,LOW);

with that on setup : String toto="";

but it doesn't work :

Conquest6:84: error: cannot convert 'String' to 'uint8_t' for argument '1' to 'void digitalWrite(uint8_t, uint8_t)'

I searched on forums and websites without success, if anyone can help me !

Thanks alot for your help.

digitalWrite() takes a single byte to set the the output high or low. Sending a string does not make sense.

. . . and sending (or even using a) String in the first place makes even less sense.

Don’t use a sting. Instead, use a constant. For example, if the “too low” LED is tied to pin 7 and “too hi” is on pin 8, then you could use:

#define TOOLOW   7
#define TOOHI    8
// ...more code

if (something < target) {        // Is the measured value, something, less than we want...
   digitalWrite(TOOLOW, HIGH);   // Yes
} else {
   digitalWrite(TOOHI, HIGH);    // No
}

I’m assuming you want to turn the LED on. You could also write the code as:

const byte TOOLOW = 7;
const byte  TOOHI = 8;

Using strings isn’t going to work unless you do additional coding to interpret the string.

The problem you are having is that once the compiler is done with your code, all the variable names are gone and they are replaced with memory locations. So once the code is compiled, the words vert_led have absolutely no meaning.

What you want to do is make them constants like suggested above. If that won't work for you then please post all the code you have and describe exactly what you want it to do so someone can help you figure out a way to do it.

oh yeah you are right

I have this first : const int vert_led = 9;

I would like to use function because I have twice same code for : rouge_led and vert_led

Here's my all code : http://pastebin.com/cTxDq4SC

you can see that same code is used twice all program long

Could it be as simple as

digitalWrite(vert_led, LOW);

and skip the whole string thing? Why do you need to go through a String first?

because I have a lot of code which is the same with rouge_led and vert_led, rouge_temps, vert_temps ...I would like to do functions with "rouge" or "vert" as argument

dureiken:
because I have a lot of code which is the same with rouge_led and vert_led, rouge_temps, vert_temps ...I would like to do functions with "rouge" or "vert" as argument

Just pass rouge_led or vert_led (no quotes) as the argument. I'm not seeing any reason to use strings.

argument would just be rouge or vert

because after in the same function I would have rouge_temps, rouge_led

argument would just be rouge or vert

because after in the same function I would have rouge_temps, rouge_led

Carrying on, after you've been told that using names is not possible, because there are no names in the hex file running on the Arduino, is going to get you nowhere.

You need to investigate structs, arrays, etc. and come up with a way of handling data that does NOT involve the name of a variable.