Multiple objects inside of class

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();

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 {
    byte pinA;
    byte pinB;
    init() { 
      pinMode(pinA, INPUT); 
      pinMode(pinB, INPUT);

    flex(byte pinA, byte pinB) {
        this->pinA = pinA;
        this->pinB = pinB;
    double a_value() { return analogRead(pinA); }
    double b_value() { return analogRead(pinB); }

class Current {
    int pin;

    Current(int) { init(); }

    void init() { pinMode(pin, INPUT); }
    double value() { return analogRead(pin); }

class Finger {
    int currentPin;
    byte flexPinA;
    byte flexPinB;
    void init() { stepper.setMaxSpeed(MAX_SPEED); }

    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;
    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

In the Finger declaration, change:

    Current current;
    Flex flex;

to something like:

    Current current[2];
    Flex flex[2];


    Current currentA, currentB;
    Flex flexA, flexB;

Hi, I tried those solutions however I am still getting an issue with this line inside the Finger class:

Finger(AccelStepper stepper, int currentPin, byte flexPinA, byte flexPinB) : current(currentPin) : flex(flexPinA, flexPinB) { 

I am getting this error

error: expected '{' before ':' token

I the error goes away and it compiles if I remove the : flex(flexPinA, flexPinB)
Does c++ not allow multiple objects in a class? Or have I got a syntax issue?

The second colon should be a comma:

Finger(AccelStepper stepper, int currentPin, byte flexPinA, byte flexPinB) : current(currentPin), flex(flexPinA, flexPinB) { 

You should probably also initialize the stepper in the initializer list.

I am not familiar with the initializer list, could you please explain or direct me towards an explanation? Thanks.

Also, I tried using a comma instead of a colon and it fixed part of the issue, the new issue was that I did not use a capital letter for the constructor of Flex. Annoying little error that caused a lot of debugging time.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.