I am writing code for a phone controlled device and I need to be able to reset the code when there is nothing happening. I tried using goto from my functions wait and getnumber but it does not seem to work globally and will only work locally within each function. I know that goto command is frowned upon but I can't come up with anything else at this point. Any help would be greatly appreciated.
//dtmf code
//xbee connected to pins 0 and 1
//ringer connected to pin 2
//tone out connected to pin 3
//dtmf OE connected to pin 4
//dtmf Q1 - Q4 connected to pins 5 - 8
//hook relay connected to pin 9
const int ringer = 2;
const int tonepin = 3;
const int OE = 4;
const int Q1 = 5;
const int Q2 = 6;
const int Q3 = 7;
const int Q4 = 8;
const int hook = 9;
const int ledpin = 13; // indicator LED
int buff[4];
int buff2[4];
int passcode[4] = {0,0,0,0};
int command;
int a=0;
void setup() {
pinMode (ringer, INPUT);
pinMode (hook, OUTPUT);
pinMode (OE, INPUT);
pinMode (Q1, INPUT);
pinMode (Q2, INPUT);
pinMode (Q3, INPUT);
pinMode (Q4, INPUT);
Serial.begin(9600);
}
void loop(){
restart:
while (a <= 3){
if (getnumber() == 0 && digitalRead(ringer) == HIGH){
a++;
delay(200);
}
else{a = 0;}
if (a >= 3){
a = 0;
digitalWrite(hook, HIGH);
input:
wait(OE, LOW);
buff[0] = getnumber();
wait(OE, LOW);
buff[1] = getnumber();
wait(OE, LOW);
buff[2] = getnumber();
wait(OE, LOW);
buff[3] = getnumber();
if (buff == passcode){
again:
beep(2);
wait(OE, LOW);
command = getnumber();
switch (command){
case 11:
Serial.println("UNLOCK");
digitalWrite(ledpin,HIGH);
delay(500);
digitalWrite(ledpin,LOW);
beep(1);
goto again;
break;
case 12:
Serial.println("LOCK");
digitalWrite(ledpin,HIGH);
delay(500);
digitalWrite(ledpin,LOW);
beep(2);
goto again;
break;
case 0:
enter:
beep(3);
wait(OE, LOW);
buff[0] = getnumber();
wait(OE, LOW);
buff[1] = getnumber();
wait(OE, LOW);
buff[2] = getnumber();
wait(OE, LOW);
buff[3] = getnumber();
beep(3);
wait(OE, LOW);
buff2[0] = getnumber();
wait(OE, LOW);
buff2[1] = getnumber();
wait(OE, LOW);
buff2[2] = getnumber();
wait(OE, LOW);
buff2[3] = getnumber();
if (buff == buff2){
passcode[0] = buff[0];
passcode[1] = buff[1];
passcode[2] = buff[2];
passcode[3] = buff[3];
delay(100);
beep(2);
}
else{
tone(tonepin, 104, 1000);
delay(500);
goto enter;
}
goto again;
break;
default:
tone(tonepin, 104, 1000);
goto again;
break;
}
}
else{
tone(tonepin, 104, 1000);
}
goto input;
}
}
}
int getnumber()
{
int keyvalue;
int x;
int i = 0;
do
{
i++;
x = digitalRead(OE);
delay(50);
if (i >= 600){ //remove hook when nothing pressed for 30 sec: 20 = 1 second
digitalWrite(hook, LOW);
goto restart;
}
}while (x != HIGH);
if (digitalRead(Q1) == HIGH) { keyvalue = 1; }
else { keyvalue = 0; }
if (digitalRead(Q2) == HIGH) { keyvalue = keyvalue + 2; }
if (digitalRead(Q3) == HIGH) { keyvalue = keyvalue + 4; }
if (digitalRead(Q4) == HIGH) { keyvalue = keyvalue + 8; }
return keyvalue;
}
void wait(int pin, int state){
int i = 0;
do
{
i++;
x = digitalRead(pin);
delay(50);
if (i >= 600){ //remove hook when held for 30 sec: 20 = 1 second
digitalWrite(hook, LOW);
goto restart;
}
}while (x != state);
}
void beep(int numbeeps){
for (int i=0; i<numBeeps; i++) {
tone(tonepin, 1047, 250);
delay(250);
}
}
basically I need it to be able to restart the main void loop when nothing is pressed for 30 secs after it hangs up the line.