// Test for minimum program size.

#include <Wire.h>
#include <Servo.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"

// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C

// Define proper RST_PIN if required.
#define RST_PIN -1

SSD1306AsciiWire oled;
Servo s;
int lastClk = HIGH;
int rotation;
//------------------------------------------------------------------------------
void setup() {
  Wire.begin();
  Wire.setClock(400000L);

#if RST_PIN >= 0
  oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
  oled.begin(&Adafruit128x64, I2C_ADDRESS);
#endif // RST_PIN >= 0

  oled.setFont(System5x7);
  oled.clear();
  oled.print("Hello world!");
  s.attach(5);
  s.write(0);
}
//------------------------------------------------------------------------------
void loop() {
  int newClk = digitalRead(2);

  oled.println(rotation);

  if (newClk != lastClk) {
    // There was a change on the CLK pin
    lastClk = newClk;
    int dtValue = digitalRead(3);
    
    if (newClk == LOW && dtValue == HIGH) {
      rotation += 5;
    }
    else if (newClk == LOW && dtValue == LOW) {
      rotation -= 5;
    }

    if (rotation > 180)
    {
      rotation = 0;
    }
    else if (rotation < 0)
    {
      rotation = 180;
    }

    s.write(rotation);
  }
}