Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Basic Digital Read
- * ------------------
- * This code reads whether the phone is on the hook by treating that hook like a button that is either open or depressed
- */
- #define INITPIN 12
- #define NUMPIN 11
- int counter; // holds the pulse count for each dial spin
- int currentValue = 0;
- long lastDebounceTime = 0; // the last time the output pin was toggled
- long debounceDelay = 5; // the debounce time; increase if the output flickers
- int ledPin = 13; // choose the pin for the LED
- int inPin = 7; // choose the input pin (for a pushbutton)
- int val = 0; // variable for reading the pin status
- int incomingByte = 0; //for incoming serial data
- void setup(){
- Serial.begin(9600);
- //configure the rotary wheel pins
- pinMode(INITPIN, INPUT_PULLUP);
- pinMode(NUMPIN, INPUT_PULLUP);
- // configure the hook inputs, and the onboard LED.
- pinMode(ledPin, OUTPUT); // declare LED as output
- pinMode(inPin, INPUT); // declare phone hook as input
- }
- void loop(){
- hookReader();
- dialNumber();
- }
- void hookReader(){
- // send data only when you receive data:
- if (Serial.available() > 0) {
- // read the incoming byte:
- incomingByte = Serial.read();
- // say what you got:
- Serial.print("I received: ");
- Serial.println(incomingByte, DEC);
- }
- int initRead = digitalRead (INITPIN); // Is the wheel turning or not?
- static int lastValue = HIGH; // holds the last read from the pulse pin.
- val = digitalRead(inPin); // read input value
- if (val == LOW && incomingByte == 'B' && incomingByte != 'C') {
- digitalWrite(ledPin, LOW); // turn LED OFF
- Serial.println("dial tone");
- delay(1); // delay in between reads for stability
- } else if (val == HIGH) {
- // check if the input is HIGH (button released)
- digitalWrite(ledPin, HIGH); // turn LED ON
- Serial.println("on the hook"); // print "on the hook"
- Serial.print('A');
- delay(1); // delay in between reads for stability
- }
- if (initRead == LOW) { // If the wheel is turning....
- Serial.println("dialing");
- }
- else {
- Serial.println("waiting");
- }
- }
- void dialNumber(){
- int initRead = digitalRead (INITPIN); // Is the wheel turning or not?
- static int lastValue = HIGH; // holds the last read from the pulse pin.
- if (initRead == LOW) { // If the wheel is turning....
- int newValue = digitalRead (NUMPIN); // check the pulse pin.
- if (newValue != lastValue) { // if it's CHANGED from the last read...
- lastDebounceTime = millis(); // save its clock cycle; we need to check it.
- }
- // If enough time has passed (aka, it's not just a "bounce" from the
- // analog signal)...
- if ((millis() - lastDebounceTime) > debounceDelay) {
- // and if the current value is DIFFERENT than the one you just read...
- if (currentValue != newValue) {
- currentValue = newValue; // make them the same.
- if (newValue == 1) { // If you just set it to a 1...
- counter++; // it just finished a pulse, so add one to the counter.
- }
- }
- }
- lastValue = newValue; // Your new value becomes the old one for comparison.
- } else {
- // once the dial returns home and switches the initializing pin OFF,
- // you can be sure that there will be no more pulses coming.
- // "Counter" is the number dialed. You may now use it for whatever you want.
- // This is adjusted for the special case of "0" actually being 10 pulses.
- if (counter > 0) {
- if (counter == 10) {
- Serial.println (0);
- } else {
- Serial.println (counter);
- }
- }
- // After you're done using it, reset counter so it can start again on the
- // next dial.
- counter = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement