#include <EEPROM.h>
#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define WHITE 1
#define BLACK 0
//----------------------------------- Functions
  void barrel(int bar);
  void Buzzer();
  void calibration();
  void Color_Sensor();
  void logo_display();
  void cl_logo(int cl_logo_x, int cl_logo_y);
  void ph_logo(int ph_logo_x, int ph_logo_y);
  void o3_logo(int o3_logo_x, int o3_logo_y);
  void Menu();
  void ready_to_use();
//------------------------------------------------ Color Sensor
#define S0 4
#define S1 5
#define S2 2
#define S3 3
#define sensorOut 7
#define sensorLED 6
#define arrey_width 20
int number_of_samples=20;
int R_frequency[arrey_width];
int G_frequency[arrey_width];
int B_frequency[arrey_width];
int C_frequency[arrey_width];
// float red_ppm, green_ppm, blue_ppm, ppm;
int red_color=0, green_color=0, blue_color=0, clear_color=0;
// float g0, b0;
bool calib;
// float g_slope, b_slope;
#define green_eeprom_location 0
#define blue_eeprom_location 1
int status;   //------ 1=Cl, 2=pH, 3=O3
#define status_eeprom_location 2
//------------------------------------------------ Buzzer
#define buzzer_pin A2
//------------------------------------------------ Keypad
const byte ROWS = 3; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'c','p','o'},
  {'d','M','u'},
  {' ','b','R'}
};
byte rowPins[ROWS] = {8, 9, 10}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {11, 12, 13}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
boolean power_on=true;
//------------------------------------------------ Bitmap
  // ' oks_logo', 60x60px
  const unsigned char epd_bitmap__oks_logo [] PROGMEM = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x06, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0x00, 0x06, 0x40, 0x00, 0x00, 
    0x00, 0x00, 0xe6, 0x00, 0x06, 0x70, 0x00, 0x00, 0x00, 0x01, 0xe6, 0x00, 0x06, 0x78, 0x00, 0x00, 
    0x00, 0x07, 0xe6, 0x00, 0x06, 0x7e, 0x00, 0x00, 0x00, 0x0f, 0xe6, 0x00, 0x06, 0x67, 0x00, 0x00, 
    0x00, 0x1c, 0x66, 0x00, 0x06, 0x63, 0x80, 0x00, 0x00, 0x38, 0x66, 0x00, 0x06, 0x61, 0xc0, 0x00, 
    0x00, 0x70, 0x66, 0x00, 0x06, 0x60, 0xe0, 0x00, 0x00, 0xe0, 0x66, 0x00, 0x06, 0x60, 0x70, 0x00, 
    0x01, 0xc0, 0x66, 0x00, 0x06, 0x60, 0x38, 0x00, 0x03, 0x80, 0x66, 0x00, 0x06, 0x60, 0x1c, 0x00, 
    0x03, 0x00, 0x66, 0x00, 0x06, 0x60, 0x0c, 0x00, 0x07, 0x00, 0x66, 0x00, 0x06, 0x60, 0x06, 0x00, 
    0x0e, 0x00, 0x66, 0x00, 0x06, 0x60, 0x07, 0x00, 0x0c, 0x00, 0x66, 0x00, 0x06, 0x60, 0x03, 0x00, 
    0x1c, 0x00, 0x66, 0x00, 0x06, 0x60, 0x03, 0x80, 0x18, 0x00, 0x66, 0x00, 0x0c, 0x60, 0x00, 0x00, 
    0x18, 0x00, 0x66, 0x00, 0x18, 0x60, 0x00, 0x00, 0x30, 0x00, 0x66, 0x00, 0x30, 0x60, 0x00, 0x00, 
    0x30, 0x00, 0x66, 0x00, 0x60, 0x60, 0x00, 0x00, 0x30, 0x00, 0x66, 0x00, 0xc0, 0x60, 0x00, 0x00, 
    0x20, 0x00, 0x66, 0x01, 0x80, 0x60, 0x00, 0x00, 0x60, 0x00, 0x66, 0x03, 0x00, 0x60, 0x00, 0x00, 
    0x60, 0x00, 0x67, 0xfe, 0x00, 0x7f, 0xff, 0xe0, 0x60, 0x00, 0x67, 0xff, 0x00, 0x7f, 0xff, 0xe0, 
    0x60, 0x00, 0x66, 0x01, 0x80, 0x00, 0x00, 0x60, 0x60, 0x00, 0x66, 0x00, 0xc0, 0x00, 0x00, 0x60, 
    0x60, 0x00, 0x66, 0x00, 0x60, 0x00, 0x00, 0x60, 0x60, 0x00, 0x66, 0x00, 0x30, 0x00, 0x00, 0x60, 
    0x60, 0x00, 0x66, 0x00, 0x18, 0x00, 0x00, 0x60, 0x60, 0x00, 0x66, 0x00, 0x0c, 0x00, 0x00, 0x60, 
    0x60, 0x00, 0x66, 0x00, 0x06, 0x60, 0x00, 0x60, 0x20, 0x00, 0x66, 0x00, 0x06, 0x60, 0x00, 0xc0, 
    0x30, 0x00, 0x66, 0x00, 0x06, 0x60, 0x00, 0xc0, 0x30, 0x00, 0x66, 0x00, 0x06, 0x60, 0x00, 0xc0, 
    0x30, 0x00, 0x66, 0x00, 0x06, 0x60, 0x01, 0xc0, 0x18, 0x00, 0x66, 0x00, 0x06, 0x60, 0x01, 0x80, 
    0x18, 0x00, 0x66, 0x00, 0x06, 0x60, 0x01, 0x80, 0x1c, 0x00, 0x66, 0x00, 0x06, 0x60, 0x03, 0x80, 
    0x0c, 0x00, 0x66, 0x00, 0x06, 0x60, 0x03, 0x00, 0x0e, 0x00, 0x66, 0x00, 0x06, 0x60, 0x07, 0x00, 
    0x07, 0x00, 0x66, 0x00, 0x06, 0x60, 0x06, 0x00, 0x07, 0x00, 0x66, 0x00, 0x06, 0x60, 0x0c, 0x00, 
    0x03, 0x80, 0x66, 0x00, 0x06, 0x60, 0x1c, 0x00, 0x01, 0xc0, 0x66, 0x00, 0x06, 0x60, 0x38, 0x00, 
    0x00, 0xe0, 0x66, 0x00, 0x06, 0x60, 0x70, 0x00, 0x00, 0x70, 0x66, 0x00, 0x06, 0x60, 0xe0, 0x00, 
    0x00, 0x38, 0x66, 0x00, 0x06, 0x61, 0xc0, 0x00, 0x00, 0x1c, 0x66, 0x00, 0x06, 0x63, 0x80, 0x00, 
    0x00, 0x0f, 0xe6, 0x00, 0x06, 0x6f, 0x00, 0x00, 0x00, 0x07, 0xe6, 0x00, 0x06, 0x7e, 0x00, 0x00, 
    0x00, 0x01, 0xe6, 0x00, 0x06, 0x78, 0x00, 0x00, 0x00, 0x00, 0xe6, 0x00, 0x06, 0x70, 0x00, 0x00, 
    0x00, 0x00, 0x26, 0x00, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x06, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  };
  // ' icon_buzzer', 16x16px
  const unsigned char epd_bitmap__icon_buzzer [] PROGMEM = {
    0x00, 0x40, 0x00, 0xc0, 0x01, 0x40, 0x02, 0x40, 0x04, 0x44, 0x78, 0x42, 0x40, 0x52, 0x40, 0x4a, 
    0x40, 0x4a, 0x40, 0x4a, 0x78, 0x52, 0x04, 0x42, 0x02, 0x44, 0x01, 0x40, 0x00, 0xc0, 0x00, 0x40
  };
  // ' icon_calibration', 16x16px
  const unsigned char epd_bitmap__icon_calibration [] PROGMEM = {
    0x01, 0x00, 0x07, 0xe0, 0x09, 0x10, 0x13, 0xc8, 0x25, 0x24, 0x49, 0x12, 0x50, 0x0a, 0x51, 0xbf, 
    0xfd, 0x8a, 0x50, 0x0a, 0x48, 0x92, 0x24, 0xa4, 0x13, 0xc8, 0x08, 0x90, 0x07, 0xe0, 0x00, 0x80
  };
  // ' icon_date_and_time', 16x16px
  const unsigned char epd_bitmap__icon_date_and_time [] PROGMEM = {
    0x00, 0x00, 0x07, 0xe0, 0x09, 0x10, 0x10, 0x08, 0x21, 0x04, 0x41, 0x02, 0x41, 0x02, 0x41, 0x06, 
    0x60, 0x82, 0x40, 0x42, 0x40, 0x22, 0x20, 0x04, 0x10, 0x08, 0x08, 0x90, 0x07, 0xe0, 0x00, 0x00
  };
  // ' icon_locations', 16x16px
  const unsigned char epd_bitmap__icon_locations [] PROGMEM = {
    0x00, 0x00, 0x03, 0xc0, 0x04, 0x20, 0x08, 0x10, 0x09, 0x90, 0x09, 0x90, 0x08, 0x10, 0x08, 0x10, 
    0x04, 0x20, 0x02, 0x40, 0x02, 0x40, 0x01, 0x80, 0x05, 0xa0, 0x08, 0x10, 0x07, 0xe0, 0x00, 0x00
  };
  // ' icon_memory', 16x16px
  const unsigned char epd_bitmap__icon_memory [] PROGMEM = {
    0x00, 0x00, 0x7f, 0xf0, 0x40, 0x08, 0x49, 0x24, 0x49, 0x22, 0x49, 0x22, 0x40, 0x02, 0x40, 0x02, 
    0x4f, 0xf2, 0x48, 0x12, 0x48, 0x12, 0x48, 0x12, 0x4f, 0xf2, 0x40, 0x02, 0x7f, 0xfe, 0x00, 0x00
  };
  // ' scrollbar_background ', 8x64px
  const unsigned char epd_bitmap__scrollbar_background [] PROGMEM = {
    0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 
    0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 
    0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 
    0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00
  };
  // ' selected_outline', 128x21px
  const unsigned char epd_bitmap__selected_outline [] PROGMEM = {
    0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 
    0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 
    0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 
    0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0
  };
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 240)
const unsigned char* bitmap_icons[5] = {
	epd_bitmap__icon_locations,
  epd_bitmap__icon_calibration,
  epd_bitmap__icon_memory,
  epd_bitmap__icon_date_and_time,
  epd_bitmap__icon_buzzer	
};
const int NUM_ITEMS = 5;
char menu_item [NUM_ITEMS][20]{
  {"Locations"},
  {"Calibration"},
  {"Saved tests"},
  {"Date and time"},
  {"Buzzer"}
};
int item_selected;
int item_sel_previous;
int item_sel_next;

//------------------------------------------------------ Setup
void setup() {
//------------------------------------------------------ color sensor installation
 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
  pinMode(sensorLED, OUTPUT);
  pinMode(buzzer_pin, OUTPUT);  //buzzer

  digitalWrite(S0,LOW);
  digitalWrite(S1,LOW);// Power down color sensor

  status=2;//EEPROM.read(status_eeprom_location);
  delay(250); // wait for the OLED to power up
  //Buzzer();
  display.setTextColor(WHITE);
  display.display();
  display.clearDisplay();
  logo_display();
  ready_to_use();
}


//------------------------------------------------------ Main loop
void loop() {
  char key = keypad.getKey();
  if(key){
    switch (key) {
      case 'c':
          status=1;
          ready_to_use();
          break;
      case 'p':
          status=2;
          ready_to_use();
          break;
      case 'o':
          status=3;
          ready_to_use();
          break;
      case 'M':
          Menu();
          break;
      case 'u':
          for(int i=0; i<2; i++) Buzzer();
          delay(50);
          for(int i=0; i<3; i++) Buzzer();
          break;
      case 'd':
          for(int i=0; i<2; i++) Buzzer();
          delay(50);
          for(int i=0; i<3; i++) Buzzer();
          break;
      case 'R':
          Color_Sensor();
          break;
      case 'b':
            for(int i=0; i<4; i++) Buzzer();
            delay(50);
            //u8g.sleepOff();
            for(int i=0; i<2; i++) Buzzer();
          break;
    }
   }
}

