This is my code for the rotary dial alone:
int reading = digitalRead(wheelTicker);
long currentTime = millis();
if (((currentTime - lastStateChangeTime) > 3000) && input_number != "")
{
if (input_number.length() < 20)
{
input_number.toCharArray(dialedNumber, 20);
Serial.println("Dialling this number: ");
Serial.print(dialedNumber);
}
else
{
Serial.print("Not a valid phone number!");
}
delay(2000);
}
if ((currentTime - 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);
input_number += count % 10;
needToPrint = 0;
count = 0;
cleared = 0;
}
}
if (reading != lastState)
{
lastStateChangeTime = currentTime;
}
if ((currentTime - 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;
And it works fine by itself. Now I want to insert it into code I've written for a rotary phone connected to a gsm shield.
#include<GSM.h>
#include <ezButton.h>
// PIN Number
#define PINNUMBER ""
// initialize the library instance
GSM gsmAccess;
GSMVoiceCall vcs;
ezButton phone(12);
int wheelTicker = 4;
char dialedNumber[20];
int needToPrint = 0;
int count;
int lastState = LOW;
int trueState = LOW;
long lastStateChangeTime = 0;
int cleared = 0;
char number;
String input_number;
int dialHasFinishedRotatingAfterMs = 100;
int debounceDelay = 10;
char incomingNumber[20];
void setup()
{
// initialize serial communications and wait for port to open:
Serial.begin(9600);
pinMode(wheelTicker, INPUT);
while (!Serial)
{
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Phone is starting");
// connection state
bool notConnected = true;
// Start GSM shield
// If your SIM has PIN, pass it as a parameter of begin() in quotes
while (true)
{
Serial.println("connecting");
if (gsmAccess.begin(PINNUMBER) == GSM_READY)
{
break;
}
Serial.println("Not connected");
delay(1000);
}
// This makes sure the modem correctly reports incoming events
vcs.hangCall();
Serial.println("Phone started");
}
void loop()
{
phone.loop();
int hangedUp = phone.getState();
// Check the status of the voice call
switch (vcs.getvoiceCallStatus())
{
case IDLE_CALL: // Nothing is happening
if (hangedUp == HIGH)
{
Serial.println("Waiting for call!");
delay(1000);
}
if (hangedUp == LOW)
{
Serial.println("Dial a number!");
int reading = digitalRead(wheelTicker);
long currentTime = millis();
if (((currentTime - lastStateChangeTime) > 3000) && input_number != "")
{
if (input_number.length() < 20)
{
input_number.toCharArray(dialedNumber, 20);
Serial.println("Dialling this number: ");
Serial.print(dialedNumber);
}
else
{
Serial.print("Not a valid phone number!");
}
delay(2000);
}
if ((currentTime - 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);
input_number += count % 10;
needToPrint = 0;
count = 0;
cleared = 0;
}
}
if (reading != lastState)
{
lastStateChangeTime = currentTime;
}
if ((currentTime - 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;
}
break;
case RECEIVINGCALL: // Yes! Someone is calling us
Serial.println("RECEIVING CALL");
// Retrieve the calling number
vcs.retrieveCallingNumber(incomingNumber, 20);
// Print the calling number
Serial.print("Number:");
Serial.println(incomingNumber);
if (hangedUp == LOW)
{
Serial.println("phone is busy");
vcs.hangCall();
}
while (phone.getState() == HIGH)
{
if (vcs.getvoiceCallStatus() != RECEIVINGCALL)
{
delay(1000);
break;
}
Serial.println("pick up!");
phone.loop();
delay(1000);
}
vcs.answerCall();
break;
case TALKING: // In this case the call would be established
Serial.println("TALKING.");
if (phone.getState() == HIGH)
{
vcs.hangCall();
Serial.println("Hanging up");
}
break;
}
delay(1000);
}
Everything works fine except if(hangedUp == LOW)
. The code for the rotary dial within that if-clause just doesn't work. The variable hangedUp is just the state of a simple on and off switch.
Any help is appreciated.