Created
August 17, 2017 08:55
-
-
Save c7h/932f0ae25d163ae9847979d8c9ff6f48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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