Code getting stuck at Wire.endTransmission();

When I use the below-mentioned function in the void loop it works fine, but when I use this function in some external defined if in a function it gets stuck at Wire.endTransmission();. The other if function is also stated below.

I am using Redbear Blendv2 for the testing of this code and also have attached the ino file for any clarification.

void readData()
{
Serial.println(“Inside read”);
int i = 0;
uint32_t data[8];
Wire.beginTransmission(BME280_ADDRESS);
Serial.println(“After begin”);
Wire.write(0xF7);
Serial.println(“After write”);
Wire.endTransmission();
Serial.println(“After end”);
Wire.requestFrom(BME280_ADDRESS,8);
Serial.println(“After request”);
while(Wire.available()){
data = Wire.read();

  • i++;*
  • Serial.println(“Inside while of read”);*
  • }*
  • pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4);*
  • temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4);*
  • hum_raw = (data[6] << 8) | data[7];*
    }
    void m_status_check_handle() {
    if (analog_enabled && k==5) {
  • Serial.print(“Inside Environment”);*
  • // if analog reading enabled*
  • // Read and send out*
  • // uint16_t value = analogRead(ANALOG_IN_PIN_X);*
  • //uint16_t value1 = analogRead(ANALOG_IN_PIN_Y);*
  • //uint16_t value2 = analogRead(ANALOG_IN_PIN_Z);*
  • double temp_act = 0.0, press_act = 0.0,hum_act=0.0;*
  • signed long int temp_cal;*
  • unsigned long int press_cal,hum_cal;*
  • readData();*
  • temp_cal = calibration_T(temp_raw);*
  • press_cal = calibration_P(pres_raw);*
  • hum_cal = calibration_H(hum_raw);*
  • temp_act = (double)temp_cal / 100.0;*
  • press_act = (double)press_cal / 100.0;*
  • hum_act = (double)hum_cal / 1024.0;*
  • uint16_t hum_act_new= (uint16_t)hum_act;*
  • uint16_t press_act_new= (uint16_t)press_act;*
  • uint16_t temp_act_new= (uint16_t)temp_act;*
  • Serial.print("TEMP : ");*
  • Serial.print(temp_act_new);*
  • Serial.print(" DegC PRESS : ");*
  • Serial.print(press_act_new);*
  • Serial.print(" hPa HUM : ");*
  • Serial.print(hum_act_new);*
  • Serial.println(" %"); *
  • buf[0] = (0x0B);*
  • buf[1] = (hum_act_new >> 8);*
  • buf[2] = (hum_act_new);*
  • buf[3] = (0x0A);*
  • buf[4] = (0x0B);*
  • buf[5] = (press_act_new >> 8);*
  • buf[6] = (press_act_new);*
  • buf[7] = (0x0A);*
  • buf[8] = (0x0B);*
  • buf[9] = (temp_act_new >> 8);*
  • buf[10] = (temp_act_new);*
  • ble.updateCharacteristicValue(characteristic2.getValueAttribute().getHandle(), buf, 9);*
  • }*
    }
    BME280.ino (20 KB)

The Wire.endTransmission() function has no idea whether it is being called from loop() or from some other function.

If you think that the result is different, you need to prove it. Post ALL of your code. Make sure that that code does NOTHING more than call the Wire.startTransmission() through Wire.endTransmission() in loop() and in some other function that is called by loop().

I was told a long time ago to stick a delay(5); just after the Wire.endTransmission(); , admittedly in the context of writing to eeprom.

So I wonder if the difference is that you have such a delay() (even if by accident or for another purpose) in loop() so it works, and not where it doesn't?

No harm in trying...

(But it would help you to get help if you posted all your code as suggested and while you're at it put it in code tags so it's not half in italic and full of 8)'s )

I have attached the .ino file, with the query.

It would have been better if you just edited the code in the thread with code tags. Select the code and hit the </> key in full edit. Or you can type a [code] in front and a [/code] at the back.

Did you try my delay(5) idea?