Uses callbacks for the rotary encoder and its push switch. The callbacks run inside the interrupt, so they only record what happened; loop() does the printing. Prints the count when the knob turns, toggles the green LED (D1) on each press and lights the red LED (D2) while the switch is held.
volatile bool turned = false;
volatile bool press_seen = false;
void onEncoder(int32_t count, int8_t step) {
(void)count;
(void)step;
turned = true;
}
void onSwitch(bool pressed) {
if (pressed) {
press_seen = true;
}
}
void setup() {
Serial.begin(115200);
}
void loop() {
if (turned) {
turned = false;
Serial.print("Count: ");
}
if (press_seen) {
press_seen = false;
Serial.print("Pressed at count ");
}
}
Library for the U of T Physics Analog Shield (Introduction to Electronics).
Encoder, switch, temperature signals, potentiometer and LEDs on the Analog Shield.
Definition AnalogShield.h:64
void LedON(uint8_t index, bool on=true) noexcept
Turns an LED on or off.
Definition AnalogShield.cpp:266
void SetEncoderCallback(EncoderCallback callback) noexcept
Sets the function called on every encoder step.
Definition AnalogShield.cpp:104
int32_t EncoderCount() const noexcept
Encoder steps since begin() or the last SetEncoderCount().
Definition AnalogShield.cpp:93
void begin()
Sets pin modes, turns both LEDs off and starts tracking the encoder and its switch.
Definition AnalogShield.cpp:56
void SetSwitchCallback(SwitchCallback callback) noexcept
Sets the function called when the encoder switch is pressed or released.
Definition AnalogShield.cpp:146
void LedToggle(uint8_t index) noexcept
Toggles an LED.
Definition AnalogShield.cpp:272
Shield signals by Arduino pin, from the Rev-C schematic.
Definition AnalogShield.h:26
constexpr uint8_t LED1
D1 green LED, active low (anode to VDD).
Definition AnalogShield.h:44
constexpr uint8_t LED2
D2 red LED, active low (anode to VDD).
Definition AnalogShield.h:43