Problem with code for VR glove

Hi! So I have recently been working on this project which is supposed to be a VR glove that limits your hand movements and takes data from Unity game engine and lets you feel the shape and size of virtual objects. However, I am not the most experienced programmer and cannot seem to get it working right now. I connected a wire to a servo to detect its position in real time which is the indexFingerPos variable. I just need to have the servo be unattached when it is less than the indexFingerPos and have it attach to port 9 when it is past it and send it one degree less than the lockPos so that it stops locking it. Here is the section of my code that handles this.

if(indexPosition > indexLockPos)
  {    
    servo1.attach(9);
  
    servo1.write(indexLockPos - 1);

    delay(30);

    servo1.detach();
  }

I got it working for a little bit, however, it was very jittery so I kept fiddling with it and now it never detaches once past the lockPos. I would really appreciate some help on this as I am stumped. Thank you!

See Snippets R Us!... (we have no clue where indexPosition and indexLockPos are changed, how, what’s their type...)

Sorry about that here is the entire thing.

#include <Servo.h>
#include<Wire.h>

//const int MPU_addr=0x68;  // I2C address of the MPU-6050
//int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

Servo servo1;
Servo servo2;

int analogPin2 = 2;
int analogPin3 = 3;
int indexLockPos = 40;
int thumbLockPos = 10;

void setup()
{
  //Wire.begin();
  //Wire.beginTransmission(MPU_addr);
  //Wire.write(0x6B);  // PWR_MGMT_1 register
  //Wire.write(0);     // set to zero (wakes up the MPU-6050)
  //Wire.endTransmission(true);
  
  Serial.begin(2000000);  
}

void loop()
{
  //Index Finger Data
  int indexVal = analogRead(analogPin2); 
  int indexPosition = map(indexVal, 81, 487, 0, 180); 
 //Thumb Finger Data
  int thumbVal = analogRead(analogPin3); 
  int thumbPosition = map(thumbVal, 81, 487, 0, 180); 

  //Wire.beginTransmission(MPU_addr);
  //Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  //Wire.endTransmission(false);
  //Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  
  //GyX = Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  //GyY = Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  //GyZ = Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

  
  
  Serial.print(thumbPosition);
  Serial.print(",");
  Serial.println(indexPosition);
  //Serial.print(",");
  //Serial.print(GyX);
  //Serial.print(",");
  //Serial.print(GyY);
  //Serial.print(",");
  //Serial.println(GyZ);
  //----------LockPos Start-------------------------------------|
  
  if(indexPosition > indexLockPos)
  { 
    servo1.attach(9);
  
    servo1.write(indexLockPos - 10);

    delay(30);

    servo1.detach();
  }
  

  if(thumbPosition > thumbLockPos)
  {
    servo2.attach(10);
  
    servo2.write(thumbLockPos - 10);

    delay(30);

    servo2.detach();
  }
  //----------LockPos End-------------------------------------|

}

Just a note, the commented out parts deal with the accelerometer which I am not currently focused on.

that's an ambitious baud rate... Serial.begin(2000000);

That will likely not compile

 }
  else

  //----------LockPos End-------------------------------------|

}

did you miss some code?

My bad, I copied it wrong.

#include <Servo.h>
#include<Wire.h>

//const int MPU_addr=0x68;  // I2C address of the MPU-6050
//int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

Servo servo1;
Servo servo2;

int analogPin2 = 2;
int analogPin3 = 3;
int indexLockPos = 40;
int thumbLockPos = 10;

void setup()
{
  //Wire.begin();
  //Wire.beginTransmission(MPU_addr);
  //Wire.write(0x6B);  // PWR_MGMT_1 register
  //Wire.write(0);     // set to zero (wakes up the MPU-6050)
  //Wire.endTransmission(true);
  
  Serial.begin(9600);  
}

void loop()
{
  //Index Finger Data
  int indexVal = analogRead(analogPin2); 
  int indexPosition = map(indexVal, 81, 487, 0, 180); 
 //Thumb Finger Data
  int thumbVal = analogRead(analogPin3); 
  int thumbPosition = map(thumbVal, 81, 487, 0, 180); 

  //Wire.beginTransmission(MPU_addr);
  //Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  //Wire.endTransmission(false);
  //Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  
  //GyX = Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  //GyY = Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  //GyZ = Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)

  
  
  Serial.print(thumbPosition);
  Serial.print(",");
  Serial.println(indexPosition);
  //Serial.print(",");
  //Serial.print(GyX);
  //Serial.print(",");
  //Serial.print(GyY);
  //Serial.print(",");
  //Serial.println(GyZ);
  //----------LockPos Start-------------------------------------|
  
  if(indexPosition > indexLockPos)
  { 
    servo1.attach(9);
  
    servo1.write(indexLockPos - 10);

    delay(30);

    servo1.detach();
  }
  

  if(thumbPosition > thumbLockPos)
  {
    servo2.attach(10);
  
    servo2.write(thumbLockPos - 10);

    delay(30);

    servo2.detach();
  }
}

So that's your code without all the un-necessary pieces that were commented out and with a few modification to make it cleaner

#include <Servo.h>
#include<Wire.h>

Servo servo1;
Servo servo2;

const byte servo1Pin = 9;
const byte servo2Pin = 10;

const byte indexPin = A2;
const byte thumbPin = A3;

const int indexLockPos = 40;
const int thumbLockPos = 10;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  //Index Finger Data
  int indexVal = constrain(analogRead(indexPin), 81, 487); // just to be sure
  int indexPosition = map(indexVal, 81, 487, 0, 180);

  //Thumb Finger Data
  analogRead(thumbPin); // to make sure to have a clean read, we read once and throw it away
  int thumbVal = constrain(analogRead(thumbPin), 81, 487); // just to be sure
  int thumbPosition = map(thumbVal, 81, 487, 0, 180);

  Serial.print(thumbPosition);
  Serial.print(F(", "));
  Serial.println(indexPosition);

  if (indexPosition > indexLockPos) {
    servo1.attach(servo1Pin);
    servo1.write(indexLockPos - 10);
    delay(30);
    servo1.detach();
  }

  if (thumbPosition > thumbLockPos) {
    servo2.attach(servo2Pin);
    servo2.write(thumbLockPos - 10);
    delay(30);
    servo2.detach();
  }
}

So now the question is what's your problem? (the if have no else, so nothing happens if you don't meet the condition... and should those [color=blue]>[/color] be actually [color=blue]<[/color] ? )

Yes, that is the correct code. The problem is that once the servos position goes past the lockPos, it locks but stays locked in that position. I need to be able to move freely until it gets past that position and instead of locking, it acts as more of a block. The servo is still able to be pushed back freely but it just can't go past that point. Hope that makes sense, thanks!

I don’t get what you are hoping to do
You never move the servos anywhere but to the lock pos -10

I still wonder if you are sure you want > versus < in   if (indexPosition [color=red]>[/color] indexLockPos) {

You detach your servos so they are no longer asked to hold one specific position once the condition is no longer met

Have you “hacked” the servos (or purchased some with the capability) to add one wire so that The analogRead is actually the current servo position ?

I am not actually trying having the servo apply any forces until it reaches the lockPos which it then just acts as a limit or block, the goal is that you actually push the servo with your finger until it reaches that position and it limits how far you are able to push it.

Also, yes I did "hack" the servos and am just getting raw data from the build in potentiometer.

Would have been good to say so :slight_smile:

Have you tried just printing out the values from the servos ? Where do you power them from? What type of servos are they ?

what do you mean by printing out the value of the servos? I am currently using J-Deal servos and powering them with 5v.

À SG90 Micro Servo (i assume this is what they are) can sink up to 550mA, that’s a lot for your arduino (esp if you have 2). having a good dedicated power supply would make sense

I meant ensuring analogRead() does return what you want when you move the arm around

Have a look at kammo’s post (in French but here is code and a demo video link) for handling the servo and see if that works for you - would be a good sign all is wired right