Use Adafruit_INA219.h inside class

Hello

I’m trying to use library Adafruit_INA219 inside my onw class.
But since 3 day, each time i upload my sketch to my arduino, Arduino reboot itself on init

solarTracker.h

#include <Arduino.h>

#ifndef solarTracker_h
#define solarTracker_h

#include "Adafruit_INA219.h"

class solarTracker {
  public:

    solarTracker(char motorLetter, int ina219Addr);
   
	
  private:
	
	Adafruit_INA219 *_ina219;
	float _current_mA;	
    
};



#endif

solarTracker.cpp

#include <Arduino.h>
#include "solarTracker.h"

#include "Adafruit_INA219.h"



solarTracker::solarTracker(char motorLetter, int ina219Addr) {

	_ina219 = new Adafruit_INA219(ina219Addr);
	_ina219->begin();                                        // **** Seem to be where problem occurred ! ***

	_current_mA = _ina219->getCurrent_mA();
	
}


void solarTracker::motorBegin(byte PWM, byte IN1, byte IN2) {
    _PWM = PWM;
    _IN1 = IN1;
    _IN2 = IN2;
	
	
    pinMode(_PWM,OUTPUT);
    digitalWrite(_PWM,LOW);
    pinMode(_IN1,OUTPUT); 
    digitalWrite(_IN1,LOW);
    pinMode(_IN1,OUTPUT); 
    digitalWrite(_IN1,LOW);
	
}


float solarTracker::currentMotorGetCurrent() {
	return _ina219->getCurrent_mA();
}

Arduino code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_INA219.h>
#include <solarTracker.h>

solarTracker moteurV('A', 0x41);


void setup() {
    moteurV.motorBegin(2, 3, 4);
}



void loop() {


  ... rest of code ...
}

Any idea ?

Thanks
Manu

You must introduce a begin() method (or something similar) where you call the begin() method of the Adafruit object and the getCurrent_mA() method. This begin() method should be called in setup() then. Otherwise this code is called during the initialization where non of the libraries is ready to fulfill your requests.

Hello

Thanks for your reply.

Yes, it works now :wink:

How can i say … THANKS TO YOU !!! :wink:
The power of community …

For anybody who want to understand, look below full correct code:

SolarTracker.h

#include <Arduino.h>

#ifndef solarTracker_h
#define solarTracker_h

#include "Adafruit_INA219.h"

class solarTracker {
  public:

    solarTracker(char motorLetter, int ina219Addr);
    void begin();
   
 
  private:
 
 Adafruit_INA219 *_ina219;
 float _current_mA; 
   
};



#endif

SolarTracker.cpp

#include <Arduino.h>
#include "solarTracker.h"

#include "Adafruit_INA219.h"



solarTracker::solarTracker(char motorLetter, int ina219Addr) {

 _ina219 = new Adafruit_INA219(ina219Addr);
 
}

void solarTracker::begin() {
 _ina219->begin();
}


void solarTracker::motorBegin(byte PWM, byte IN1, byte IN2) {
    _PWM = PWM;
    _IN1 = IN1;
    _IN2 = IN2;
 
 
    pinMode(_PWM,OUTPUT);
    digitalWrite(_PWM,LOW);
    pinMode(_IN1,OUTPUT);
    digitalWrite(_IN1,LOW);
    pinMode(_IN1,OUTPUT);
    digitalWrite(_IN1,LOW);
 
}


float solarTracker::currentMotorGetCurrent() {
 return _ina219->getCurrent_mA();
}

Arduino code

void setup() {
    moteurV.motorBegin(2, 3, 4);
    moteurV.begin();
}



void loop() {


  ... rest of code ...
}

Manu

1. Can we see the full codes of your loop() function?

2. How have you created the object moteurV?

3. What is the purpose of your program. If possible, post the schematic/block diagram connection of the gadgets you have used.