Servos and Potentiometers, help with code please

Hi, I have 6 servos and 6 potentiometers and an Arduino Duemilanove. I wish to create a system where each potentiometer will control one given servo. I wrote the code for this and wired it up however when up and running I found that one potentiometer was controlling all 6 servos and the others were doing nothing. Can anyone help?
Here is the non-working code I wrote:

#include <Servo.h> 
 
Servo HipL;
Servo HipR;
Servo KneeL;
Servo KneeR;
Servo AnkleL;
Servo AnkleR;
 
int HipLpotpin = 0;
int HipRpotpin = 1;
int KneeLpotpin = 2;
int KneeRpotpin = 3;
int AnkleLpotpin = 4;
int AnkleRpotpin = 5;
int val;

void setup() 
{ 
  HipL.attach(0);
  HipR.attach(1);
  KneeL.attach(2);
  KneeR.attach(3);
  AnkleL.attach(4);
  AnkleR.attach(5);
} 
 
void loop() 
{ 
  val = analogRead(HipLpotpin); 
  val = map(val, 0, 1023, 0, 179);
  HipL.write(val);
  val = analogRead(HipRpotpin); 
  val = map(val, 0, 1023, 0, 179);
  HipR.write(val);
  val = analogRead(KneeLpotpin); 
  val = map(val, 0, 1023, 0, 179);
  KneeL.write(val);
  val = analogRead(KneeRpotpin); 
  val = map(val, 0, 1023, 0, 179);
  KneeR.write(val);
  val = analogRead(AnkleLpotpin); 
  val = map(val, 0, 1023, 0, 179);
  AnkleL.write(val);
  val = analogRead(AnkleRpotpin); 
  val = map(val, 0, 1023, 0, 179);
  AnkleR.write(val);
  delay(15);
}

Any help would be very much appreciated! Thanks in advance,

Mat

HAHAHAHA, sorry guys!

just checked all my wires and found some pins not pushed into my breadboard properly, i’m a dumbass.

all works fine and the code is good!
:slight_smile:

Have you noticed how much of your code looks similar?

  val = analogRead(HipLpotpin);
  val = map(val, 0, 1023, 0, 179);
  HipL.write(val);

looks virtually identical to:

  val = analogRead(HipRpotpin);
  val = map(val, 0, 1023, 0, 179);
  HipR.write(val);

This usually means there's a shorter and simpler way of constructing your code, using arrays and loops.

Shorter code gives the bugs fewer dark corners to hide.

That sounds interesting, however I am new to Arduino, and programming so have no idea how to do this. Can you give me any ideas?

(uncompiled, untested)

const int nJoints = 6;
Servo jointServo [nJoints];
const int potPin [nJoints] = {0, 1, 2, 3, 4, 5}; //modify to taste
const int jointPin [nJoints] = {0, 1, 2, 3, 4, 5};  //modify to taste

void setup()
{
  for (int i = 0; i < nJoints; i++) {
    jointServo.attach (jointPin [i]);
  }  
}

void loop()
{
  for (int i = 0; i < nJoints; i++) {
    jointServo.write(map(analogRead(jointPot [i], 0, 1023, 0, 179));
  }
  delay (15);
}

{{cough}}

 jointServo.write(map(analogRead(potPin [i], 0, 1023, 0, 179));

{{cough}}

(uncompiled, untested)

:wink: