#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h> // Library for SSC1306
#include <Adafruit_SSD1306.h> // Library for SSC1306


const int SCREEN_WIDTH = 128; // OLED display width, in pixels
const int SCREEN_HEIGHT = 64; // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
//Adafruit_SSD1306 (uint8_t w, uint8_t h, TwoWire *twi=&Wire, int8_t rst_pin=-1, uint32_t clkDuring=400000UL, uint32_t clkAfter=100000UL)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define UpBtn 18
#define SelBtn 17
#define DownBtn 16
#define led 15

int state = 0;
void setup() {
  pinMode(UpBtn, INPUT);
  pinMode(DownBtn, INPUT);
  pinMode(SelBtn, INPUT);
  pinMode(led, OUTPUT);
   Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    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.
  delay(2000); // Pause for 2 seconds

  display.clearDisplay();

  display.setTextSize(1);
  display.setCursor(20, 0);
  display.setTextColor(WHITE);
  display.print("LED [Waiting]");
  display.setCursor(35, 10);
  display.print("1.blink 1 S");
  display.setCursor(35, 20);
  display.print("2.blink 2 S");
  display.setCursor(35, 30);
  display.print("3.blink 3 S");
  display.setCursor(0, 0);
  display.print("I>");
  display.display();
  
}

void loop() {

  if(digitalRead(DownBtn)==1){
    state = state + 1;
    if(state == 1){
      Serial.print("State :"); Serial.println(state); 
      display.setTextColor(BLACK);
      display.setCursor(0, 0);
      display.print("I>");
      display.display();
      display.setTextColor(WHITE);
      display.setCursor(0, 10);
      display.print("I>");
      display.display();
    }
    else if(state == 2){
      Serial.print("State :"); Serial.println(state); 
      display.setTextColor(BLACK);
      display.setCursor(0, 10);
      display.print("I>");
      display.display();
      display.setTextColor(WHITE);
      display.setCursor(0, 20);
      display.print("I>");
      display.display();
    }
    else if(state == 3){
      Serial.print("State :"); Serial.println(state); 
      display.setTextColor(BLACK);
      display.setCursor(0, 20);
      display.print("I>");
      display.display();
      display.setTextColor(WHITE);
      display.setCursor(0, 30);
      display.print("I>");
      display.display();
    }

  }
  
  if(digitalRead(UpBtn)==1){
    state = state - 1;
    if(state == 1){
      Serial.print("State :"); Serial.println(state); 
      display.setTextColor(BLACK);
      display.setCursor(0, 0);
      display.print("I>");
      display.display();
      display.setTextColor(WHITE);
      display.setCursor(0, 10);
      display.print("I>");
      display.display();
    }
    else if(state == 2){
      Serial.print("State :"); Serial.println(state); 
      display.setTextColor(BLACK);
      display.setCursor(0, 10);
      display.print("I>");
      display.display();
      display.setTextColor(WHITE);
      display.setCursor(0, 20);
      display.print("I>");
      display.display();
    }
    else if(state == 3){
      Serial.print("State :"); Serial.println(state); 
      display.setTextColor(BLACK);
      display.setCursor(0, 20);
      display.print("I>");
      display.display();
      display.setTextColor(WHITE);
      display.setCursor(0, 30);
      display.print("I>");
      display.display();
    }

  }

  if(digitalRead(SelBtn==1)){
    if(state == 1){
      Serial.print("State :"); Serial.print(state); Serial.println("1 sec");
      digitalWrite(led, 1);
      delay(1000);
      digitalWrite(led, 0);
    }
    else if(state == 2){
      Serial.print("State :"); Serial.print(state); Serial.println("2 sec");
      digitalWrite(led, 1);
      delay(2000);
      digitalWrite(led, 0);
    }
    else if(state == 3){
      Serial.print("State :"); Serial.print(state); Serial.println("3 sec");
      digitalWrite(led, 1);
      delay(3000);
      digitalWrite(led, 0);
    }
  }
 
}