Powering i2c oled display using digital pin

hi everybody, sorry i am asking for your help, i am trying to turn on and off i2c oled display using digital pin using digitalWrite function (pin number, mode) but using digitalWrite (pin number, HIGH) function as switch button pressed OLED display does not show anything but if use digitalWrite (pin number, LOW) if the switch button is not pressed OLED display turns off properly I do not know why, below is the code I ask for your help.

#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <SPI.h>
#include <Wire.h>
#include <EEPROM.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
//#include <Fonts/FreeSans12pt7b.h>
//#include <Fonts/Monospaced_bold_10.h>
#include <ArduinoJson.h>
#include "RTClib.h"
RTC_DS3231 rtc;

//Connect with pin 18 and 19
//long   lat,lon; // create variable for latitude and longitude object
float lat, lon;
TinyGPS gps; // create gps object

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);

StaticJsonBuffer<200> jsonBuffer;
char deviceID[12] = "METER001";

/* Two "independant" timed events */
const long eventTime_1_TIME = 1000; //in ms
const long eventTime_2_BAT = 1000; //in ms
const long eventTime_3_GPRS = 600000; //in ms
const long eventTime_4_ACCELEROMETER = 1000; //in ms
const long eventTime_5_GPS = 1000; //in ms

/* When did they start the race? */
unsigned long previousTime_1 = 0;
unsigned long previousTime_2 = 0;
unsigned long previousTime_3 = 0;
unsigned long previousTime_4 = 0;
unsigned long previousTime_5 = 0;

volatile int flow_frequency; // Measures flow sensor pulses
// Calculated litres/hour
float vol = 0.0, l_minute;
unsigned char flowsensor = 2; // Sensor Input
//unsigned char flowsensor = A0; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;

//-- Accelerometer --//
int Contrast = 20;
#define x A1
#define y A2
#define z A6
int xsample = 0;
int ysample = 0;
int zsample = 0;
#define samples 10
#define minVal -50
#define MaxVal 50

const int Buzzer = 8;

//-- End Accelerometer --//

//Voltage Readings//
int analogInput = A3;
float Percentage = 0.0;
float vout = 0.0;
float vin = 0.0;
//float Re1 = 100000;
//float Re2 = 10000;
float Re2 = 10000;
float Re1 = 1000;
int Value = 0;

float Units;

int oledDisplay = 38;
#define BUTTON_PIN 39

const uint8_t DAWASA_LOGO[] PROGMEM = {
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xc0, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0x7c, 0xfb, 0xff, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xfe, 0x0f, 0xe6, 0xec, 0xdb, 0xdf, 0xf0, 0x7f, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xf0, 0x7e, 0xe6, 0xec, 0x35, 0xdb, 0x3e, 0x1f, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xc3, 0xf4, 0xe2, 0x6d, 0xb1, 0xda, 0xf7, 0x83, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0x0f, 0xea, 0x20, 0x6d, 0x8d, 0xb6, 0x63, 0xf0, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xfc, 0x3c, 0xe5, 0x8d, 0xff, 0xff, 0xc7, 0x46, 0x3c, 0x3f, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xf8, 0xee, 0x75, 0x3f, 0xff, 0xff, 0xfc, 0x04, 0xef, 0x1f, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xe3, 0xe6, 0x13, 0xf8, 0x00, 0x00, 0x1f, 0xf0, 0xdd, 0xc7, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0x8f, 0x32, 0x6f, 0x80, 0x7f, 0xfe, 0x01, 0xf3, 0xbb, 0xe3, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0x1f, 0x84, 0x7c, 0x0f, 0xff, 0xff, 0xf0, 0x3f, 0x74, 0xf8, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xfe, 0x3f, 0xd3, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x07, 0xe4, 0xbc, 0x7f, 0xff, 0xff,
  0xff, 0xff, 0xfc, 0xf1, 0xef, 0x87, 0xff, 0xff, 0xff, 0xff, 0xe1, 0xec, 0xe7, 0x3f, 0xff, 0xff,
  0xff, 0xff, 0xf1, 0xda, 0x7e, 0x1f, 0xff, 0xff, 0xf3, 0x83, 0xf8, 0x79, 0x0f, 0x9f, 0xff, 0xff,
  0xff, 0xff, 0xe3, 0xed, 0xb8, 0x7f, 0xff, 0xff, 0x80, 0x0f, 0xfe, 0x1e, 0x1f, 0xc7, 0xff, 0xff,
  0xff, 0xff, 0xc7, 0x86, 0xf1, 0xff, 0xff, 0xff, 0x10, 0x07, 0xff, 0x8f, 0xbe, 0xe3, 0xff, 0xff,
  0xff, 0xff, 0x8f, 0xfb, 0xc7, 0x00, 0x00, 0x00, 0x10, 0x03, 0xff, 0xe3, 0xf0, 0xf1, 0xff, 0xff,
  0xff, 0xff, 0x1d, 0xff, 0x8f, 0x00, 0x00, 0x00, 0x00, 0x19, 0xff, 0xf1, 0xe9, 0x39, 0xff, 0xff,
  0xff, 0xff, 0x3c, 0x0e, 0x3f, 0x00, 0x01, 0xc0, 0x03, 0x0f, 0xff, 0xfc, 0xf0, 0x7c, 0xff, 0xff,
  0xff, 0xfe, 0x77, 0x3c, 0x7f, 0x00, 0x01, 0xc0, 0x19, 0x07, 0xff, 0xfe, 0x7d, 0xee, 0x7f, 0xff,
  0xff, 0xfc, 0xf3, 0xb8, 0xff, 0x00, 0x0f, 0xf8, 0x19, 0x37, 0xff, 0xff, 0x18, 0x0f, 0x3f, 0xff,
  0xff, 0xf8, 0xe0, 0x31, 0xff, 0x00, 0x1d, 0xfe, 0x1b, 0x3f, 0xff, 0xff, 0x9f, 0x3f, 0x1f, 0xff,
  0xff, 0xf9, 0xfc, 0xe3, 0xff, 0x00, 0x18, 0xfc, 0x5f, 0x3f, 0xff, 0xff, 0xcf, 0x79, 0x9f, 0xff,
  0xff, 0xf3, 0x80, 0xe7, 0xff, 0x00, 0x18, 0xf8, 0x1f, 0x3f, 0xff, 0xff, 0xe6, 0x01, 0xcf, 0xff,
  0xff, 0xe3, 0xc3, 0xcf, 0xff, 0x00, 0x18, 0xf0, 0x1f, 0x9f, 0xff, 0xff, 0xf3, 0x3e, 0xcf, 0xff,
  0xff, 0xe7, 0xf3, 0x9f, 0xff, 0x00, 0x38, 0xe0, 0x1f, 0x9f, 0xff, 0xff, 0xf1, 0xb1, 0xe7, 0xff,
  0xff, 0xc6, 0x79, 0x1f, 0xff, 0x00, 0x3c, 0xc0, 0x1f, 0xcf, 0xff, 0xff, 0xf9, 0xc4, 0x67, 0xff,
  0xff, 0xcf, 0x8b, 0x3f, 0xff, 0x00, 0x3c, 0xc0, 0x1f, 0xc7, 0xff, 0xff, 0xfc, 0xe9, 0xf3, 0xff,
  0xff, 0xcc, 0x66, 0x7f, 0xff, 0x00, 0x3e, 0x00, 0x1f, 0xe7, 0xff, 0xff, 0xfc, 0xf1, 0x73, 0xff,
  0xff, 0x9f, 0x1e, 0x7f, 0xff, 0x00, 0x00, 0x00, 0x1f, 0xe3, 0xff, 0xff, 0xfe, 0x60, 0xf9, 0xff,
  0xff, 0x9c, 0x0c, 0xff, 0xff, 0x00, 0x00, 0x80, 0x1f, 0xf3, 0xff, 0xff, 0xfe, 0x7b, 0x99, 0xff,
  0xff, 0x9e, 0x3c, 0xff, 0xff, 0x00, 0x00, 0xf0, 0x0f, 0xf1, 0xff, 0xff, 0xff, 0x36, 0xd9, 0xff,
  0xff, 0x3f, 0xcc, 0xff, 0xff, 0x03, 0x80, 0xfe, 0x1f, 0xf1, 0xff, 0xff, 0xff, 0x30, 0xf8, 0xff,
  0xff, 0x38, 0x39, 0xff, 0xff, 0x01, 0xc1, 0xff, 0x1f, 0xf0, 0x7f, 0xff, 0xff, 0x18, 0x1c, 0xff,
  0xff, 0x3e, 0x39, 0xff, 0xff, 0x07, 0x0f, 0xf9, 0x9e, 0x00, 0x00, 0x7f, 0xff, 0x98, 0x0c, 0xff,
  0xff, 0x3f, 0x99, 0xff, 0xff, 0x00, 0xf3, 0xce, 0x20, 0x00, 0x00, 0x7f, 0xff, 0x9e, 0x6c, 0xff,
  0xfe, 0x30, 0x79, 0xff, 0xff, 0x00, 0x1e, 0x38, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x9c, 0x9c, 0xff,
  0xfe, 0x7e, 0x39, 0xff, 0xff, 0x00, 0x01, 0xc0, 0x00, 0x00, 0x00, 0x7f, 0xff, 0x98, 0xfc, 0xff,
  0xfe, 0x70, 0x33, 0xff, 0xff, 0x00, 0x00, 0x1f, 0xc0, 0x00, 0x00, 0x7f, 0xff, 0x9e, 0x0c, 0xff,
  0xfe, 0x7f, 0xb3, 0xff, 0xff, 0x00, 0x03, 0xff, 0xc0, 0x00, 0x00, 0x7f, 0xff, 0x9c, 0x3e, 0x7f,
  0xfe, 0x7f, 0xf3, 0xff, 0xff, 0x00, 0x00, 0xfb, 0x80, 0x00, 0x00, 0x7f, 0xff, 0x9f, 0xfe, 0x7f,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0x80, 0x07, 0xfc, 0x07, 0xe0, 0xe0, 0x78, 0x3f, 0x03, 0xfe, 0x00, 0x3f, 0xe0, 0x7f, 0xff,
  0xff, 0x80, 0x01, 0xf8, 0x03, 0xe0, 0xe0, 0x30, 0x7e, 0x01, 0xfc, 0x18, 0x1f, 0xc0, 0x3f, 0xff,
  0xff, 0x83, 0xc0, 0xf0, 0x43, 0xf0, 0x40, 0x30, 0x7c, 0x00, 0xfc, 0x00, 0xff, 0x80, 0x1f, 0xff,
  0xff, 0x83, 0xe0, 0xf0, 0xc1, 0xf0, 0x02, 0x00, 0xf8, 0x30, 0x7e, 0x00, 0x1f, 0x06, 0x0f, 0xff,
  0xff, 0x83, 0xe0, 0xe0, 0x00, 0xf8, 0x06, 0x00, 0xf0, 0x00, 0x3f, 0xf8, 0x0e, 0x00, 0x07, 0xff,
  0xff, 0x80, 0x01, 0xc0, 0x00, 0x7c, 0x07, 0x01, 0xe0, 0x00, 0x18, 0x1c, 0x0e, 0x00, 0x03, 0xff,
  0xff, 0x80, 0x03, 0x83, 0xf0, 0x3c, 0x0f, 0x81, 0xc0, 0xfc, 0x0e, 0x00, 0x3c, 0x1f, 0x81, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xfc, 0xff, 0xfd, 0xff, 0xff, 0xff, 0xfd, 0xff, 0xdf, 0x77, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff,
  0xff, 0x57, 0xdb, 0xee, 0xd6, 0x2d, 0xed, 0xf2, 0x5e, 0x66, 0xd9, 0x5d, 0xb9, 0xe1, 0xac, 0xff,
  0xff, 0x46, 0x1d, 0x9a, 0x2a, 0x11, 0x0e, 0x4c, 0x86, 0x72, 0x23, 0x52, 0x59, 0x5a, 0xc9, 0x3f,
  0xf9, 0x06, 0xce, 0x00, 0x3a, 0x51, 0x7e, 0x04, 0x96, 0x38, 0x24, 0x52, 0x54, 0x26, 0x09, 0x3f,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xf6, 0xf7, 0xf7, 0xff, 0xfd, 0xff, 0xff, 0xff, 0x7f,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

#if (SH1106_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SH1106.h!");
#endif

void flow () // Interrupt function
{
  flow_frequency++;
}

void setup() {
 
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(oledDisplay, OUTPUT);
  digitalWrite(oledDisplay, HIGH);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  
  EEPROM.get(4, vol);
  EEPROM.get(0, Units);
  DynamicJsonBuffer jsonBuffer;

  delay(3000); // wait for console opening
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  }


  for (int j = 0; j < samples; j++)
  {
    xsample += analogRead(x);
    ysample += analogRead(y);
    zsample += analogRead(z);
  }

  xsample /= samples;
  ysample /= samples;
  zsample /= samples;
  delay(1000);

  pinMode(flowsensor, INPUT);
  digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
  attachInterrupt(digitalPinToInterrupt(flowsensor), flow, RISING); // Setup Interrupt
  //attachPinChangeInterrupt(digitalPinToInterrupt(flowsensor), flow, RISING); // interrupt connected to pin A0
  currentTime = millis();
  cloopTime = currentTime;
  

  display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  //display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)// Check your I2C address and enter it here, in Our case address is 0x3C
  display.clearDisplay();
  display.drawBitmap(0, 0, DAWASA_LOGO, 128, 64, WHITE); // display.drawBitmap(x position, y position, bitmap data, bitmap width, bitmap height, color)
  display.display();
  delay(4000);
  display.clearDisplay();
  display.display(); // this command will display all the data which is in buffer
  display.setTextColor(WHITE, BLACK);
  //display.setFont(&Monospaced_bold_10);
  //display.drawRect(117, 25, 3, 3, WHITE);     // Put degree symbol ( ° )
  draw_text(68, 0, "BAT =", 1);
  draw_text(120, 0, "V", 1);
  draw_text(0, 0, "L/M =", 1);
  draw_text(0, 30, "UNTS =", 1);
}

