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.
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!
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 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.
À 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