2

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);
}
  • Would you be able to tell me what is wrong with the code?
  • Is there a more efficient way to do the same task?
nbloqs
  • 3,152
  • 1
  • 28
  • 49
Thing
  • 51
  • 6
  • maybe those ee folks would like a crack at this: http://electronics.stackexchange.com – Grady Player Feb 22 '17 at 03:32
  • 1
    What's in `Servo.h`? C++ programs require a `main` function. Where is yours? Please provide a complete minimal example so we can help you. – Ben Feb 22 '17 at 03:43
  • 1
    @Ben: removed not relevant c++ tag, but this is a perfectly valid *mcve* for *Arduino*. @RithvikKolanu if you don't provide your *servo model numbers* it is impossible for us to know what should happen when you `write()` values on the servo.. each one has its own specs. – Patrick Trentin Feb 22 '17 at 04:35
  • @Ben: programs in the Arduino IDE use a hidden main, so setup() and loop() makes a complete, valid program there. I know: it's horrible, but it is what it is. The user stated that he is using Arduino. – nbloqs Feb 22 '17 at 04:36
  • @Rithvik Kolanu: you should be more specific than just "it's not working out". What is not working? Are the servos moving at least, or they do not move at all? Are the connection pins correct? How about your power supply? Also, there is a specific Arduino Stack Exchange: http://arduino.stackexchange.com/ – nbloqs Feb 22 '17 at 04:41
  • 2
    @nbloqs my power supply is my laptop, I haven't used a battery pack yet. As for the issue, the servos aren't responding to the potentiometer and they just jitter – Thing Feb 22 '17 at 13:51

2 Answers2

2

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.

relevant quote:

[...] 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

Community
  • 1
  • 1
BOC
  • 1,109
  • 10
  • 20
  • I think that you are referring to "Volts" by "0 to 5". This should not be confused with the value returned by the analogRead Arduino API function, which returns 0 to 1023, since it's the result of a 10 bits ADC (Analog to Digital Converter). – nbloqs Feb 22 '17 at 14:48
  • 1
    @nbloqs I think he meant the *analog pins* `A0-A5`, he observed that the OP is using pin `potpin = 8` as parameter of `analogRead()`, which [shouldn't work](http://arduino.stackexchange.com/questions/13522/can-i-use-analogread-to-read-a-digital-pin) in contrast with what the OP might expect – Patrick Trentin Feb 22 '17 at 15:04
  • You're right! If someone edits the answer I will rollback my -1! (since I can't now without it being edited.) – nbloqs Feb 22 '17 at 15:06
  • 1
    @nbloqs edited, both of your answers are quite valid – Patrick Trentin Feb 22 '17 at 15:09
  • Gave a +1 then! – nbloqs Feb 22 '17 at 15:10
1

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:

  1. 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.

  2. 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:

  1. Connect only one servo (let's say bicep), plus the potentiometer.
  2. Run your program and test the servo's movements.
  3. If it works, disconnect that servo and connect the other one.
  4. Run and test, specially direction.
  5. If they still jitter, try the one configuration servo with a better power supply. Be careful: most servos need 4.8V as the input voltage, so do not go over that voltage since you can damage them or significantly reduce their life time.
  6. 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.

nbloqs
  • 3,152
  • 1
  • 28
  • 49