void barrel(int bar){
  if(bar<128){
    display.drawLine(bar,40,bar,46,WHITE);
  }
  if(bar==30){
    display.setCursor(80,30); 
    display.print(".");
  }
  if(bar==60){
    display.setCursor(90,30); 
    display.print(".");
  }
  if(bar==90){
    display.setCursor(100,30); 
    display.print(".");
  }
  display.display();
  delay(3);
}
void Buzzer(){
    digitalWrite(buzzer_pin, HIGH);
    delay(27);                      //************** Set Timer...!!!
    digitalWrite(buzzer_pin, LOW);
}

void calibration(){
  calib= true;
  Color_Sensor();
  // for(int i=0; i<EEPROM.length(); i++){
  //   EEPROM.write(i,0);
  // }
  // EEPROM.write(green_eeprom_location,g0);
  // EEPROM.write(blue_eeprom_location,b0);
  calib= false;
}


/*
display.invertDisplay(true);
  delay(1000);
  display.invertDisplay(false);
  delay(1000);
*/

void Color_Sensor() {
  int y_cursor=56, i=0, n=0, blink=0, bar=0;
  int cl_logo_x=0, cl_logo_y=0;
  int ph_logo_x=11, ph_logo_y=0;
  int o3_logo_x=4, o3_logo_y=0;
  char buf[6];
  green_color=0;
  red_color=0;
  blue_color=0;
  clear_color=0;
      display.clearDisplay();
      display.setTextSize(1);
      display.drawRect(0,40,128,8,1);
      display.setCursor(35,30); 
      display.print("Reading");
      display.display();
      Buzzer();

  digitalWrite(sensorLED,HIGH);
  delay(10);
  // Setting frequency-scaling LL: Power down, LH: 2%, HL: 20%, HH:100%
  digitalWrite(S0,LOW);
  digitalWrite(S1,HIGH);//

    //----------------------------------------------- Red
    digitalWrite(S2,LOW);
    digitalWrite(S3,LOW);
    delay(10);
    // Reading the output frequency
    for(i=0; i<number_of_samples; i++){
    R_frequency[i] = pulseIn(sensorOut, digitalRead(sensorOut) == HIGH ? LOW : HIGH);
    bar++;
    barrel(bar);
    bar++;
    barrel(bar);
    }

    //----------------------------------------------- Green
    digitalWrite(S2,HIGH);
    digitalWrite(S3,HIGH);
    delay(10);
    // Reading the output frequency
    for(i=0; i<number_of_samples; i++){
    G_frequency[i] = pulseIn(sensorOut, digitalRead(sensorOut) == HIGH ? LOW : HIGH);
    bar++;
    barrel(bar);
    bar++;
    barrel(bar);
    }

    //----------------------------------------------- Blue
    digitalWrite(S2,LOW);
    digitalWrite(S3,HIGH);
    delay(10);
    // Reading the output frequency
    for(i=0; i<number_of_samples; i++){
    B_frequency[i] = pulseIn(sensorOut, digitalRead(sensorOut) == HIGH ? LOW : HIGH);
    bar++;
    barrel(bar);
    bar++;
    barrel(bar);
    }
    //----------------------------------------------- Clear
    digitalWrite(S2,HIGH);
    digitalWrite(S3,LOW);
    delay(10);
    // Reading the output frequency
    for(i=0; i<number_of_samples; i++){
    C_frequency[i] = pulseIn(sensorOut, digitalRead(sensorOut) == HIGH ? LOW : HIGH);
    bar++;
    barrel(bar);
    bar++;
    barrel(bar);
    }
  while(bar<128){
    bar++;
    barrel(bar);
  }

  digitalWrite(sensorLED,LOW);
  
    for(n=0; n<number_of_samples; n++){
      red_color= red_color+R_frequency[n];
      green_color= green_color+G_frequency[n];
      blue_color= blue_color+B_frequency[n];
      clear_color= clear_color+C_frequency[n];
    }
    red_color=red_color/number_of_samples;
    green_color=green_color/number_of_samples;
    blue_color=blue_color/number_of_samples;
    clear_color=clear_color/number_of_samples;

      if(calib){
        // g0=green_color;
        // b0=blue_color;
          for(blink=0; blink<3; blink++)
          {
            display.clearDisplay();
            display.setTextSize(2);
            display.setCursor(5,18);
            display.print("Calibrated");
            display.display();
            delay(300);
            display.clearDisplay();
            display.display();
            delay(150);
            display.setCursor(5,18);
            display.print("Calibrated");
            display.display();
          }
      }
// if(red_color<25 && green_color<25 && blue_color<25) // no sample!
//   {
//     for(blink=0; blink<3; blink++)
//     {
//       display.clearDisplay();
//       display.setTextSize(2);
//       display.setCursor(5,18);
//       display.print("No Sample!");
//       display.display();
//       delay(400);
//       display.clearDisplay();
//       display.display();
//       delay(200);
//       display.setCursor(5,18);
//       display.print("No Sample!");
//       display.display();
//     }
//     display.setTextSize(1);
//   }
// else
//   {
    // g_slope= 0.0097*g0-0.2374;
    // green_ppm= g_slope*(green_color-g0);
    // b_slope= 0.8215-0.026*b0;
    // blue_ppm= b_slope*(blue_color-b0);

    // if(green_ppm<=0.5){
    //   ppm= green_ppm;
    // }
    // if(green_ppm>0.5 && green_ppm<=0.75){
    //   ppm= 1.64*green_ppm-0.3129;
    // }
    // if(green_ppm>0.75 && green_ppm<=1.3){
    //   ppm= 2.5641*green_ppm-1.0031;
    // }
    // if(green_ppm>1.3){
    //   ppm= (green_ppm+blue_ppm)/2;
    // }

    //   if(ppm<0){
    //     ppm=0;
    //   }

      display.clearDisplay();
      display.setTextSize(1);
      if(status==1){
        display.setCursor(0,2);
        display.println(F("CHLORINE level is"));
        cl_logo(106, -3);
        display.display();
      }
      if(status==2){
        display.setCursor(0,2);
        display.println(F("pH level is"));
        ph_logo(83, -1);
        display.display();
      }
      if(status==3){
        display.setCursor(0,2);
        display.println(F("OZONE level is"));
        o3_logo(92, -2);
        display.display();
      }
          display.setCursor(0,32); 
          display.print("r_");
          display.print(red_color,1);
          display.setCursor(0,44);
          display.print("g_");
          display.print(green_color,1);
          display.setCursor(0,56);
          display.print("b_");
          display.print(blue_color,1);
          display.setCursor(0,20);
          display.print("c_");
          display.print(clear_color,1);

display.display();
//           display.setCursor(0,y_cursor+16); 
//           display.print(red_ppm,1);
//           display.setCursor(43,y_cursor+16); 
//           display.print(green_ppm,1);
//           display.setCursor(87,y_cursor+16); 
//           display.print(blue_ppm,1);
//           display.setCursor(87,y_cursor); 
//           display.print(clear_ppm,1);
// display.display();
//     // if(abs(blue_ppm-green_ppm)>4){     // out of range 
    //   display.setTextSize(2);
    //   display.setCursor(30,20);
    //   display.print("OUT OF");
    //   display.setCursor(30,38);
    //   display.print("RANGE!");
    // }
    // else{

  //     display.setCursor(0,22);
  //     //display.setTextSize(4);/////////////////////////////////////////////////////////////////////
  //     if(ppm<10)
  //       dtostrf(ppm,4,2,buf); // at least 4 chars, 2 numbers after point
  //     else
  //       dtostrf(ppm,4,1,buf);
  //     display.print(buf);
  //     display.display();
  // delay(200);


  //     if(status!=2){ 
  //       display.display();
  //       display.setTextSize(1);
  //       display.setCursor(109,25);
  //       display.print("ppm");
  //     }
  //   display.display();  
  digitalWrite(S0,LOW);
  digitalWrite(S1,LOW);// Power down color sensor
}


