Hello all,
I am trying to make a VR glove that contains multiple sensors as well as stepper motors. I believe the best way of doing this in code is to use classes so I can address the individual sensors on the fingers as such:
int flexVal = thumb.flexSensor.value();
float currentVal = ring.currentSensor.value();
middle.stepper.position(90);
However, I am not very well versed in C++ and the classes don't nest very easily.
#include <AccelStepper.h>
#include <MultiStepper.h>
class Flex {
private:
byte pinA;
byte pinB;
init() {
pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
}
public:
flex(byte pinA, byte pinB) {
this->pinA = pinA;
this->pinB = pinB;
init();
}
double a_value() { return analogRead(pinA); }
double b_value() { return analogRead(pinB); }
};
class Current {
private:
int pin;
public:
Current(int) { init(); }
void init() { pinMode(pin, INPUT); }
double value() { return analogRead(pin); }
};
class Finger {
private:
int currentPin;
byte flexPinA;
byte flexPinB;
void init() { stepper.setMaxSpeed(MAX_SPEED); }
public:
AccelStepper stepper;
Finger(AccelStepper stepper, int currentPin, byte flexPinA, byte flexPinB) : current(currentPin) : flex(flexPinA, flexPinB) {
this->stepper = stepper;
this->currentPin = currentPin;
this->flexPinA = flexPinA;
this->flexPinB = flexPinB;
init();
}
Current current;
Flex flex;
};
MultiStepper steppers;
AccelStepper stThumb(AccelStepper::DRIVER, 2, 3);
AccelStepper stIndex(AccelStepper::DRIVER, 4, 5);
AccelStepper stMiddle(AccelStepper::DRIVER, 6, 7);
AccelStepper stRing(AccelStepper::DRIVER, 8, 9);
// Pinky is not used but is added in just in case of future versions
AccelStepper stPinky(AccelStepper::DRIVER, 11, 12);
Finger thumb(stThumb, 15, 21, 22);
Finger index(stIndex, 16, 23, 24);
Finger middle(stMiddle, 17, 25, 26);
Finger ring(stRing, 20, 27, 38);
Finger pinky(stPinky, 41, 38, 40);
I am trying to have the class Finger
to have two objects attached one from each of the other classes Flex
and Current
.
How would I go about this? Thanks