How to use an object from the main in a class?

Hi ! I guess this problem is quite common, but I didn't succeed to address the issue...
I create an object in the main (a Joystick object from the joystick library).

I create a class SRTPedal.h, that need to get access to the joystick object to set one axis.

In the best case, what I managed to do is to have the object from the class pedal creating a second joystick object...
Here is the code :

#include "src/Joystick.h"

Joystick_ Joystick(0x04, 0x04, 40, 0, true, true, false,  true, false, false, false, false, true, true, false); //I want to use that object in SRTPedals.cpp

#include "SRTPedals.h"
SRTPedals_ SRTPedals(0, 1023, 
Joystick);  //the Joystick to link with


void setup() {
  
delay(2000); //security delay to flash the code
Serial.begin(9600);
SRTPedals.begin();
Joystick.begin();
}

void loop() {
  SRTPedals.setPedals(); //Read A0, and update the axis state.
   }

The SRTPedals.h :

#ifndef SRTPEDALS_H
#define SRTPEDALS_H
#include "src/Joystick.h"

class SRTPedals_
{
private:
Joystick_ Joystick;
long minInput = 0;
long maxInput = 0;

public:
SRTPedals_(long minInput1,
  long maxInput1, Joystick_ Joystick);
  void begin();
  void setPedals();
};

#endif

The SRTPedals.cpp :

#include "SRTPedals.h"
#include <Arduino.h>
#include "src/Joystick.h"


SRTPedals_::SRTPedals_(long minInput1,
  long maxInput1, Joystick_ Joystick){
this->minInput= minInput1;
this->maxInput= maxInput1;
this->Joystick = Joystick;
  }

void SRTPedals_::begin(){ Joystick.setXAxisRange(minInput, maxInput);}

void SRTPedals_::setPedals(){
Joystick.setXAxis(analogRead(A0));     
Serial.println((String)"Axis 1 raw value : "+analogRead(A0));}


I think it's similar to this issue, but I can't sort it out...
pedalArduino.zip (10.9 KB)

pass a reference or a pointer to your other instance

here with a pointer:

your .h becomes:

#ifndef SRTPEDALS_H
#define SRTPEDALS_H
#include "src/Joystick.h"

class SRTPedals_
{
  private:
    long minInput = 0;
    long maxInput = 0;
    Joystick_ * joystickPtr;

  public:
    SRTPedals_(long _minInput, long _maxInput, Joystick_ * _joystickPtr);
    void begin();
    void setPedals();
};

#endif

the .cpp

#include "SRTPedals.h"
#include <Arduino.h>
#include "src/Joystick.h"

SRTPedals_::SRTPedals_(long _minInput, long _maxInput, Joystick_ * _joystickPtr) : minInput(_minInput), maxInput(_maxInput), joystickPtr(_joystickPtr) {}

void SRTPedals_::begin() {
  joystickPtr->setXAxisRange(minInput, maxInput);
}

void SRTPedals_::setPedals() {
  joystickPtr->setXAxis(analogRead(A0));
  Serial.println((String)"Axis 1 raw value : " + analogRead(A0));
}

and the .ino

#include "src/Joystick.h"

Joystick_ Joystick(0x04, 0x04, 40, 0, true, true, false,  true, false, false, false, false, true, true, false); //I want to use that object in SRTPedals.cpp

#include "SRTPedals.h"
SRTPedals_ SRTPedals(0, 1023, &Joystick);  //the Joystick to link with


void setup() {
  delay(2000); //security delay to flash the code
  Serial.begin(9600);
  SRTPedals.begin();
  Joystick.begin();
}

void loop() {
  SRTPedals.setPedals(); //Read A0, and update the axis state.
}
1 Like

Thank you so much !!!!!!

have fun :slight_smile:

1 Like

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