You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.3 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// Пример с прямой работой библиотеки на прерываниях
#include <EncButton.h>
EncButton<EB_TICK, 2, 3, 4> enc; // энкодер с кнопкой <A, B, KEY>
//EncButton<EB_TICK, 2, 3> enc; // просто энкодер <A, B>
//EncButton<EB_TICK, 4> enc; // просто кнопка <KEY>
void setup() {
Serial.begin(9600);
// желательно подключить оба пина энкодера на внешние прерывания по CHANGE
// можно использовать PCINT https://github.com/NicoHood/PinChangeInterrupt
attachInterrupt(0, isr, CHANGE); // D2
attachInterrupt(1, isr, CHANGE); // D3
// подключил оба прерывания на одну функцию
}
void isr() {
enc.tickISR(); // в прерывании вызываем тик ISR
}
void loop() {
// тут тоже вызываем тик, нужен для
// корректной работы дебаунсов и прочих таймеров!!!
enc.tick();
if (enc.turn()) { // любой поворот
Serial.print("turn ");
Serial.println(enc.counter); // вывод счётчика
}
// имитация загруженной программы, обработка происходит в прерывании
delay(50);
}