Count encoder ticks and fast events with external interrupts.
Open in compiler →/*
Roboda verified Arduino sample 0587
Feature: Interrupt Counter
Generated for online build, classroom practice, and hardware upload testing.
*/
volatile unsigned long ticks = 0;
void onPulse() {
ticks++;
}
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), onPulse, FALLING);
}
void loop() {
noInterrupts();
unsigned long copy = ticks;
interrupts();
Serial.println(copy);
delay(465);
}
How it works
Practice this Arduino feature with a verified Roboda sketch.
- Read the constants at the top of the sketch.
- Wire the pins exactly as listed in the pin map.
- Upload or compile once before changing values.
- Change one value at a time and watch Serial Monitor or the output device.
Connections
| Pin | Connect to | Note |
|---|---|---|
| D2 | Encoder channel, pulse sensor, or button to GND | Use an interrupt-capable pin on your selected board. |