void loop() {
  
  DateTime now = rtc.now();
  /* Updates frequently */
  unsigned long currentTime = millis();

  byte buttonState = digitalRead(BUTTON_PIN);

  /* This is my event_1 */
  if ( currentTime - previousTime_1 >= eventTime_1_TIME) {

    display.setTextSize(1);
   // display.setFont(&Monospaced_bold_10);
    //display.setCursor(0, 0);
    //display.print(daysOfTheWeek[now.dayOfTheWeek()]);
    char currentDate [16];
    uint8_t thisDay, thisMonth ;
    thisDay = now.day();
    thisMonth = now.month();
    sprintf (currentDate, "%02d/%02d/", thisDay, thisMonth); //add leading zeros to the day and month
    display.setTextSize(1);
    display.setCursor(62, 15);
    display.print(currentDate);
    display.setTextSize(1);
    display.setCursor(102, 15);
    display.print(now.year(), DEC);
    /*================Display Time================*/
    char buffer [16];
    uint8_t thisSec, thisMin, thisHour;
    thisSec = now.second();
    thisMin = now.minute();
    thisHour = now.hour();
    sprintf (buffer, "%02d:%02d:%02d", thisHour, thisMin, thisSec);
    display.setTextSize(1);
    display.setCursor(0, 15);
    display.print(buffer);

    /* Update the timing for the next event*/
    previousTime_1 = currentTime;
  }

  /* This is my event_2 */
  if ( currentTime - previousTime_2 >= eventTime_2_BAT) {

    Value = analogRead(analogInput);
    vout = Value * (5.0 / 1024.0) * 2;
    //vin = vout / (Re2 / (Re1 + Re2));
    //Percentage = map(vin, 7.4, 8.6, 0, 100);
    Percentage = vout;
    //display.setFont(&Monospaced_bold_10);
    display.setTextSize(1);
    display.setCursor(102, 0);
    display.print(Percentage, 0);
    //display.print(rtc.getTemperature());
    display.display();


    /* Update the timing for the next event*/
    previousTime_2 = currentTime;
  }

  if (currentTime >= (cloopTime + 1000))
  {
    
    cloopTime = currentTime; // Updates cloopTime
    if (flow_frequency != 0) {
      // Pulse frequency (Hz) = 4.8Q, Q is flow rate in L/min.
      l_minute = (flow_frequency / 4.8); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
     // display.setFont(&Monospaced_bold_10);
      display.setCursor(32, 0);
      display.print(l_minute);
      l_minute = l_minute / 60;
      display.setCursor(40, 30);
      vol = vol + l_minute;
      Units = vol / 1000.0;
      display.print(Units, 4);
      flow_frequency = 0; // Reset Counter
      EEPROM.put(4, vol);
      EEPROM.put(0, Units);
      display.display();
      
    }

    
    else {
      
      //display.setFont(&Monospaced_bold_10);
      display.setCursor(32, 0);
      display.print(flow_frequency);
      display.setCursor(40, 30);
      display.print(Units, 4);
      EEPROM.put(4, vol);
      EEPROM.put(0, Units);
      display.display();
      //delay(5000);
      digitalWrite(oledDisplay, LOW);
    }
  }

  if (buttonState == LOW) {
      
      digitalWrite(oledDisplay, HIGH);
      Adafruit_SH1106 display(OLED_RESET);
  }

  if (buttonState == HIGH) {
      digitalWrite(oledDisplay, LOW);
  }

  if ( currentTime - previousTime_3 >= eventTime_3_GPRS) {

    Send_Data();
    previousTime_3 = currentTime;
  }


  if ( currentTime - previousTime_4 >= eventTime_4_ACCELEROMETER) {

    Accelerometer();
    previousTime_4 = currentTime;
  }

  
  /*
    if ( currentTime - previousTime_5 >= eventTime_5_GPS) {


      //display.clearDisplay(); // clearing the display
      displayInfo();
      display.display();
      previousTime_5 = currentTime;
    }
  */

  while (Serial1.available()) { // check for gps data
    if (gps.encode(Serial1.read())) // encode gps data
    {
      gps.f_get_position(&lat, &lon); // get latitude and longitude
      //display.setFont(&Monospaced_bold_10);
      display.setCursor(2, 36);
      display.print(lat, 6);
      display.setCursor(74, 36);
      display.print(lon, 6);

    }

    /*else{
      
      display.setCursor(2, 26);
      display.print("*.******");
      display.setCursor(74, 26);
      display.print("*.******");
      }
     */
  }
}

