Reputation: 51
I am trying to make a model of the muscle system in the arm for a project with Arduino, but to accomplish this I need bicep and triceps to move in opposite direction.
I am currently experimenting with a potentiometer and trying to make the two servos move in opposite directions, but somehow the code doesn't seem to work as I would expect since they keep moving in the same direction.
My power supply is my laptop, I haven't used a battery pack yet. As for the specific issue, the servos aren't responding to the potentiometer and they just jitter
#include <Servo.h>
Servo Bicep;
Servo Tricep;
Servo Extensor;
Servo Flexor;
int pos = 0;
int biceppin = 3;
const int triceppin = 4;
const int extensorpin = 5;
const int flexorpin = 6;
int potpin = 8;
int potval = 0;
int potval2;
void setup() {
Bicep.attach(biceppin);
Tricep.attach(triceppin);
Extensor.attach(extensorpin);
Flexor.attach(flexorpin);
}
void loop() {
potval = analogRead(potpin);
potval = map(potval, 0, 1023, 0, 180);
potval2 = 180 - potval;
Bicep.write(potval);
Tricep.write(potval2);
delay(15);
}
Upvotes: 2
Views: 1901
Reputation: 1139
You set potpin = 8
, but analogRead()
works only over analog inputs A0-A5
and on most boards, included the tagged Arduino Uno board, pin 8
is a digital pin.
[...] you cannot use analogRead() to read a digital pin. A digital pin cannot behave as analog because it isn't connected to the ADC (Analog to Digital Converter).
You can test this with the example of https://www.arduino.cc/en/Reference/AnalogRead
Upvotes: 2
Reputation: 3282
I think that there are more than one problem there.
Assuming the program is correct (which may not be the case, since as explained in @BOC's answer you are using pin 8 as an analog input while in an Arduino Uno board, that pin is only digital), if the servos jitter, a good candidate as the source of the problem is your power supply:
You stated (on a comment, but I edited your question to specify this, since it's important) that your power supply is your laptop. Most USB ports are limited to 500 mA.
Although you did not explain anything about your particular servos and their specifications, most of them use at least 250 mA (for example, a Futaba S3003). But low cost/low quality servos usually take more than that, as well as bigger servos. But even if your servos are using 250 mA each one (best case scenario), you are also powering your Arduino Uno board itself (who's linear regulator is not very efficient), and thus you are reaching the limit of your USB port. In my own experience, most (non-mini) servos do take way more than just 250 mA.
Possible solutions
As a fast test, if you lack a good power source, I would do the following, which is in general a good practice when you are mixing code and electronics in a project:
If you have doubts about the potentiometer itself, you can also try the one servo configuration without potentiometer at all, just hardcoding values in the servo.write()
method; something like this:
void loop() {
delay(500);
Bicep.write(50);
delay(500);
Bicep.write(100);
}
That will reduce any potential problem to a software only problem, since now, according to your description, it's not clear wether you are facing an electronic configuration problem or a software bug.
Upvotes: 1