0

I am trying to use the i2c library wire.h with my Arduino MKR1000. I can compile and upload the simple test program but after uploading, the Arduino resets and doesn't come back (won't connect to the serial port). I have to double click the reset button to get it back into bootloader mode.

I've isolated this to the line "lib device;" where I am creating an object. When this line is included and the "Wire.begin();" line is included in the constructor function, the Arduino crashes. Comment out either line and it works without crashing.

How can I create a i2c master device in a library class?

Main file

#include "lib.h"

int next = 0;
lib device;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (millis() > next) {
    Serial.println(millis());
    next = millis()+1000;
  }
}

lib library files: lib.h

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

class lib {
  public:
    lib();
};

and lib.cpp

#include "lib.h"

lib::lib() {
  Wire.begin();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.