void draw_text(byte x_pos, byte y_pos, char *text, byte text_size) {
  display.setCursor(x_pos, y_pos);
  display.setTextSize(text_size);
  display.print(text);
  display.display();
}

void ShowSerialData()
{
  DateTime now = rtc.now();
  while (Serial.available() != 0)
    Serial.write(Serial.read());
  delay(1000);

}


void Send_Data()
{

  DateTime now = rtc.now();

  if (Serial.available())

    DateTime now = rtc.now();
  Serial.write(Serial.read());
  Serial.println("AT");
  delay(1000);
  Serial.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
  delay(6000);
  ShowSerialData();
  Serial.println("AT+SAPBR=3,1,\"APN\",\"internet\"");//APN
  delay(6000);
  ShowSerialData();
  Serial.println("AT+SAPBR=1,1");
  delay(6000);
  ShowSerialData();
  Serial.println("AT+SAPBR=2,1");
  delay(6000);
  ShowSerialData();
  Serial.println("AT+HTTPINIT");
  delay(6000);
  ShowSerialData();
  Serial.println("AT+HTTPPARA=\"CID\",1");
  delay(6000);
  char deviceID[12] = "METER001";
  ShowSerialData();
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& object = jsonBuffer.createObject();
  object.set("deviceID", deviceID);
  //object.set("map_link",http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=lat+lon);
  object.set("batteryStatus",Percentage);
  object.set("Units", Units);
  object.set("Latitude", lat, 6);
  object.set("Longitude", lon, 6);
  object.printTo(Serial);
  Serial.println(" ");
  String sendtoserver;
  object.prettyPrintTo(sendtoserver);
  delay(4000);
  Serial.println("AT+HTTPPARA=\"URL\",\"http://mis.dawasa.go.tz/amr_system/data.php\""); //Server address
  delay(4000);
  ShowSerialData();
  Serial.println("AT+HTTPPARA=\"CONTENT\",\"application/json\"");
  delay(4000);
  ShowSerialData();
  Serial.println("AT+HTTPDATA=" + String(sendtoserver.length()) + ",100000");
  Serial.println(sendtoserver);
  delay(6000);
  ShowSerialData();
  Serial.println(sendtoserver);
  delay(6000);
  ShowSerialData;
  Serial.println("AT+HTTPACTION=1");
  delay(6000);
  ShowSerialData();
  Serial.println("AT+HTTPREAD");
  delay(6000);
  ShowSerialData();
  Serial.println("AT+HTTPTERM");
  delay(10000);
  ShowSerialData;
  Serial.println("AT+SAPBR=0,1"); // Close GPRS context
  delay(300);
  ShowSerialData;

  Serial.println("");
  delay(300);

}

