Problem whit the code

I'm working on a bionic hand project and I've already managed to make each finger move
individually, but I can't make the fine adjustments and gestures.
If anyone wants to help me, this is the code:

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;

int Bt = 11;
int Bt2 = 10;
int Bt3 = 9;
int Bt4 = 8;

const int pinoSW = 7;
int joyY = A1;
int joyX = A0;
int pos = 0;

void setup(){
  servo1.attach(2);
  servo2.attach(3);
  servo3.attach(4);
  servo4.attach(5);
  servo5.attach(6);
  pinMode(Bt, INPUT_PULLUP);
  pinMode(Bt2, INPUT_PULLUP);
  pinMode(Bt3, INPUT_PULLUP);
  pinMode(pinoSW, INPUT_PULLUP);
}

void loop(){
  if(digitalRead(Bt) == (HIGH) && analogRead(joyX) > 600){
    servo1.write(90);
  }else {
    servo1.write(0);
  }
  
  if(digitalRead(Bt2) == (HIGH) && analogRead(joyX) > 600){
    servo2.write(90);
  }else {
    servo2.write(0);
  }
  
  if(digitalRead(Bt3) == (HIGH) && analogRead(joyX) > 600){
    servo3.write(90);
  }else {
    servo3.write(0);
  }
  
  if(digitalRead(Bt4) == (HIGH) && analogRead(joyX) > 600){
    servo4.write(90);
  }else {
    servo4.write(0);
  }
  
  int estadoBotao = digitalRead(pinoSW);
  if(estadoBotao == (HIGH)){
    servo5.write(90);
  }else {
    servo5.write(0);
  }
}

The current code only moves each servo to either 0 or 90 degrees depending on button presses or joystick position, which is why you can’t get fine adjustments or gestures.

You need to map the joystick values to a range of servo angles instead of just switching between two fixed positions. For example, you can read the joystick with analogRead , map it to 0–180 degrees, and write that to the servo. Using map() and possibly smoothing the values (take 90% of the previous value and 10% of the new value) will allow gradual finger movements. You also might want to control multiple fingers at once by combining joystick axes and buttons rather than treating each finger completely separately.

2 Likes

i think what you might try is scaling the readings of your analog joystick to the range of motion of your servos beyond your threshold of 600, so 600-1023.

You should be able to use that result and map it to the range you determine you want to use on each servo, using servo.writeMicroseconds(val);.

The total range of your servos will be available from their datasheet, something like 640-2400 (for some models). Often, servo.writeMicroseconds(1000); is used as one extreme position (0 degrees), servo.writeMicroseconds(1500); is "neutral" or 90 degrees and servo.writeMicroseconds(2000); corresponds to 180 degrees, but not always as I said.

You just have to check the datasheet and you should find this gives you finer control.

Another trick you might use is setting each finger in its own function and calling servo1.attach(2); (and the rest) only right before you are about to move a particular digit, then servo1.detach(); once you are finished with the motion. It will help avoid servo jitter and save on wear and tear.

Finally, I would name each servo to correspond to the digit it represents, something like Servo thumb; or whatever. I don't know about you, but it would help me visualize what's going on with what I'm coding better, as you will probably look at your own hand to try to get a realistic motion as you go, so it's easier to instantly see that, say, Servo ring; and Servo pinkie; will generally move together, that kind of thing.

1 Like

Gestures use the same code format as fingers. Estimate the angles of all servos and then calculate (or "map()") the initial position to the final position.

I have deleted your other cross-post @ticos_micos.

Cross-posting is against the Arduino Forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.