Only 1 servo and potentiometer of my 4 work

I am trying to wire up 4 servos and have one potentiometer each, but I have been unable to get more than one working properly. If someone could help, that would be great.

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 

Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;

int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;

void setup() 
{
  myservo1.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);
  myservo4.attach(12);
}

void loop()
{  
  
  for (val1 = 0; val1 <= 180; val1 += 1) {
     val1 = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
     val1 = map(val1, 0, 1023, 20, 180);     // scale it to use it with the servo (value between 0 and 180)
     myservo1.write(val1);                  // sets the servo position according to the scaled value
     delay(15);  
  } 
  for (val1 = 180; val1 >= 0; val1 -= 1) {
     val1 = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
     val1 = map(val1, 0, 1023, 20, 180);     // scale it to use it with the servo (value between 0 and 180)
     myservo1.write(val1);                  // sets the servo position according to the scaled value
     delay(15);  
  }
  
  for (val2 = 0; val2 < 180; val2++) {
    val2 = analogRead(potpin2);           
    val2 = map(val2, 0, 1023, 0, 179);
    myservo2.write(val2);
    delay(10);
  } 
  for (val2 = 180; val2 > 0; val2--) {
    val2 = analogRead(potpin2);           
    val2 = map(val2, 0, 1023, 0, 179);
    myservo2.write(val2);
    delay(10);
  }
  
  for (val3 = 0; val3 < 180; val3++) {
    val3 = analogRead(potpin3);           
    val3 = map(val3, 0, 1023, 0, 179);
    myservo3.write(val3);
    delay(10);
  } 
  for (val3 = 180; val3 > 0; val3--) {
    val3 = analogRead(potpin3);           
    val3 = map(val3, 0, 1023, 0, 179);
    myservo3.write(val3);
    delay(10);
  }
  
  for (val4 = 0; val4 < 180; val4++) {
    val4 = analogRead(potpin4);           
    val4 = map(val4, 0, 1023, 0, 179);
    myservo4.write(val4);
    delay(10);
  } 
  for (val4 = 180; val4 > 0; val4--) {
    val4 = analogRead(potpin4);           
    val4 = map(val4, 0, 1023, 0, 179);
    myservo4.write(val4);
    delay(10);
  }
  delay(90);
}

I don’t know where the lines like this came from

for (val1 = 0; val1 <= 180; val1 += 1) {

They should all be removed together with their corresponding }

And if you are having trouble with wiring then you need to make a simple pencil drawing showing all your connections and post a photo of the drawing. See this Simple Image Guide

…R

I think you have to figure out what you want the sketch to do. You seem to be using the for-loops from the "sweep" example AND the analog input from the "knob" example. What were you trying to accomplish?

I am trying to make the servos move with a potentiometer so where the potentiometer is positioned, the servo goes, but i need to do it with all 4 of them i am wanting to use the knob function

I don't think that pin A0 and pin 0 are the same thing. Similarly for A1, A2, and A3, and pins 1, 2, and 3.

When you are tempted to suffix variable names with numbers, it is time to learn about arrays.

Go and look at the Knob example again. Note that it only needs to write to the servo once. You only need to write to each servo once. And Knob doesn't need any for loops at all. You don't need your for loops either.

The code in your loop() should be roughly:

valX = analogRead(potpinX)
valX = map (as required)
myservoX.write(valX)
delay(15)

One set like that for each servo where X = 1 or 2 or 3 or 4. That's all it needs.

Steve

slipstick:
Go and look at the Knob example again. Note that it only needs to write to the servo once. You only need to write to each servo once. And Knob doesn't need any for loops at all. You don't need your for loops either.

The code in your loop() should be roughly:

valX = analogRead(potpinX)
valX = map (as required)
myservoX.write(valX)
delay(15)

One set like that for each servo where X = 1 or 2 or 3 or 4. That's all it needs.

Steve

I did what you said and it worked, but now it is shaking like crazy, which loop were you talking about?

Post your current code. What servos are you using and how exactly are they powered. A circuit diagram would probably help.

If "it" means all of the servos then jittering/shaking is usually a wiring/power problem rather than a coding problem.

Steve

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor

#include <Servo.h> 

Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;

int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;

void setup() 
{
  myservo1.attach(9);
  myservo2.attach(10);
  myservo3.attach(11);
  myservo4.attach(12);
}

void loop()
{  
     val1 = analogRead(potpin1);            
     val1 = map(val1, 0, 1023, 20, 180);
     myservo1.write(val1);    
     val2 = analogRead(potpin2);           
     val2 = map(val2, 0, 1023, 20, 180);
     myservo2.write(val2);              
  delay(10);
}

\

I am using SG90’s and actually now only one is moving again and it is the one shaking. I will try to get a circuit diagram as soon as possible.

Ok after some unwiring and rewiring this morning, they all work now, thanks to everyone for the help.

Glad you got it working.

A comment was made earlier and I just want to reinforce it.

When you are tempted to suffix variable names with numbers, it is time to learn about arrays.

vinceherman:
Glad you got it working.

A comment was made earlier and I just want to reinforce it.

I will keep that in mind. Thanks for the help.