timed action - trying to add timeout/break
hi,
i trying add timeout statement endless loop, within timed action loop.
the setup: wanted old rotary telephone ring @ set interval, every half hour (i set interval @ shorter time whilst testing) , if picked play message. if phone picked in meantime wanted dead tone play through receiver.
after phone has been ringing 15 seconds , not been picked want loop return main void loop , not carry on other statements within timed action loop.
i adapted blink without delay code in 1 of examples added break statement end loop @ point seems kind of work has glitches.
after 15 seconds phone stops ringing but,
1. looking @ serial.print still runs through next 2 steps in ring loop - though fast - less second, before returning main loop. causes slight glitch in sound coming speaker- possible straight main loop here?
2. if handset picked whilst ringing, message plays ok, when ring loop comes around second time ringing track not activated , serial.print shows runs through stages of ring loop before returning void loop again. next time ring loop occurs works fine - ringer plays expected
this code within ring loop,
just in case interested here entire code
i trying add timeout statement endless loop, within timed action loop.
the setup: wanted old rotary telephone ring @ set interval, every half hour (i set interval @ shorter time whilst testing) , if picked play message. if phone picked in meantime wanted dead tone play through receiver.
after phone has been ringing 15 seconds , not been picked want loop return main void loop , not carry on other statements within timed action loop.
i adapted blink without delay code in 1 of examples added break statement end loop @ point seems kind of work has glitches.
code: [select]
if (currentmillis - previousmillis2 > interval2) {
previousmillis2 = currentmillis;
if (ringstate == low)
ringstate = high;
else
ringstate = low;
rings ++;
}
if ((ringstate == low) && (rings >1)) {
serial.print("phone not picked stop ringing ");
rings = 0;
break;
}after 15 seconds phone stops ringing but,
1. looking @ serial.print still runs through next 2 steps in ring loop - though fast - less second, before returning main loop. causes slight glitch in sound coming speaker- possible straight main loop here?
2. if handset picked whilst ringing, message plays ok, when ring loop comes around second time ringing track not activated , serial.print shows runs through stages of ring loop before returning void loop again. next time ring loop occurs works fine - ringer plays expected
this code within ring loop,
code: [select]
void ring (){
if (hookstate==high) {
ringaction.setinterval(30*1000); //check again in 30 sec because phone lifted
}
else { // start ringing
serial.println("2 hookval low. start ringing: tracktwo high, relay low - start void ring loop ");
digitalwrite(relayswitch, low); //relay switch low - main speaker
while (digitalread(switchpin)==low) {
unsigned long currentmillis = millis();
if(currentmillis - previousmillis > interval) {
// save last time blinked led
previousmillis = currentmillis;
// if led off turn on , vice-versa:
if ((ledstate == low) && (blinks <1))
ledstate = high;
else
ledstate = low;
blinks ++;
// set led ledstate of variable:
digitalwrite(tracktwo, ledstate);
}
//todo add timeout endless loop, calls until pickup
if (currentmillis - previousmillis2 > interval2) {
previousmillis2 = currentmillis;
if (ringstate == low)
ringstate = high;
else
ringstate = low;
rings ++;
}
if ((ringstate == low) && (rings >1)) {
serial.print("phone not picked stop ringing ");
rings = 0;
break;
// ringaction.setinterval(interval); //schedule next call
}
}
//phone ringing , picked up
serial.println("3 hookval - trackthree high, relayswitch high - phone picked playing message ");
digitalwrite(relayswitch, high); //relay switch high - plays through earpiece
digitalwrite(trackthree, high); // play track 3 answer message
delay(200);
digitalwrite(trackthree, low);
while (digitalread(switchpin)==high) {/*wait*/
}
serial.print("4 hookval- trackstop high, relayswitch low - phone put down stop message playing ");
digitalwrite(trackstop, high);
delay(200);
digitalwrite(trackstop, low);
digitalwrite(relayswitch, low); //relay switch low - main speaker
serial.println("end of void ring loop ");
blinks=0;
ringaction.setinterval(interval); //schedule next call
}
}just in case interested here entire code
code: [select]
#include <timedaction.h>
//this initializes timedaction class make phone ring every 30mins.
const unsigned long interval = (60000*1); // 1 minute * x
timedaction ringaction = timedaction(interval, ring);
//pin / state variables
int trackone = 13;
const int tracktwo = 12;
int trackthree = 11;
int trackstop = 10;
int relayswitch = 9;
int switchpin = 2; // switch attached on pin 2
int hookstate; // reads state of switchpin , stores it
int oldhookstate;
int softswitch;
int hookval1;
int hookval2;
int rings = 0;
void(* resetfunc) (void) = 0;
// variables change:
int ledstate = low; // ledstate used set led
int ringstate = low;
long previousmillis = 0; // store last time led updated
long previousmillis2 = 0;
// follow variables long because time, measured in miliseconds,
// become bigger number can stored in int.
long interval = 500; // interval @ blink (milliseconds)
long interval2 = (1000*15); // time ringing track plays before retuning main loop
int blinks = 0;
void setup(){
pinmode(switchpin,input);
pinmode(trackone,output);
pinmode(tracktwo,output);
pinmode(trackthree,output);
pinmode(trackstop,output);
pinmode(relayswitch,output);
hookval1=hookval2= digitalread(switchpin);
softswitch = 0;
oldhookstate = hookstate = digitalread(switchpin); //reading input on pin 2
serial.begin(9600);
}
void loop(){
hookstate = digitalread(switchpin);
hookval1 = hookstate;
delay(20);
hookval2 = hookstate;
ringaction.check();
if ((hookstate==high) && (softswitch == 0) && (hookval1 ==hookval2)){ //phone picked without ringing - play dial tone track
serial.println("loop phone up");
digitalwrite(relayswitch, high); //relay switch high - plays through earpiece
digitalwrite(trackone, high);
delay(400);
digitalwrite(trackone, low);
softswitch = 1;
}
//if should trigger every loop when phone down should 'else'
if((hookstate == low) && (softswitch ==1) && (hookval1 ==hookval2)) { // when phone put down play track nothing on
serial.println("loop phone down");
digitalwrite(trackstop, high);
delay(300);
digitalwrite(trackstop,low);
delay(100);
digitalwrite(relayswitch,low); // relayswitch low
oldhookstate = hookstate;
softswitch = 0;
//should there condition this? do?
// digitalwrite (trackstop,low);
//digitalwrite (trackone,low);
//digitalwrite(relayswitch,low);
}
}
void ring (){
if (hookstate==high) {
ringaction.setinterval(30*1000); //check again in 30 sec because phone lifted
}
else { // start ringing
serial.println("2 hookval low. start ringing: tracktwo high, relay low - start void ring loop ");
digitalwrite(relayswitch, low); //relay switch low - main speaker
while (digitalread(switchpin)==low) {
unsigned long currentmillis = millis();
if(currentmillis - previousmillis > interval) {
// save last time blinked led
previousmillis = currentmillis;
// if led off turn on , vice-versa:
if ((ledstate == low) && (blinks <1))
ledstate = high;
else
ledstate = low;
blinks ++;
// set led ledstate of variable:
digitalwrite(tracktwo, ledstate);
}
//todo add timeout endless loop, calls until pickup
if (currentmillis - previousmillis2 > interval2) {
previousmillis2 = currentmillis;
if (ringstate == low)
ringstate = high;
else
ringstate = low;
rings ++;
}
if ((ringstate == low) && (rings >1)) {
serial.print("phone not picked stop ringing ");
rings = 0;
break;
}
}
//phone ringing , picked up
serial.println("3 hookval - trackthree high, relayswitch high - phone picked playing message ");
digitalwrite(relayswitch, high); //relay switch high - plays through earpiece
digitalwrite(trackthree, high); // play track 3 answer message
delay(200);
digitalwrite(trackthree, low);
while (digitalread(switchpin)==high) {/*wait*/
}
serial.print("4 hookval- trackstop high, relayswitch low - phone put down stop message playing ");
digitalwrite(trackstop, high);
delay(200);
digitalwrite(trackstop, low);
digitalwrite(relayswitch, low); //relay switch low - main speaker
serial.println("end of void ring loop ");
blinks=0;
ringaction.setinterval(interval); //schedule next call
}
}
what if change:
to:
then move this:
below while (digitalread(switchpin)==low) loop.
not tested , not complete solution, yes wanted write in full caps 8-)
code: [select]
while (digitalread(switchpin)==low) {
unsigned long currentmillis = millis();to:
code: [select]
unsigned long currentmillis = millis();
previousmillis = millis();
while (digitalread(switchpin)==low) {then move this:
code: [select]
while (digitalread(switchpin)==high) {/*wait*/
}
serial.print("4 hookval- trackstop high, relayswitch low - phone put down stop message playing ");
digitalwrite(trackstop, high);
delay(200);
digitalwrite(trackstop, low);
digitalwrite(relayswitch, low); //relay switch low - main speaker
serial.println("end of void ring loop ");
blinks=0;
ringaction.setinterval(interval); //schedule next callbelow while (digitalread(switchpin)==low) loop.
not tested , not complete solution, yes wanted write in full caps 8-)
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > timed action - trying to add timeout/break
arduino
Comments
Post a Comment