Hello, I am trying to create a library that uses soft wire to communicate with an i2c device. I have written this code that contains the header, source, and Arduino code in one file. The code works perfectly but when I separate each of these into different files (.h, .cpp, .ino), I keep getting errors such as undeclared variables and such. What is the correct way to declare the variables so that they have the correct scope?
#define SDA_PORT PORTD
#define SDA_PIN 2
#define SCL_PORT PORTD
#define SCL_PIN 3
#define I2C_TIMEOUT 100
#include "Arduino.h"
#include <SoftWire.h>
/////////////////////////HEADER////////////////////////////////////////////////////////////////////
#ifndef AS5600_h
#define AS5600_h
class AS5600
{
//Code not important (cut bc it exceeded character limit)//////////////////////////////////
};
#endif
///////////////////////////SOURCE//////////////////////////////////////////////////////////////
SoftWire Wire = SoftWire();
AS5600::AS5600() {
Wire.begin();
}
//*Code cut again here (not important)///////////////////////////////////////////////////////////////////////
/*
* Function: _getRegister
* ----------------------------
* register1: register address
*
* returns: the value within a register.
*/
uint8_t AS5600::_getRegister(byte register1) {
uint8_t _b=0;
Wire.beginTransmission(_AS5600Address);
Wire.write(register1);
Wire.endTransmission();
Wire.requestFrom(_AS5600Address, 1);
while (Wire.available() == 0) { }
_b = Wire.read();
return _b;
}
/*
* Function: _getRegisters2
* ----------------------------
* registerMSB: register address of Most Significant Byte (MMSB)
* registerLSB: register address of Least Significant Byte (MMSB)
*
* returns: the value of the 16 bit number stored in registerMSB and registerLSB.
*/
uint16_t AS5600::_getRegisters2(byte registerMSB, byte registerLSB) {
uint16_t _hi=0, _lo=0;
_hi = _getRegister(registerMSB);
_lo = _getRegister(registerLSB);
return (_hi<<8) | (_lo);
}
/*
* Function: _writeRegister
* ----------------------------
* registerAddress: register address to write to
* value: value to write to register at registerAddress
*
* returns: void
*/
void AS5600::_writeRegister(byte registerAddress, byte value) {
Wire.beginTransmission(_AS5600Address);
Wire.write((uint8_t)registerAddress); //module function register address
Wire.write((uint8_t)value); //data bytes
Wire.endTransmission();
}
AS5600 encoder;
long revolutions = 0; // number of revolutions the encoder has made
double position = 0; // the calculated value the encoder is at
double output; // raw value from AS5600
long lastOutput; // last output from AS5600
void setup() {
Serial.begin(9600);
output = encoder.getPosition();
lastOutput = output;
position = output;
}
void loop() {
output = encoder.getPosition(); // get the raw value of the encoder
if ((lastOutput - output) > 2047 ) // check if a full rotation has been made
revolutions++;
if ((lastOutput - output) < -2047 )
revolutions--;
position = revolutions * 4096 + output; // calculate the position the the encoder is at based off of the number of revolutions
Serial.println(position);
lastOutput = output; // save the last raw value for the next loop
}