Servo and pot programming error

I have go a trouble with this codes :stuck_out_tongue:

error: expected ‘)’ before numeric constant

sketch_jul03a:28: error: too few arguments to function ‘long int map(long int, long int, long int, long int, long int)’

#include <Servo.h>

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

int potpin1 = 0;
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int val1;
int val2;
int val3;
int val4;

void setup()
{
myservo1.attach(6);
myservo2.attach(9);
myservo3.attach(10);
myservo4.attach(11);
Serial.begin(9600);
}

void loop() {
val1 = analogRead(potpin1);
val1 = map(val1, 0, 512 0, 180);
myservo1.write(val1);
Serial.println(val1);

val2 = analogRead(potpin2);
val2 = map(val2, 0, 512 0, 180);
myservo1.write(val2);

val3 = analogRead(potpin3);
val3 = map(val3, 0, 512 0, 180);
myservo1.write(val3);

val4 = analogRead(potpin4);
val4 = map(val4, 0, 512 0, 180);
myservo1.write(val4);

delay(5);
}

help me doing this :))))))))

 val4 = map(val4, 0, 512 0, 180);

Missing a comma.

And missing code-tags and missing indentation...

....arrays too

As AWOL points out, your program is begging for arrays.
Any time you put sequential numbers on the end of variable names, you can be pretty sure you should be using an array.

Here is my (untested) code that should do mostly the same as you are doing.

#include <Servo.h>
const int NumServos = 4;
Servo ServoArray[NumServos];
int ServoPins[NumServos] = {6, 9, 10, 11};
int PotPins[NumServos] = {0, 1, 2, 3};
int val;

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < NumServos; i++)
    pinMode(PotPins[i], INPUT_PULLUP);
  for (int i = 0; i < NumServos; i++)
    ServoArray[i].attach(ServoPins[i]);
}

void loop() {
  // put your main code here, to run repeatedly:
  for (int i = 0; i < NumServos; i++)
  {
    val = analogRead(PotPins[i]);
    val = map(val, 0, 512, 0, 180);
    ServoArray[i].write(val);
  }
}
  for (int i = 0; i < NumServos; i++)
    pinMode(PotPins[i], INPUT_PULLUP);

You do NOT need to set the mode of the digital pin that shares space with the analog pin, when using the pin as an analog pin. Turning on the internal pullup resistors can actually have negative affects on the analog pin.

Good catch. I used an example from another post that was using buttons. There the pullups are desirable.
Here is the post without the pullups.

#include <Servo.h>
const int NumServos = 4;
Servo ServoArray[NumServos];
int ServoPins[NumServos] = {6, 9, 10, 11};
int PotPins[NumServos] = {0, 1, 2, 3};
int val;

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < NumServos; i++)
    pinMode(PotPins[i], INPUT);
  for (int i = 0; i < NumServos; i++)
    ServoArray[i].attach(ServoPins[i]);
}

void loop() {
  // put your main code here, to run repeatedly:
  for (int i = 0; i < NumServos; i++)
  {
    val = analogRead(PotPins[i]);
    val = map(val, 0, 512, 0, 180);
    ServoArray[i].write(val);
  }
}

There is NO NEED to set the pinMode of a pin used as an analogue input.

@vinceherman, you’re still messing with the digital state while you use it as analog :wink: Even worst, because you define them as (low) numbers you don’t even mess with the same pin :wink:

pinMode(0, INPUT) //messes with D0
analogRead(0) //reads A0

So better to use the Ax notation :wink:

#include <Servo.h>
const byte NumServos = 4; //byte will do
Servo servos[NumServos]; //why would we explicitly call an array and ServoPins not?
const byte ServoPins[NumServos] = {6, 9, 10, 11}; //const byte will do ;)
const byte PotPins[NumServos] = {A0, A1, A2, A3}; //To not make mistakes when using them, use Ax notation
//int val; no need for a global

void setup() {
  Serial.begin(9600);
  //no need to mess with the digital state
  /*for (int i = 0; i < NumServos; i++)
    pinMode(PotPins[i], INPUT);*/
  for (byte i = 0; i < NumServos; i++){ //byte will do
    servos[i].attach(ServoPins[i]);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  for (byte i = 0; i < NumServos; i++){
    unsigned int val = analogRead(PotPins[i]);
    val = map(val, 0, 512, 0, 180);
    servos[i].write(val);
  }
}

Did I mention that it was untested code? :wink:
Thanks for helping me get it straight, even if it takes a 2x4 to do so some times.

agakarlar, Look at septillion's code. The use of arrays does greatly reduce repetitive code.