Skip to content

Instantly share code, notes, and snippets.

@c7h c7h/rotarydial.ino
Created Aug 17, 2017

Embed
What would you like to do?
/* Rotary Dial encoder
encode the rotary dial of an old analog phone.
by Christoph Gerneth
*/
const int active_pin = 2;
const int pulse_pin = 3;
int number_counter = 0;
int last_pulse_state = 0; // last state of the rotary dial pulser
int last_active_state = 0;
// the following variables are unsigned long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup(){
pinMode(active_pin, INPUT); // configure rotary pins
pinMode(pulse_pin, INPUT);
digitalWrite(pulse_pin, HIGH); // pull all keys high
digitalWrite(active_pin, HIGH);
Serial.begin(9600);
Serial.println("intializing..");
}
void loop() {
// read inputs
int active_state = digitalRead(active_pin);
int pulse_state = digitalRead(pulse_pin);
if (pulse_state != last_pulse_state){
if ((millis() - lastDebounceTime) > debounceDelay) {
// actual button pressed
number_counter++;
lastDebounceTime = millis();
}
}
if (active_state == 0 && number_counter > 0){
Serial.print("counted ");
Serial.println(number_counter-1);
number_counter = 0;
}
last_active_state = active_state;
last_pulse_state = pulse_state;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.