void logo_display(void) {
  display.clearDisplay();
  display.drawBitmap( 34, -2, epd_bitmap__oks_logo, 60, 60, 1);
  display.display();
  delay(600);
  for(int i=0; i<3; i++){
  display.fillCircle(50+i*14,62,1,WHITE);
  display.display();
  delay(400);}
}

void cl_logo(int cl_logo_x, int cl_logo_y){
  display.fillCircle(12+cl_logo_x,16+cl_logo_y,8,WHITE);
  display.fillTriangle(12+cl_logo_x,2+cl_logo_y,5+cl_logo_x,12+cl_logo_y,19+cl_logo_x,12+cl_logo_y,WHITE); 
  display.display();
}

void ph_logo(int ph_logo_x, int ph_logo_y){
  display.fillCircle(35+ph_logo_x,16+ph_logo_y,8,WHITE);
    display.drawRect(33+ph_logo_x,4+ph_logo_y,4,6,WHITE);
    display.fillRoundRect(31+ph_logo_x,1+ph_logo_y,8,3,90,WHITE);
    display.drawCircle(35+ph_logo_x,16+ph_logo_y,7,BLACK);
    display.drawPixel(35+ph_logo_x,8+ph_logo_y,BLACK);
    display.drawPixel(34+ph_logo_x,8+ph_logo_y,BLACK);
    display.drawFastHLine(33+ph_logo_x, 10+ph_logo_y, 5, BLACK);
    display.drawFastHLine(31+ph_logo_x, 11+ph_logo_y, 9, BLACK);
    display.drawFastHLine(30+ph_logo_x, 12+ph_logo_y, 11, BLACK);
    display.display();
}

