I have a dial attached to pin 8. Rotating the dial sends out pulses which are measured through digitalread. Once the dial finishes rotating the number of pulses is put into an array. The problem is if I have it as part of a switch statement the measurements don't seem to happen properly.
loop()
{
dial();
}
Seems to work fine. However if I do something like
loop()
{
switch (vcs.CallStatus())
{
case X: //casex is met by default
dial();
break;
case Y:
break;
case Z:
break;
}
}
It seems to be unable to properly detect the dialing, especially with smaller number of pulses. I think it might have to do with the pin not being measured quickly enough as the arduino is looping through the code so pulses are lost as they occur between measurements. The problem is I want to keep track of both callstatus and whether dialing is occurring at the same time or at least have them measured quickly enough that it does not matter and would rather not choose one or the other.
Dialing a value that allows the dial to rotate longer makes it more likely that a value (not necessarily the right one) is entered.
Here is the dialing code
//variables to read in dial pulses
int needToPrint = 0;
int count; //dial pulse counts
int lastState = LOW;
int trueState = LOW;
long lastStateChangeTime = 0;
// constants
int dialHasFinishedRotatingAfterMs = 100;
int debounceDelay = 10;
//phone number variables
int numquota = 0;//keeps track of how many digits of the phone number we have entered
char callednum[20];//number to call
int dial() //code for recognizing and reading in dial pulses
{
int reading = digitalRead(dialPin);//set pin to read dial pulses
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
// and reset the count.
transferredchar = dialarray[count-1];
callednum[numquota]= transferredchar;
callednum[numquota+1]= '\0';
Serial.println("detecting dialing");
Serial.println(callednum);
needToPrint = 0;
count = 0;
numquota++;//increment till phone number length reached
}
}
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;
//rotary addition
if (numquota == 11 ) {//once the last digit has been reached send the number
Serial.println("SENDING");
gsm.call(callednum, 100);//send number to call
while(1);
Serial.println("NUMBER SENT");
numquota = 0;//resets phone number digit count
callednum[0] = '\0';
}
}