-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathESP8266RotaryEncoderInterruptsSerial.ino
82 lines (65 loc) · 3.89 KB
/
ESP8266RotaryEncoderInterruptsSerial.ino
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
/***************************************************************************************************/
/*
This is an Arduino sketch for RotaryEncoder library using interrupts
written by : enjoyneering79
sourse code: https://github.com/enjoyneering/
This sketch uses interrupts, specials pins are required to interface
Board: int.0 int.1 int.2 int.3 int.4 int.5 Level
Uno, Mini, Pro, ATmega168, ATmega328..... 2 3 x x x x 5v
Mega2560................................. 2 3 21 20 19 18 5v
Leonardo, Micro, ATmega32U4.............. 3 2 0 1 7 x 5v
Digistump, Trinket, ATtiny85............. 2/physical pin 7 5v
Due, SAM3X8E............................. all digital pins 3v
Zero, ATSAMD21G18........................ all digital pins, except pin 4 3v
Blue Pill, STM32F103xxxx boards.......... all digital pins, maximun 16 pins at the same time 3v
ESP8266.................................. all digital pins, except gpio6 - gpio11 & gpio16 3v/5v
ESP32.................................... all digital pins 3v
NOTE:
- LOW interrupt trigges whenever the pin is low
- HIGH interrupt triggers whenever the pin is high (Arduino Due, Zero, MKR1000 only)
- CHANGE interrupt triggers whenever the pin changes value
- RISING interrupt triggers when the pin goes from low to high
- FALLING interrupt triggers when the pin goes from high to low
Frameworks & Libraries:
TimerOne AVR - https://github.com/PaulStoffregen/TimerOne
ATtiny Core - https://github.com/SpenceKonde/ATTinyCore
ESP32 Core - https://github.com/espressif/arduino-esp32
ESP8266 Core - https://github.com/esp8266/Arduino
STM32 Core - https://github.com/rogerclarkmelbourne/Arduino_STM32
GNU GPL license, all text above must be included in any redistribution,
see link for details - https://www.gnu.org/licenses/licenses.html
*/
/***************************************************************************************************/
#include <ESP8266WiFi.h>
#include <RotaryEncoder.h>
#define PIN_A D5 //ky-040 clk pin, interrupt & add 100nF/0.1uF capacitors between pin & ground!!!
#define PIN_B D6 //ky-040 dt pin, add 100nF/0.1uF capacitors between pin & ground!!!
#define BUTTON D7 //ky-040 sw pin, interrupt & add 100nF/0.1uF capacitors between pin & ground!!!
int16_t position = 0;
RotaryEncoder encoder(PIN_A, PIN_B, BUTTON);
void ICACHE_RAM_ATTR encoderISR() //interrupt service routines need to be in ram
{
encoder.readAB();
}
void ICACHE_RAM_ATTR encoderButtonISR()
{
encoder.readPushButton();
}
void setup()
{
WiFi.persistent(false); //disable saving wifi config into SDK flash area
WiFi.forceSleepBegin(); //disable AP & station by calling "WiFi.mode(WIFI_OFF)" & put modem to sleep
encoder.begin(); //set encoders pins as input & enable built-in pullup resistors
attachInterrupt(digitalPinToInterrupt(PIN_A), encoderISR, CHANGE); //call encoderISR() every high->low or low->high changes
attachInterrupt(digitalPinToInterrupt(BUTTON), encoderButtonISR, FALLING); //call encoderButtonISR() every high->low changes
Serial.begin(115200);
}
void loop()
{
if (position != encoder.getPosition())
{
position = encoder.getPosition();
Serial.println(position);
}
if (encoder.getPushButton() == true) Serial.println(F("PRESSED")); //(F()) saves string to flash & keeps dynamic memory free
}