Timed Action Compiling Help

Hi,
I have written some code based on the timed action example, but am having a few problems getting the code to compile. I dont really have much experience of using this library before so i'm probably missing something obvious...

the problem:
In the second line of codeTimedAction ringAction = TimedAction(300000, ring);
the compiler gives the error message ''ring' was not declared in this scope'

Clearly something is wrong but as far as i can tell i have followed the layout for naming a timed action from the example code. Any ideas what i'm doing wrong?

here's the code:

#include <TimedAction.h>

//this initializes a TimedAction class that will change the state of an LED every second.
TimedAction ringAction = TimedAction(300000, ring);

//pin / state variables
int trackone = 13;
int tracktwo = 12;
int trackthree = 11; 
int trackstop = 10;
int relayswitch = 9;
int switchPin = 2;
int softswitch;
int hookState;

void setup(){
  pinMode(switchPin,INPUT);
  pinMode(trackone,OUTPUT);
  pinMode(tracktwo,OUTPUT);
  pinMode(trackthree,OUTPUT);
  pinMode(trackstop,OUTPUT);
  pinMode(relayswitch,OUTPUT);
  softswitch = 0;
  hookState = digitalRead(switchPin);
}

void loop(){

  ringAction.check();

  if (hookState == HIGH) { //sets the relays outputs either to speaker one or two
    digitalWrite(relayswitch, HIGH);
    if (hookState == LOW)
      digitalWrite(relayswitch,LOW);


    if (hookState == HIGH) && (softswitch == 0) { //if phone is picked up without ringing signal is given to play dial tone track
      digitalWrite(trackone, HIGH);
      delay(400);
      digitalWrite(trackone, LOW);
      delay(200);
      softswitch = 1;  //stops code running in continuous loop
      if (hookState == LOW) && (softswitch ==1); // when phone is put down play track with nothing on
      digitalWrite(trackstop, HIGH);
      delay(400);
      softswitch = 0;  //stops code running in continuous loop

    }
  } 

}

void ring (){ 

  if (hookState== LOW) {
    digitalWrite(tracktwo, HIGH);
    delay (200);
    digitalWrite(tracktwo, LOW);
    delay(200);
    softswitch = 3;

    else if (hookState == HIGH){
      delay(30000); //wait 30 secs then check again


      if (hookState == HIGH) && (softswitch == 3) { 
        digitalWrite(trackthree, HIGH);
        delay(200);
        digitalWrite(trackthree, LOW);
        delay(200);
        softswitch = 4;


        if (hookState == LOW) && (softswitch == 4) {
          digitalWrite(trackstop, HIGH);
          delay(200);
          digitalWrite(trackstop, LOW);
          delay(200);
          softswitch = 0;
        }
      }
    }
  }

}

thanks for any suggestions.

This compiles: (Arduino-0021 + TimedAction in %ARDUINO%/libraries)

#include <TimedAction.h>

//this initializes a TimedAction class that will change the state of an LED every second.
TimedAction ringAction = TimedAction(300000, ring);

//pin / state variables
int trackone = 13;
int tracktwo = 12;
int trackthree = 11;
int trackstop = 10;
int relayswitch = 9;
int switchPin = 2;
int softswitch;
int hookState;

void setup(){
  pinMode(switchPin,INPUT);
  pinMode(trackone,OUTPUT);
  pinMode(tracktwo,OUTPUT);
  pinMode(trackthree,OUTPUT);
  pinMode(trackstop,OUTPUT);
  pinMode(relayswitch,OUTPUT);
  softswitch = 0;
  hookState = digitalRead(switchPin);
}

void loop(){

  ringAction.check();

  if (hookState == HIGH) { //sets the relays outputs either to speaker one or two
    digitalWrite(relayswitch, HIGH);
    if (hookState == LOW)
      digitalWrite(relayswitch,LOW);


    if ((hookState == HIGH) && (softswitch == 0)) { //if phone is picked up without ringing signal is given to play dial tone track
      digitalWrite(trackone, HIGH);
      delay(400);
      digitalWrite(trackone, LOW);
      delay(200);
      softswitch = 1;  //stops code running in continuous loop
      if ((hookState == LOW) && (softswitch ==1)) // when phone is put down play track with nothing on
      //DO YOU WANT {}HERE?
      digitalWrite(trackstop, HIGH);
      delay(400);
      softswitch = 0;  //stops code running in continuous loop

    }
  }

}

void ring (){

  if (hookState== LOW) {
    digitalWrite(tracktwo, HIGH);
    delay (200);
    digitalWrite(tracktwo, LOW);
    delay(200);
    softswitch = 3;
  } else if (hookState == HIGH){
      delay(30000); //wait 30 secs then check again


      if ((hookState == HIGH) && (softswitch == 3)) {
        digitalWrite(trackthree, HIGH);
        delay(200);
        digitalWrite(trackthree, LOW);
        delay(200);
        softswitch = 4;


        if ((hookState == LOW) && (softswitch == 4)) {
          digitalWrite(trackstop, HIGH);
          delay(200);
          digitalWrite(trackstop, LOW);
          delay(200);
          softswitch = 0;
        }
      }
    }
}

You need to change the internal 'time' variable of TimedAction to unsigned long.
The max interval is 65535.

If you ask me, I'll do it and publish the library as a new version. I need a catalyst.
:slight_smile:

Thanks, so much. :slight_smile:

I'd heard about the unsigned long prob before so i'd already changed the library files... but i reckon it would make alot of people happy if you did upload an updated version, it makes sense... thanks again.

More help for the stupid...
thanks for getting me up and running with the code before AlphaBeta, i ran the code and there are a few probs, I've made some changes but i'm up against the old compiler issue 'expected identifier before '(' token' for the first part of the 'void ring' code. arrghh! how can i avoid doing this again? Any help welcome!

#include <TimedAction.h>

//this initializes a TimedAction class to make a phone ring every 30mins.
TimedAction ringAction = TimedAction(10000, ring);

//pin / state variables
int trackone = 13;
int tracktwo = 12;
int trackthree = 11;
int trackstop = 10;
int relayswitch = 9;
int switchPin = 2;  // switch attached on pin 2
int softswitch;
int hookState; // reads the state of switchPin and stores it
int hookval; //reads the switchPin state in loop

void setup(){
  pinMode(switchPin,INPUT);

  pinMode(trackone,OUTPUT);
  pinMode(tracktwo,OUTPUT);
  pinMode(trackthree,OUTPUT);
  pinMode(trackstop,OUTPUT);
  pinMode(relayswitch,OUTPUT);

  softswitch = 0;
  hookState = digitalRead(switchPin); //reading input on pin 2
  Serial.begin(9600);

}

void loop(){

  hookState = digitalRead(switchPin);


  ringAction.check();

  if (hookState == LOW) {
    digitalWrite(relayswitch,LOW);
  }
  {
    if (hookState == HIGH)  //sets the relay outputs either to speaker one or two
      digitalWrite(relayswitch, HIGH);
  }



  if ((hookState == HIGH) && (softswitch == 0)) { //if phone is picked up without ringing signal is given to play dial tone track
    digitalWrite(trackone, HIGH);
    delay(400);
    digitalWrite(trackone, LOW);
    delay(200);
    softswitch = 1;  //stops code running in continuous loop
  }

  if ((hookState == LOW) && (softswitch ==1)) { // when phone is put down play track with nothing on
    digitalWrite(trackstop, HIGH);
    delay(400);
    digitalWrite(trackstop,LOW);
    delay(200);
    softswitch = 0;  //stops code running in continuous loop
  }  

}

void ring (){

  if (hookState== LOW) && (softswitch == 0) {
    digitalWrite(tracktwo, HIGH);
    delay (400);
    digitalWrite(tracktwo, LOW);
    delay(200);
    softswitch = 3;

    else{
      delay(30000); //wait 30 secs then check again. ? i think this could be stalling the loop for 30 secs?
    }


    if ((hookState == HIGH) && (softswitch == 3)) {
      digitalWrite(trackthree, HIGH);
      delay(400);
      digitalWrite(trackthree, LOW);
      delay(200);
      softswitch = 4;
    }


    if ((hookState == LOW) && (softswitch == 4)) {
      digitalWrite(trackstop, HIGH);
      delay(400);
      digitalWrite(trackstop, LOW);
      delay(200);
      softswitch = 0;
    }
  }
}

You see this line: if (hookState== LOW) && (softswitch == 0) { ?

You need to encapsulate the equality checks inside a set of parans so they get evaluated as a whole.
As the code is now you have told the program to check if (hookState== LOW) then if that is true do && (softswitch == 0) { and is not a valid c++ statement.

So you need to make the line like this: if ((hookState== LOW) && (softswitch == 0)) {

You see?

Got it. Cheers.

Now, I've uploaded TimedAction version 1.6 and it uses unsigned long also for the interval variable.
Maximum (theoretical) interval of 4,294,967,295 :slight_smile:

(each TimedAction instance costs two bytes of RAM more, not a big deal)

ok. tested the code again and it half works. the loop code seems to do what it is meant to; it waits for a switch closure and release and triggers the correct outputs, the problem is that the code then loops. i had anticipated this by including the softswitch variable, it doesnt seem to be working as i thought it would. Any tricks to get round this? I commented out void ring loop, so i could work with one problem at a time...

#include <TimedAction.h>

//this initializes a TimedAction class to make a phone ring every 30mins.
//TimedAction ringAction = TimedAction(10000, ring);

//pin / state variables
int trackone = 13;
int tracktwo = 12;
int trackthree = 11;
int trackstop = 10;
int relayswitch = 9;
int switchPin = 2;  // switch attached on pin 2
int softswitch;
int hookState; // reads the state of switchPin and stores it
int hookval; //reads the switchPin state in loop

void setup(){
  pinMode(switchPin,INPUT);

  pinMode(trackone,OUTPUT);
  pinMode(tracktwo,OUTPUT);
  pinMode(trackthree,OUTPUT);
  pinMode(trackstop,OUTPUT);
  pinMode(relayswitch,OUTPUT);

  softswitch = 0;
  hookState = digitalRead(switchPin); //reading input on pin 2
  Serial.begin(9600);

}

void loop(){

  hookval = digitalRead(switchPin);


 // ringAction.check();

  if (hookval == LOW) {
    digitalWrite(relayswitch,LOW);
  }
  if (hookval == HIGH) { //sets the relay outputs either to speaker one or two
      digitalWrite(relayswitch, HIGH);
  }

  if ((hookval == HIGH) && (softswitch == 0)) { //phone is picked up without ringing - play dial tone track
    digitalWrite(trackone, HIGH);
    delay(400);
    digitalWrite(trackone, LOW);
    delay(200);
    softswitch = 1;  //stops code running in continuous loop
  }

  if ((hookval == LOW) && (softswitch ==1)) { // when phone is put down play track with nothing on
    digitalWrite(trackstop, HIGH);
    delay(400);
    digitalWrite(trackstop,LOW);
    delay(200);
    softswitch = 0;  //stops code running in continuous loop
  }  
  

}

/*void ring (){

  if ((hookval== LOW) && (softswitch == 0)) { // check if phone is on the hook, if it is start ringing
    digitalWrite(tracktwo, HIGH); // trigger track two - ringtone
    delay (400);
    digitalWrite(tracktwo, LOW);
    delay(200);
    softswitch = 3;
  }
    else if ((hookval == HIGH) && (softswitch== 0)) { 
      delay(30000); //if phone is picked up already wait 30 secs then check again.
    }
    if ((hookval == HIGH) && (softswitch == 3)) {
      digitalWrite(trackthree, HIGH);
      delay(400);
      digitalWrite(trackthree, LOW);
      delay(200);
      softswitch = 4;


    if ((hookval == LOW) && (softswitch == 4)) {
      digitalWrite(trackstop, HIGH);
      delay(400);
      digitalWrite(trackstop, LOW);
      delay(200);
      softswitch = 0;
    
    }
  }
}
*/

Just spotted the Time action update - good work :sunglasses: