Skip to content

Instantly share code, notes, and snippets.

@ihutc
Last active December 31, 2015 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihutc/8009208 to your computer and use it in GitHub Desktop.
Save ihutc/8009208 to your computer and use it in GitHub Desktop.
int rotaryPin = 2;
int hookswitchPin = 3;
int needToPrint = 0;
int count;
int lastState = LOW;
int trueState = LOW;
long lastStateChangeTime = 0;
int cleared = 0;
// constants
int dialHasFinishedRotatingAfterMs = 100;
int debounceDelay = 10;
void setup() {
Serial.begin(9600);
pinMode(rotaryPin, INPUT);
pinMode(hookswitchPin, INPUT);
}
boolean isIntroPlayed = false;
boolean isReadyForNumber = false;
boolean hasReceivedNumber = false;
int receivedNumber = 0;
void loop() {
readNumbers();
// User lifts receiver, hears intro once
if(digitalRead(hookswitchPin) == LOW && !isIntroPlayed) {
// INSERT THE CODE TO PLAY THE INTRO HERE
delay(100); // CHANGE DELAY TO THE LENGTH OF THE INTRO IN MILISECONDS
// Set the intro as played
isIntroPlayed = true;
}
else {
// Reset everything
isIntroPlayed = false;
}
// Do this after the intro has played
if(digitalRead(hookswitchPin) == LOW && isIntroPlayed) {
// Do this once after a number has been received
if(hasReceivedNumber) {
hasReceivedNumber = false;
// Do different things depending on nuber
switch(receivedNumber) {
case 0:
// INSERT CODE HERE FOR EACH NUMBER etc
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
}
}
}
}
void readNumbers() {
int reading = digitalRead(rotaryPin);
if ((millis() - lastStateChangeTime) > dialHasFinishedRotatingAfterMs) {
// the dial isn't being dialed, or has just finished being dialed.
if (needToPrint) {
// if it's only just finished being dialed, we need to send the number down the serial
// line and reset the count. We mod the count by 10 because '0' will send 10 pulses.
Serial.print(count % 10, DEC);
needToPrint = 0;
count = 0;
cleared = 0;
receivedNumber = count % 10;
hasReceivedNumber = true;
}
}
if (reading != lastState) {
lastStateChangeTime = millis();
}
if ((millis() - lastStateChangeTime) > debounceDelay) {
// debounce - this happens once it's stablized
if (reading != trueState) {
// this means that the switch has either just gone from closed->open or vice versa.
trueState = reading;
if (trueState == HIGH) {
// increment the count of pulses if it's gone high.
count++;
needToPrint = 1; // we'll need to print this number (once the dial has finished rotating)
}
}
}
lastState = reading;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment