Wire.endTransmission() hangs sketch

Hi,,,I just want to offer some facts in case that happens to be ralativelly to the subject of this post.

I have a very simple arduino uno program.
An I2C LCD screen is attached to arduino.
But the Vcc (of LCD) is passing from a switch
that also this switch is pined on #2 of arduino.

So that when switch is on(closed) i have lcd-i2c working...

But i must have in my code the lcd.begin();
And this causes further calls to (from library)

void LiquidCrystal_I2C::expanderWrite(uint8_t _data){

  • Serial.println("1.");*
    Wire.beginTransmission(_addr);
  • Serial.println("2.");*
    Wire.write((int)(_data) | _backlightval);
  • Serial.println("3.");*
    Wire.endTransmission();
  • Serial.println("4.");*
    }

and hangs in 3...before endTransmisiion

here are the steps (assuming lcd is powered on startup)

a)We have LCD...and we are on lcd mode
b)We push the switch and going to the other mode

c)We again push switch ...the lcd takes voltage and turns on
but the Wire.endTransmission(); of the library make it hung
(from mine code ->lcd.begin())

d)close switch ...then the previus hang un-hangs...but again hungs

e)open again switch and finally all good.

Meaning i must make a further off-on action in order to UN- hang..

entering LCD mode
BEFORE expanderWrite(_backlightval);
1.
2.
3.
4.
AFTER expanderWrite
1.
2.
3.
4.
1.
2.
3.
4.
.............many 1.2.3.4.here
1.
2.
3.
4.
LCD loopin 3910Starting loop ..all good
entering SMS modeSwitch off ..all good
SMS loopin 6917loop is working fine
entering LCD mode Here i press the switch again to ON but.....
BEFORE expanderWrite(_backlightval);
1.
2.
3..... Hangs here
4.
AFTER expanderWrite
1.
2.
3.Unstack previus and hang again here when switrch is off (also lcd power off)

And as i ve said on the second switch on ...all good......

Just in case someone have a simple idea how to fix...i am making this post.
(I know its an old thing the hanging of endTransmission....)

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int was,is;
bool lcdmode(){
return is==HIGH;
}
void setup() {
Serial.begin(9600);
pinMode(2,INPUT);
is=digitalRead(2);
was=is+1;//to enter some mode on beginning
}
void loop() {
is=digitalRead(2);
if(is!=was){//if button pressed....no interrupts using
was=is;
if(lcdmode()){
Serial.println("entering LCD mode");
delay(500);
lcd.begin();
Serial.println("begun()");
lcd.print("welcome");
}
else{Serial.println("entering SMS mode");}
}
if(lcdmode()){Serial.print("LCD ");}
else{Serial.print("SMS ");}

Serial.print("loopin ");
Serial.println(millis());
delay(3000);
}