I think you are totally misunderstanding, about variables and types and how using multiple servos with 1 function can work, and that this is your real question and not the thing with inserting variables (whatever you mean by that).
You are trying to put together a string and use it as variable name. That does not work. A string is only a variable/instance of type/class String
. It cannot have methods of the Servo
class. Also function parameters each have their own type. You cannot provide a value of a different type to this parameter. Servo.write()
takes an integer value. It cannot use a String value. Thus the code inside the for loop is nonsense and won't compile. I would suggest doing some C++ tutorials from the web, that explain, how variables work in C++.
Instead you have to use multiple variables of the type Servo
, one for each servo motor, that you are using. These variables are instances of the Servo
class, which implements all the servo related functions (like write) Each motor gets it's own pin on the Arduino. You can put these Servo
objects into an array, so that they can be indexed by an integer number. For example you can define the following variable in the global scope:
#define N_SERVOS 6
Servo servos[N_SERVOS];
This creates an array of type Servo
with 6 elements. Each element is one independent servo object. (Note, that I used a define to easily change the number of attached servos when changing the code). You can reach an individual servo object by indexing it:
servos[3].write(pos);
This would write a new position to servo 4 (4 because array indices are starting from 0). Now we need to attach each Servo object to one pin of the Arduino. I would create an extra array, which contains the corresponding pin numbers - also at global scope:
int servo_pin[N_SERVOS] = {6, 7, 8, 9, 10, 11};
Here I used pins 6 to 11, but you can change them, as you like. Then in the setup()
function we loop over our servo elements and corresponding pin numbers and attach the servo objects to the pins:
for(int i=0;i<N_SERVOS;i++){
servo[i].attach(servo_pin[i]);
}
And now you can rewrite your openValve()
function corresponding to that. I don't understand, what you wanted the servo to do in the for loop, so I will leave this to you. First you need to correct the syntax at the function declaration. You didn't specify the type of the parameter n
. It has to look like:
void openValve(int n){
And then you shouldn't declare a new variable with the same name, so delete the line int n=3;
. Then you can delete all the weird string concatiation. Then you need to fix the for loop (as I wrote I will leave that to you, since you didn't explain, what the servo should actually do there). And finally you need to rewrite the servo write line like described above:
servo[n].write(new_pos);
Note, that new_pos
is just a placeholder here, for whatever values you need to write to the servo (as already said, I don't understand, what exactly you want the servo to do).
what I then should use instead of the String object?
The String
class from the Arduino framework can get you into memory trouble (to be exactly memory fragmentation). You can read Majenko's blog post about Arduino Strings for further explanations.
Instead you should use char
arrays aka C-Strings. You create a buffer with a fixed length, that is big enough to hold the biggest chunk of data, that you need to process. Let's say you need at max 30 characters. Every C-String is terminated by a zero ('\0'). This zero also needs space in the buffer. So the actual buffer size is 31.
char string_buffer[31];
You can even directly initialize it with a string value:
char string_buffer[31]="Teststring";
You can use sprintf()
to put a formatted string into the buffer. For example a string with a number:
sprintf(string_buffer, "Number: %d", 50);
will put the string "Number: 50" to the buffer and will terminate it with a zero.
String
object is not going to make it "lean". The second thing I noticed, was that the code doesn't compile. The first error is "error: 'n' was not declared in this scope. I don't think this is the correct syntax: "void openValve(n) {". HINT: The "n" is an issue. Please fix the syntax errors then edit your question to show us some code that does compile.char
.