I am trying to implement the SoftI2CMaster with VCNL 4040 Proximity sensor from SparkFun interfaced with Arduino Mega, when i compile the code, it only displays "I2C device busy". Please below is the code, i am kind of new to this, can you help how to do i read the proximity readings from the sensor
// -- c++ --
// Simple sketch to read out one register of an I2C device
define I2C_TIMEOUT 1000
define I2C_PULLUP 1 //to enable the internal pullup resistores of the MCU
define SDA_PORT PORTC
define SDA_PIN 4
define SCL_PORT PORTC
define SCL_PIN 5
//#define I2C_FASTMODE 1 //to permit for faster transfers up to 400kHz
include
define I2C_7BITADDR 0x60 // DS1307
//#define MEMLOC 0x0A
define MEMLOC 0x0C
//#define ADDRLEN 3 // address length, usually 1 or 2 bytes
void setup(void) {
Serial.begin(9600);
if (!i2c_init()) Serial.println("I2C init failed"); // Initialize everything and check for bus lockup
}
void loop(void){
if (!i2c_start_wait((I2C_7BITADDR<<1)|I2C_WRITE)) // start transfer
{
Serial.println("I2C device busy");
delay(100);
return;
}
// for (byte i=3; i<ADDRLEN; i++) i2c_write(0x00);
i2c_write(MEMLOC); // send memory address
i2c_rep_start((I2C_7BITADDR<<1)|I2C_READ); // restart for reading
byte val = i2c_read(true); // read one byte and send NAK to terminate
i2c_stop(); // send stop condition
Serial.println(val);
delay(1000);
I am trying to implement the SoftI2CMaster with VCNL 4040 Proximity sensor from SparkFun interfaced with Arduino Mega, when i compile the code, it only displays "I2C device busy". Please below is the code, i am kind of new to this, can you help how to do i read the proximity readings from the sensor
// -- c++ -- // Simple sketch to read out one register of an I2C device
define I2C_TIMEOUT 1000
define I2C_PULLUP 1 //to enable the internal pullup resistores of the MCU
define SDA_PORT PORTC
define SDA_PIN 4
define SCL_PORT PORTC
define SCL_PIN 5
//#define I2C_FASTMODE 1 //to permit for faster transfers up to 400kHz
include
define I2C_7BITADDR 0x60 // DS1307
//#define MEMLOC 0x0A
define MEMLOC 0x0C
//#define ADDRLEN 3 // address length, usually 1 or 2 bytes
void setup(void) { Serial.begin(9600); if (!i2c_init()) Serial.println("I2C init failed"); // Initialize everything and check for bus lockup }
void loop(void){ if (!i2c_start_wait((I2C_7BITADDR<<1)|I2C_WRITE)) // start transfer { Serial.println("I2C device busy"); delay(100); return; } // for (byte i=3; i<ADDRLEN; i++) i2c_write(0x00); i2c_write(MEMLOC); // send memory address i2c_rep_start((I2C_7BITADDR<<1)|I2C_READ); // restart for reading byte val = i2c_read(true); // read one byte and send NAK to terminate i2c_stop(); // send stop condition Serial.println(val); delay(1000);
}