#include <SPI.h>
#include <Wire.h>
// #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "Dice.h"
#define CLK 3
#define DT 4
#define SW 5
int counter = 0;
int currentStateCLK;
int lastStateCLK;
unsigned long lastButtonPress = 0;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup()
{
// Serial.begin(9600);
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // here the 0x3c is the I2C address, check your i2c address if u have multiple devices.
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
lastStateCLK = digitalRead(CLK);
}
void loop()
{
currentStateCLK = digitalRead(CLK);
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
if (digitalRead(DT) != currentStateCLK) { // Encoder is rotating CW so increment
counter ++;
if (counter > 3) counter = 0;
} else { // Encoder is rotating CCW so increment
counter --;
if (counter < 0) counter = 3;
}
display.clearDisplay();
display.setCursor(24, 24);
display.print("State ");
display.println(counter);
display.display();
}
lastStateCLK = currentStateCLK;
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (digitalRead(SW) == LOW) {
for ( int x = 0; x < 8; x++ ) {
display.clearDisplay();
display.drawBitmap(16, 16, dice_bitmap[random(6)], 32, 32, WHITE);
display.drawBitmap(80, 16, dice_bitmap[random(6)], 32, 32, WHITE);
display.display();
delay(100);
}
}
}