#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SDA 2
#define SCL 3
#define MODE 5
#define DEBOUNCE_TIME 200

#define I2C_ADDRESS 0x3C

Adafruit_SSD1306 oled(128, 64);

void modeISR();

volatile int mode = 0;
volatile bool modeChanged = false;
volatile unsigned long prev_press = millis();

//----------------------------------------------------------------------------------------------------------------------
void setup()
{
  Serial.begin(57600);

  pinMode(8, INPUT_PULLUP);
  pinMode(MODE, INPUT_PULLUP);
  attachInterrupt(MODE, modeISR, RISING);

  Wire.begin(SDA, SCL);
  oled.begin(SSD1306_SWITCHCAPVCC, I2C_ADDRESS);

  oled.clearDisplay();
  oled.setTextSize(1);
  oled.setTextColor(SSD1306_WHITE);

  oled.setCursor(28, 4);
  oled.println("Digital Scale");
  oled.display();
  delay(1000);
  oled.clearDisplay();
  oled.display();
  display();
}
//----------------------------------------------------------------------------------------------------------------------
void loop()
{
  if (digitalRead(8) == LOW)
    Serial.print('~');
  
  if (modeChanged) {
    modeChanged = false;
    Serial.print(mode);
  }

  delay(100);
}

void display()
{
  Serial.print("Hello World");
}

void modeISR()
{
  if (millis() - prev_press < DEBOUNCE_TIME)
    return;
  prev_press = millis();
  mode++;
  modeChanged = true;
}