#define ENCODER_CLK 2
#define ENCODER_DT 3
void setup() {
Serial.begin(115200);
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
}
int lastClk = HIGH;
void loop() {
int newClk = digitalRead(ENCODER_CLK);
if (newClk != lastClk) {
// There was a change on the CLK pin
lastClk = newClk;
int dtValue = digitalRead(ENCODER_DT);
if (newClk == LOW && dtValue == HIGH) {
Serial.println("Rotated clockwise ⏩");
}
if (newClk == LOW && dtValue == LOW) {
Serial.println("Rotated counterclockwise ⏪");
}
}
}
/*
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_WIDTH 128 // OLED display width, in pixels
#define OLED_HEIGHT 64 // OLED display height, in pixels
#define vibOutPin1 9
#define vibOutPin2 10
//SSD1306 Plugs into A4 (SCA) and A5 (SCL)
// INPUT: Potentiometer should be connected to 5V and GND
int potPin1 = A1; // Potentiometer output connected to analog pin 3
int strength = 0; // Variable to store the input from the potentiometer
int potPin2 = A2; // Potentiometer output connected to analog pin 3
int length = 0; // Variable to store the input from the potentiometer
int potPin3 = A3; // Potentiometer output connected to analog pin 3
int span = 0; // Variable to store the input from the potentiometer
int potPin6 = A6; // Potentiometer output connected to analog pin 3
int count = 0; // Variable to store the input from the potentiometer
int countdown = 0; // Variable to store the input from the potentiometer
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);
void setup() {
pinMode(vibOutPin1, OUTPUT); // define a pin as output
pinMode(vibOutPin2, OUTPUT); // define a pin as output
Serial.begin(9600);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true)
;
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
oled.println("Dane's EMDR Tapper"); // text to display
oled.display(); // show on OLED
delay(500);
}
void loop() {
setPasses();
setCountdown();
for (int i = 0; i < count; i++) {
runTappers();
};
} // loop
void setPasses() {
updateCount();
oled.clearDisplay(); // clear display
oled.setCursor(0, 10); // position to display
oled.setTextSize(2); // text size
oled.println("Starting in 3"); // text to display
oled.setCursor(40, 50); // position to display
oled.println(count); // text to display
oled.display(); // show on OLED
delay(1000);
updateCount();
oled.clearDisplay(); // clear display
oled.setCursor(0, 10); // position to display
oled.println("Starting in 2"); // text to display
oled.setCursor(40, 50); // position to display
oled.println(count); // text to display
oled.display(); // show on OLED
delay(1000);
updateCount();
oled.clearDisplay(); // clear display
oled.setCursor(0, 10); // position to display
oled.println("Starting in 1"); // text to display
oled.setCursor(40, 50); // position to display
oled.println(count); // text to display
oled.display(); // show on OLED
delay(1000);
updateCount();
}
void updateDisplay() {
updateValues();
// print potVal
oled.clearDisplay(); // clear display
// oled.setTextSize(2); // text size
// oled.setTextColor(WHITE); // text color
oled.setCursor(0, 5); // position to display
oled.setTextSize(1); // text size
oled.println(String(strength * 100 / 255) + "%"); // text to display
oled.setCursor(55, 5); // position to display
oled.println(length); // text to display
oled.setCursor(100, 5); // position to display
oled.println(span); // text to display
oled.setCursor(60, 20); // position to display
oled.println(countdown); // text to display
animatedStrength();
oled.display(); // show on OLED
}
void runTappers() {
updateDisplay();
analogWrite(vibOutPin1, strength);
delay(length);
analogWrite(vibOutPin1, 0);
delay(span);
updateDisplay();
analogWrite(vibOutPin2, strength);
delay(length);
analogWrite(vibOutPin2, 0);
delay(span);
oled.clearDisplay(); // clear display
reduceCountdown();
}
void updateValues() {
strength = analogRead(potPin1); // read the potentiometer value at the input pin
strength = (strength) / 4.12; //normalize potVal
length = analogRead(potPin2); // read the potentiometer value at the input pin
length = (length); //normalize potVal
span = analogRead(potPin3); // read the potentiometer value at the input pin
span = (span); //normalize potVal
}
void updateCount() {
count = analogRead(potPin6); // read the potentiometer value at the input pin
if (count > 700) {
count = 9999;
} else {
count = int((count) / 50) * 5;
};
}
void setCountdown() {
countdown = (count);
}
void reduceCountdown() {
countdown = (countdown) - 1;
}
void animatedStrength() {
// draw rectangle
oled.fillRect((29 - (span) / 35), 64 - ((strength) / 6), 1 + ((length) / 33), 64, WHITE);
oled.fillRect((129 - (29 - (span) / 35) - ((length) / 33)), 64 - ((strength) / 6), 1 + ((length) / 33), 64, WHITE);
// oled.fillRect((128 - (30 - (span) / 35) - ((length) / 33)), 64 - ((strength) / 6), ((length) / 33), 63, WHITE);
drawCircle(64, 46, 15, BLACK);
drawPieSlice(64, 46, 15, WHITE, -90, -90 + (360 * ((countdown) - 1) / ((count) - 1)));
}
void drawPieSlice(int x, int y, int radius, int color, int startAngle, int EndAngle)
{
for (int i=startAngle; i<EndAngle; i++)
{
double radians = i * PI / 180;
double px = x + radius * cos(radians);
double py = y + radius * sin(radians);
oled.drawPixel(px, py, color);
}
}
void drawCircle(int x, int y, int radius, int color)
{
drawPieSlice(x, y, radius, color, 0, 360);
}
*/
Loading
ssd1306
ssd1306