void o3_logo(int o3_logo_x, int o3_logo_y){
  display.fillCircle(17+o3_logo_x,18+o3_logo_y,4,WHITE);
    display.fillCircle(24+o3_logo_x,6+o3_logo_y,4,WHITE);
    display.fillCircle(31+o3_logo_x,18+o3_logo_y,4,WHITE);
    display.drawLine(17+o3_logo_x,18+o3_logo_y,24+o3_logo_x,6+o3_logo_y,WHITE);
    display.drawLine(24+o3_logo_x,6+o3_logo_y,31+o3_logo_x,18+o3_logo_y,WHITE);
    display.display();
}

void Menu(){
menu_jump:
  // u8g.firstPage();
  // do {
  //   // previous item
  //   u8g.setFont(u8g_font_7x14);
  display.setCursor(26,15);
  display.print(menu_item[item_sel_previous]);
    //u8g.drawStr(26, 15, menu_item[item_sel_previous]);//////////////////////////////////
    //u8g.drawBitmapP( 4, 2, 16/8, 16, bitmap_icons[item_sel_previous]); 
    display.drawBitmap( 4, 2, bitmap_icons[item_sel_previous], 16, 16, 1);

  display.setCursor(26,37);
  display.print(menu_item[item_selected]);
    //selected item
    //u8g.setFont(u8g_font_7x14B);
    //u8g.drawStr(26, 37, menu_item[item_selected]);////////////////////////////////
    //u8g.drawBitmapP( 4, 24, 16/8, 16, bitmap_icons[item_selected]); // draw calibration icon
    display.drawBitmap( 4, 24, bitmap_icons[item_selected], 16, 16, 1);
    
  display.setCursor(26,59);
  display.print(menu_item[item_sel_next]);
    // next item
    //u8g.setFont(u8g_font_7x14);
    //u8g.drawStr(26, 59, menu_item[item_sel_next]);////////////////////////////////
    //u8g.drawBitmapP( 4, 46, 16/8, 16, bitmap_icons[item_sel_next]); // draw locations icon
    display.drawBitmap( 4, 46, bitmap_icons[item_sel_next], 16, 16, 1);  

    //u8g.drawBitmapP( 0, 22, 128/8, 21, epd_bitmap__selected_outline);
    display.drawBitmap( 0, 22, epd_bitmap__selected_outline, 128, 21, 1);
    //u8g.drawBitmapP( 120, 0, 8/8, 64, epd_bitmap__scrollbar_background);
  //} while ( u8g.nextPage() );
display.display();


    char key = keypad.getKey();

    if(key=='u'){
      Buzzer();
      item_selected= item_selected-1;
      if(item_selected<0){
        item_selected=NUM_ITEMS-1;
      }
    }
    if(key=='d'){
      Buzzer();
      item_selected= item_selected+1;
      if(item_selected>= NUM_ITEMS){
        item_selected=0;
      }
    }
    if(key=='b'){ready_to_use(); goto menu_down;}
    if(key=='c'){status=1; ready_to_use();  goto menu_down;}
    if(key=='p'){status=2; ready_to_use();  goto menu_down;}
    if(key=='o'){status=3; ready_to_use();  goto menu_down;}
    if(key=='M'){ 
    }

    item_sel_previous = item_selected-1;
    if(item_sel_previous < 0){ item_sel_previous = NUM_ITEMS-1;}
    item_sel_next = item_selected+1;
    if(item_sel_next >= NUM_ITEMS){ item_sel_next = 0;}
    goto menu_jump;
menu_down:
  delay(1);
}

void ready_to_use(){
  int cl_logo_x=0, cl_logo_y=0;
  int ph_logo_x=11, ph_logo_y=0;
  int o3_logo_x=4, o3_logo_y=0;
  display.clearDisplay();
  Buzzer();
  //display.setTextSize(2);//////////////////////////////////////////////////////

  if (status==1){
    cl_logo(cl_logo_x, cl_logo_y);
    display.setCursor(28, 8);
    display.print("CHLORINE");}
  if (status==2){
    ph_logo(ph_logo_x, ph_logo_y);
    display.setCursor(64, 8);
    display.print("pH");}
  if (status==3){
    o3_logo(o3_logo_x, o3_logo_y);
    display.setCursor(48, 8);
    display.print("OZONE");}

  display.setTextSize(1);
  display.setCursor(13, 37);
  display.println("Insert the sample");
  display.setCursor(26, 46);
  display.println("close the cap");
  display.setCursor(22, 55);
  display.println("and press READ");
  display.display();
  
  EEPROM.write(status_eeprom_location, status);
}
Loading
ssd1306
NOCOMNCVCCGNDINLED1PWRRelay Module