Sketch is not working with Nano perfectly like Mega2560

Hello,

the wires' length is not more than 20cm...

Another problem I have just noticed: if I disable the Serial.begin from the code - sensor works but gives unexpected results...and to see it, I again need Serialmonitor..Do you think it is because of Serial port stack overflow.... (I have connected sensor to 3v3 nano pin)

I thought it would be too complicated since code is so long but ...
here is my code. :slight_smile:

#include <Wire.h>
#define VL6180X_ADDRESS 0x29     //  Device address of VL6180X  
#define getreading 1
#define wait 2
#define valveon  3
#define get_time 4
#define check_time 5
#define pumpoff 6

unsigned long targettime;
unsigned long waittime;
uint8_t next_stage = getreading;
int counter = 0;
int counter2 = 0;
int counter3=0;
int counter4=0;

int reading = 0;
int analogPin =A0;     // potentiometer wiper (middle terminal) connected to analog pin 3

int D4=4;
int D6=6;
int D8=8;
int D10=10;


void WriteByte (uint16_t subAddress, uint8_t data) {    //void WriteByte (uint16_t subAddress,uint8_t data)
  delay(10);
  Wire.beginTransmission(VL6180X_ADDRESS);     // I2C_I2CMasterSendStart(VL6180X_ADDRESS, I2C_I2C_WRITE_XFER_MODE);
  Wire.write((subAddress >> 8) & 0xFF);  //    I2C_MasterWriteByte((subAddress >> 8) & 0xFF);
  Wire.write(subAddress & 0xFF);           //  I2C_MasterWriteByte(subAddress  & 0xFF);
  Wire.write(data);                         //  I2C_MasterWriteByte(data);
  Wire.endTransmission();                  //  I2C_MasterSendStop();
  
}
//
uint16_t readByte (uint16_t subAddress) {

   
  Wire.beginTransmission(VL6180X_ADDRESS);
  Wire.write((subAddress >> 8) & 0xFF); // unit32 SCB_i2cMasterWriteByte (unit32 theByte)
  Wire.write(subAddress & 0xFF);
  Wire.endTransmission(false);
  delay(1);
  Wire.requestFrom(VL6180X_ADDRESS, 2);
//return read();

 reading = Wire.read();  // receive high byte (overwrites previous reading)
 //reading = reading << 8;    // shift high byte to be high 8 bits
 //reading |= Wire.read(); // receive low byte as lower 8 bit
 //return reading;

}


void setup () 
  {
    Serial.begin(2400);
    Wire.begin (); 
    pinMode (LED, OUTPUT);
    pinMode (D4, OUTPUT);
    pinMode (D6, OUTPUT);
    pinMode (D8, OUTPUT);
    pinMode (D10,OUTPUT);
    pinMode (A0,INPUT);

  
// initialize hardware registers etc.
 VL6180x_Init() ;

// WriteByte(0x0207, 0x01);
  }

  void VL6180x_Init() {
  
  delay(100);
      if (readByte(0x016) == 1) {

    WriteByte(0x0207, 0x01);
    WriteByte(0x0208, 0x01); 
    WriteByte(0x0096, 0x00); 
    WriteByte(0x0097, 0xfd); 
    WriteByte(0x00e3, 0x00); 
    WriteByte(0x00e4, 0x04); 
    WriteByte(0x00e5, 0x02); 
    WriteByte(0x00e6, 0x01); 
    WriteByte(0x00e7, 0x03); 
    WriteByte(0x00f5, 0x02); 
    WriteByte(0x00d9, 0x05); 
    WriteByte(0x00db, 0xce); 
    WriteByte(0x00dc, 0x03); 
    WriteByte(0x00dd, 0xf8); 
    WriteByte(0x009f, 0x00); 
    WriteByte(0x00a3, 0x3c); 
    WriteByte(0x00b7, 0x00); 
    WriteByte(0x00bb, 0x3c);
    WriteByte(0x00b2, 0x09); 
    WriteByte(0x00ca, 0x09); 
    WriteByte(0x0198, 0x01); 
    WriteByte(0x01b0, 0x17); 
    WriteByte(0x01ad, 0x00); 
    WriteByte(0x00ff, 0x05); 
    WriteByte(0x0100, 0x05); 
    WriteByte(0x0199, 0x05); 
    WriteByte(0x01a6, 0x1b); 
    WriteByte(0x01ac, 0x3e); 
    WriteByte(0x01a7, 0x1f); 
  WriteByte(0x0030, 0x00);
  // Recommended : Public registers - See data sheet for more detail
WriteByte(0x0011, 0x10); // Enables polling for ‘New Sample ready’
// // when measurement completes
WriteByte(0x010a, 0x30); // Set the averaging sample period
// // (compromise between lower noise and
// // increased execution time)
WriteByte(0x003f, 0x46); // Sets the light and dark gain (upper
// // nibble). Dark gain should not be
// // changed.
WriteByte(0x0031, 0xFF); // sets the # of range measurements after
// // which auto calibration of system is
// // performed
WriteByte(0x0040, 0x63); // Set ALS integration time to 100ms
WriteByte(0x002e, 0x01); // perform a single temperature calibration
// // of the ranging sensor 
//
WriteByte(0x001b, 0x09); // Set default ranging inter-measurement
// // period to 100ms
WriteByte(0x003e, 0x31); // Set default ALS inter-measurement period
// // to 500ms
WriteByte(0x0014, 0x24); // Configures interrupt on ‘New Sample
// // Ready threshold event’ 
WriteByte(0x001b, 0x09); // Set default ranging inter-measurement
 // period to 100ms
WriteByte(0x003e, 0x31); // Set default ALS inter-measurement period
 // to 500ms
WriteByte(0x0014, 0x24); // Configures interrupt on ‘New Sample
 // Ready threshold event’ 
    //}
    WriteByte(0x0016, 0x00);

  // end of setup
}
  }

void VL6180X_Poll_Range() {
 uint16_t tstatus;
 uint16_t range_status;
 
 // check the status
 tstatus = readByte(0x04f);
 range_status = tstatus & 0x07;
  //delay(10);


 // wait for new measurement ready status
 while (range_status != 0x04) {
// WriteByte(0x018, 0x01);
 delay(5);
 tstatus = readByte(0x04f);
 range_status = tstatus & 0x07;
 Serial.println("DEBUG: wait_for_ready");

 //Serial.println(tstatus);
 }
 
 }