1

I'm trying to control 6 servos while writing a lean code. I think the function call isn't correctly yet and I think that the variables (pos_n, open_n, servo_1) in the for loop do not get inserted, but instead the string just gets inserted.

I think I have come a long way to not make this a bulky code.

What changes makes this function working, so that the call openValve(1) and openValve(2); work for both servos?

#include <Servo.h>
const int buttonPin = 2;
int buttonState = 0;

Servo servo_1;
const int pos_1 = 0;
const int open_1 = 105;
const int clos_1 = 10;
//Servo servo_2;
//const int pos_2 = 0;
//const int open_2 = 104;
//const int clos_2 = 9;

String servo_n, pos, pos_n, servo, openServo, open_n;

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  servo_1.attach(11);
}

void loop() {
  openValve(1); // call the function
  while (true);
}

void openValve(n) {
  int n = 3;
  pos = "pos_";
  pos_n = (pos + n);
  Serial.println(pos_n);

  servo = "servo_";
  servo_n = servo + n;
  Serial.println(servo_n);

  openServo = "open_";
  open_n = openServo + n;
  Serial.println(open_n);

  // DONT USE STRING BUT THE VARIABLE CONTENTS ??
  for (pos_n = pos_n; pos_n <= open_n; pos_n += 1) {
    servo_n.write(open_n);
    delay(10);
  }
}
4
  • If you are trying to write "lean" code, then using the 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.
    – VE7JRO
    Jan 8, 2020 at 0:18
  • variables in the loop do not get inserted into what?
    – jsotola
    Jan 8, 2020 at 3:59
  • @VE7JRO could you give me a hint what I then should use instead of the String object?
    – Ludo
    Jan 8, 2020 at 7:29
  • 1
    @Ludo: Plain C strings, i.e. arrays of char. Jan 8, 2020 at 8:27

1 Answer 1

1

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.

2
  • I am a bit stuck in thinking like PHP code with inserting 'variables' :p Thanks for your elaborate answer!
    – Ludo
    Jan 8, 2020 at 20:23
  • @Ludo If you think, that my answer is correct, you can mark is as correct. Then others can see, that this actually solved your problem.
    – chrisl
    Jan 9, 2020 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.