I've been having trouble with the code I've written. My problem is that I am using multiple pots to control different servos, and when I turn one, it controls all of the other pot values. All of the pots are connected to their own analog pins, and all of the servos are connected to their own digital pins. I've tried rewiring some of the servos to pots, but I am having no luck (by the way, I am still fairly new to Arduino, so try to take it easy). I'll attach my code.
R.A.T._Code.ino (1.94 KB)
Your sketch is small enough to put in a code block...
#include <Servo.h> //Including The Library for Servo functions
Servo servo1; //Setting First Servo variable as s1
Servo servo2; //Setting Second Servo variable as s2
Servo servo3; //Setting Third Servo variable as s3
int pot1 = A0; // Setting potentiometer one as "pot1" and attaching it to "analog 0"
int pot2 = A1; // Setting potentiometer two as "pot2" and attaching it to "analog 1"
int pot3 = A2; // Setting potentiometer three as "pot3" and attaching it to "analog 2"
void setup()
{
servo1.attach(8); //Attaching "s1" to pin 8
servo2.attach(9); //Attaching "s2" to pin 9
servo3.attach(10); //Attaching "s3" to pin 10
pinMode(pot1, INPUT); // Setting "pot1" as an input
pinMode(pot2, INPUT); // Setting "pot2" as an input
pinMode(pot3, INPUT); // Setting "pot3" as an input
}
void loop()
{
int pot1val;
int pot2val; // Make variables "pot1-3val"
int pot3val;
int x = 5.68+1/300;
pot1val = analogRead(pot1);
pot2val = analogRead(pot2);//Make pot1-3val read analog pot1-3
pot2val = analogRead(pot3);
pot1val = analogRead(pot1);
pot2val = analogRead(pot2);//Make pot1-3val read analog pot1-3
pot2val = analogRead(pot3);
if (pot1val >= 0){ //If "pot1val" is greater than or equal to 0
servo1.write(pot1val/x); //Set the angle of servo 1 (s1) to the value of pot1val divided by 5.68+1/300
}
else {
servo1.write(pot1val/x);//Set the angle of servo 1 (s1) to the value of pot1val divided by 5.68+1/300
}
if (pot2val >= 0){ //If "pot2val" is greater than or equal to 0
servo2.write(pot2val/x);//Set the angle of servo 2 (s2) to the value of pot2val divided by 5.68+1/300
}
else {
servo2.write(pot2val/x);//Set the angle of servo 2 (s2) to the value of pot2val divided by 5.68+1/300
}
if (pot3val >= 0){ //If "pot3val" is greater than or equal to 0
servo3.write(pot3val/x);//Set the angle of servo 3 (s3) to the value of pot3val divided by 5.68+1/300
}
else {
servo3.write(pot3val/x);//Set the angle of servo 3 (s3) to the value of pot3val divided by 5.68+1/300
}
}
I'm happy to do that bit for you, so how about you now post a wiring diagram/schematic and tell us which board you're using?
Have a read here How to use the forum. A good question will get good quality answers (though probably not from me
).
Thanks! Here is the schematic. It may be a little blurry. If it is, I can attach a higher quality version of it.
Hi
A few comments regarding the wiring schematic
- You don't need a a resistor in series with the pot if you don't need it as a voltage divider. In this setup you can never reach 0 volt to input A0-A2.
- One of the servos is connected to pin 8, but that is not a PWM output.
- You should use a 100 microF decoupling capacitor between + and - near the servos. You could have one across + and - near the pots also. It evens out any voltage changes caused by the components.
Yours /GG
This looks a little odd...
pot1val = analogRead(pot1);
pot2val = analogRead(pot2);
pot2val = analogRead(pot3); // <--- you mean pot3val
pot1val = analogRead(pot1);
pot2val = analogRead(pot2);
pot2val = analogRead(pot3); // <--- you mean pot3val
Make it like this:
// Double-reading an analogue input is a good idea when switching channels...
pot1val = analogRead(pot1);
pot1val = analogRead(pot1);
pot2val = analogRead(pot2);
pot2val = analogRead(pot2);
pot3val = analogRead(pot3);
pot3val = analogRead(pot3);
Not much point in doing fancy maths with decimal places when you store the result in an integer variable. So how about:
int x = 5;