-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestEncoder.ino
More file actions
168 lines (150 loc) · 3.74 KB
/
TestEncoder.ino
File metadata and controls
168 lines (150 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// encoder
#define encoderPinA 5 // encoder A channel
#define encoderPinB 6 // encoder B channel
#define encoderPinC 7 // encoder button
volatile long pos = 0;
unsigned long t1;
int stato = 0;
bool FIRST = true;
int eA = LOW;
int eB = LOW;
int UP, DN, CLIC;
int bt, pvbt;
void setup() {
Serial.begin(115200);
pinMode(encoderPinA, INPUT);
pinMode(encoderPinB, INPUT);
pinMode(encoderPinC, INPUT_PULLUP);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
}
void loop() {
doEncoder();
bt = !digitalRead(encoderPinC);
if ((millis() - t1) > 200) {
if (pos > 0) UP = 1;
else if (pos < 0) DN = 1;
pos = 0;
t1 = millis();
}
if (bt && !pvbt) {
CLIC = 1;
}
switch(stato) {
case 0:
menu0();
break;
case 1:
menu1();
break;
case 2:
menu2();
break;
case 3:
menu3();
break;
}
pvbt = bt;
CLIC = 0;
UP = 0;
DN = 0;
}
void doEncoder() {
eB = digitalRead(encoderPinB);
if (digitalRead(encoderPinA) != eA) {
eA = !eA;
// look for a low-to-high on channel A
if (eA == HIGH) {
// check channel B to see which way encoder is turning
if (eB == LOW) {
//pos = pos + 1; // CW
pos = 1;
} else {
//pos = pos - 1; // CCW
pos = -1;
}
} else { // look for a high-to-low on channel A
// check channel B to see which way encoder is turning
if (eB == HIGH) {
//pos = pos + 1; // CW
pos = 1;
} else {
//pos = pos - 1; // CCW
pos = -1;
}
}
}
}
void oledPrint(char *str) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
display.write(str);
display.display();
}
void stateGo(int st) {
FIRST = true;
stato = st;
}
void menu0() {
if (FIRST) {
Serial.println("menu 0");
oledPrint("menu 0");
FIRST = false;
}
if (UP) stateGo(1);
//if (DN) go(1);
if (CLIC) Serial.println("Selected 0");
}
void menu1() {
if (FIRST) {
Serial.println("menu 1");
oledPrint("menu 1");
FIRST = false;
}
if (UP) stateGo(2);
if (DN) stateGo(0);
if (CLIC) Serial.println("Selected 1");
}
void menu2() {
if (FIRST) {
Serial.println("menu 2");
oledPrint("menu 2");
FIRST = false;
}
if (UP) stateGo(3);
if (DN) stateGo(1);
if (CLIC) Serial.println("Selected 2");
}
void menu3() {
if (FIRST) {
Serial.println("menu 3");
oledPrint("menu 3");
FIRST = false;
}
//if (UP) stateGo(0);
if (DN) stateGo(2);
if (CLIC) Serial.println("Selected 3");
}