#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Rotary Encoder Inputs
#define CLK 2
#define DT 3
#define SW 4
#define BT 5
int counter = 0;
int currentStateCLK;
int lastStateCLK;
int selector = 0;
int drop = 50;
int Delay = 200;
int drop2 = 50;
int cameraDelay = 200;
String currentDir ="";
unsigned long lastButtonPress = 0;
unsigned long lastTriggerPress = 0;
#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)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
// Set encoder pins as inputs
pinMode(CLK,INPUT);
pinMode(DT,INPUT);
pinMode(SW, INPUT_PULLUP);
pinMode(BT, INPUT_PULLUP);
// Setup Serial Monitor
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
}
void loop() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.print("Drop 1:");
display.setCursor(85, 10);
display.print(drop);
display.setCursor(0, 20);
display.print("Delay 2:");
display.setCursor(85, 20);
display.print(Delay);
display.setCursor(0, 30);
display.print("Drop 2:");
display.setCursor(85, 30);
display.print(drop2);
display.setCursor(0, 40);
display.print("Camera Delay:");
display.setCursor(85, 40);
display.print(cameraDelay);
display.display();
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
counter --;
currentDir ="CCW";
} else {
// Encoder is rotating CW so increment
counter ++;
currentDir ="CW";
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
// Read the button state
int btnState = digitalRead(SW);
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
}
// Remember last button press event
lastButtonPress = millis();
}
// Put in a slight delay to help debounce the reading
// Remember last CLK state
lastStateCLK = currentStateCLK;
// Read the button state
int TrnState = digitalRead(BT);
//If we detect LOW signal, button is pressed
if (TrnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastTriggerPress > 50) {
Serial.println("Trigger pressed!");
}
// Remember last button press event
lastTriggerPress = millis();
}
// Put in a slight delay to help debounce the reading
delay(1);
}