void Accelerometer() {
  int value1 = analogRead(x);
  int value2 = analogRead(y);
  int value3 = analogRead(z);

  int xValue = xsample - value1;
  int yValue = ysample - value2;
  int zValue = zsample - value3;

  //condition for tampering detection
  if (xValue < minVal || xValue > MaxVal  || yValue < minVal || yValue > MaxVal  || zValue < minVal || zValue > MaxVal)
  {
    digitalWrite(Buzzer, HIGH);
    delay(200);
    digitalWrite(Buzzer, LOW);
    //GPSMessage();
  }
}

/*
  void displayInfo()
  {

  while (Serial1.available()) { // check for gps data
    if (gps.encode(Serial1.read())) // encode gps data
    {
      gps.f_get_position(&lat, &lon); // get latitude and longitude
      display.setCursor(2, 26);
      display.print(lat,6);
      display.setCursor(74, 26);
      display.print(lon,6);
    }
  }
  }
*/

Welcome to the forum

Please post a schematic of your project showing how the components are connected. A 'photo of a hand drawn circuit is good enough

i connected SDA of oled to SDA pin of arduino mega also i connected SCL of oled to SCL pin of arduino mega then i connected switch button to digital pin 39 of arduino mega

What about the power and GND connections to the Arduino and the OLED ?

I simple diagram will make it clearer than any amount of written descriptions

Why are you trying to power the display in this unconventional way ?

I want save power of the battery, i want oled display to be on when only there is water flowing through the flow sensor and when the button switch pressed otherwise the oled display should be off.

An Arduino GPIO pin is not really designed to provide power so the amount of current that you can safely source is limited

Please post a link to the OLED display that you are using.

am using this oled display...

According to the specification that OLED takes between 28.5 and 33 mA when running. That really is too much for a GPIO pin on a Mega to supply safely so you need to consider using a MOSFET controlled by an Arduino pin to turn the display on and power the display from a suitable current source

And it should be mentioned that if you do propose to shut off the power to the OLED display - rather than simply blanking it; what is wrong with that? - you need to turn off the I²C pull-ups.

how can i do that?

Then how can i make the whole system go to sleep when no flow of water and when the switch button not pressed? i want only the system to to be on when there flow of water through the water flow sensor and when the switch button pressed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.