Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1.  
  2. /* Basic Digital Read
  3. * ------------------
  4. * This code reads whether the phone is on the hook by treating that hook like a button that is either open or depressed
  5. */
  6.  
  7. #define INITPIN 12
  8. #define NUMPIN 11
  9.  
  10. int counter; // holds the pulse count for each dial spin
  11. int currentValue = 0;
  12. long lastDebounceTime = 0; // the last time the output pin was toggled
  13. long debounceDelay = 5; // the debounce time; increase if the output flickers
  14.  
  15. int ledPin = 13; // choose the pin for the LED
  16. int inPin = 7; // choose the input pin (for a pushbutton)
  17. int val = 0; // variable for reading the pin status
  18. int incomingByte = 0; //for incoming serial data
  19.  
  20. void setup(){
  21. Serial.begin(9600);
  22.  
  23. //configure the rotary wheel pins
  24. pinMode(INITPIN, INPUT_PULLUP);
  25. pinMode(NUMPIN, INPUT_PULLUP);
  26.  
  27. // configure the hook inputs, and the onboard LED.
  28. pinMode(ledPin, OUTPUT); // declare LED as output
  29. pinMode(inPin, INPUT); // declare phone hook as input
  30. }
  31.  
  32. void loop(){
  33.  
  34. hookReader();
  35. dialNumber();
  36.  
  37. }
  38.  
  39. void hookReader(){
  40. // send data only when you receive data:
  41. if (Serial.available() > 0) {
  42. // read the incoming byte:
  43. incomingByte = Serial.read();
  44.  
  45. // say what you got:
  46. Serial.print("I received: ");
  47. Serial.println(incomingByte, DEC);
  48. }
  49.  
  50.  
  51.  
  52. int initRead = digitalRead (INITPIN); // Is the wheel turning or not?
  53. static int lastValue = HIGH; // holds the last read from the pulse pin.
  54.  
  55. val = digitalRead(inPin); // read input value
  56.  
  57. if (val == LOW && incomingByte == 'B' && incomingByte != 'C') {
  58. digitalWrite(ledPin, LOW); // turn LED OFF
  59. Serial.println("dial tone");
  60. delay(1); // delay in between reads for stability
  61.  
  62.  
  63. } else if (val == HIGH) {
  64. // check if the input is HIGH (button released)
  65. digitalWrite(ledPin, HIGH); // turn LED ON
  66. Serial.println("on the hook"); // print "on the hook"
  67. Serial.print('A');
  68. delay(1); // delay in between reads for stability
  69. }
  70.  
  71.  
  72.  
  73. if (initRead == LOW) { // If the wheel is turning....
  74. Serial.println("dialing");
  75. }
  76.  
  77. else {
  78. Serial.println("waiting");
  79. }
  80.  
  81. }
  82.  
  83. void dialNumber(){
  84.  
  85. int initRead = digitalRead (INITPIN); // Is the wheel turning or not?
  86. static int lastValue = HIGH; // holds the last read from the pulse pin.
  87.  
  88. if (initRead == LOW) { // If the wheel is turning....
  89. int newValue = digitalRead (NUMPIN); // check the pulse pin.
  90. if (newValue != lastValue) { // if it's CHANGED from the last read...
  91. lastDebounceTime = millis(); // save its clock cycle; we need to check it.
  92. }
  93. // If enough time has passed (aka, it's not just a "bounce" from the
  94. // analog signal)...
  95. if ((millis() - lastDebounceTime) > debounceDelay) {
  96. // and if the current value is DIFFERENT than the one you just read...
  97. if (currentValue != newValue) {
  98. currentValue = newValue; // make them the same.
  99. if (newValue == 1) { // If you just set it to a 1...
  100. counter++; // it just finished a pulse, so add one to the counter.
  101. }
  102. }
  103. }
  104.  
  105. lastValue = newValue; // Your new value becomes the old one for comparison.
  106.  
  107. } else {
  108. // once the dial returns home and switches the initializing pin OFF,
  109. // you can be sure that there will be no more pulses coming.
  110. // "Counter" is the number dialed. You may now use it for whatever you want.
  111. // This is adjusted for the special case of "0" actually being 10 pulses.
  112. if (counter > 0) {
  113. if (counter == 10) {
  114. Serial.println (0);
  115. } else {
  116. Serial.println (counter);
  117. }
  118. }
  119. // After you're done using it, reset counter so it can start again on the
  120. // next dial.
  121. counter = 0;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement