SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By MICyborg
#176834
i try different ways to do this but i cant make it working.
Code: Select all
//name the pins, elapsed time and output. pin1 = A0, pin2 = A4, pin3 = A5, pin4=1, pin5 = 0
const int ringerPin = A0;
//const int offhook = A4;
const int onhook = A4;
unsigned long beginRing;
//double beginRing = -40000;

void setup(){
    pinMode(ringerPin, OUTPUT);
    //pinMode(offhook, INPUT);
    pinMode(onhook, INPUT);
    randomSeed(analogRead(0)); // this starts Arduino's random number generator
}
  
int randCall = random(20200, 100000); // generate a random number between 1 min and 60 =3600000ms min

//ring every 1 to 60 minutes if the phone is down (onhook, arduino gnd and digitalpin connected
//and dont ring if the phone is picked up (no onhook)


void loop()
{
  if (digitalRead(onhook) == HIGH){
    //timerTime = millis();
    delay (randCall);
    beginRing = millis();
    //elapsedTime = beginRingtimerTime
    while(beginRing < beginRing + 20000 && digitalRead(onhook) == HIGH){
      //turn audio off
      for(int x = 0; x < 15; x++){
        digitalWrite(ringerPin, HIGH);   // turn the solenoid on (HIGH is the voltage level)
        delay(50);                           // wait for 50 msec
        digitalWrite(ringerPin, LOW);    // turn the solenoid off by making the voltage LOW
        delay(80);                          // wait for 80 msec
      }
      delay(2500);
    }
  }
  if(beginRing < beginRing + 20000){
    //play one randomly choosen audiofile out of 10
  }
}
i realy tryed everything. please can somebody help me!
thanx in advance :-D
By Mee_n_Mac
#176850
You've got this ...
(beginRing < beginRing + 20000)
in both your while() and if() statements. That's always going to evaluate to true.

And while this ...
delay (randCall);
is in your loop(), the randCall variable is only set once, in setup() and isn't going to be random from loop to loop. Also note that while delaying for randCall msec, the program is NOT checking to see if the handset is on the hook (or not). The program will not respond to lifting the handset until the delay is done.
By Mee_n_Mac
#176852
Look at this instead. Note how the loop() runs "quickly" and asks each pass whether it's time to do something. Something like ringing the phone or checking if it's on/off hook. What I don't like is that the tune will play whenever the phone is answered, even if it wasn't ringing. That's easy enough to fix though. Also note how the phone rings for 2 secs (when it's time to) and then briefly checks for the phone to be on/off hook and then rings another 2 secs (if still onhook). It does this for some fixed amount of times, set by maxCount. It does mean the phone will continue to ring for 1+ secs when answered. I don't see that as too bad but it can be fixed as well.

It also means that the phone won't ring forever until answered. Eventually it "gives up" and waits another random time to ring again.


Start simple and work to something good !
Code: Select all
//name the pins, elapsed time and output. pin1 = A0, pin2 = A4, pin3 = A5, pin4=1, pin5 = 0
const int ringerPin = A0; // TRIG1 is a free pin
const int onhook = 1;     // TRIG4 is not normally used when not FTDI'ing
const int maxCount = 10;  // ring phone this number of times before stopping
int ringCount = 0;        // variable to count number of unanswered rings
unsigned long lastRing;   // time of last ring start
unsigned long randTime;   // a random time btw 1 and 60 mins

void setup() {
  pinMode(ringerPin, OUTPUT);     // connect to transistor driver
  pinMode(onhook, INPUT_PULLUP);  //connect to NC switch, SW = open when onhook
  randomSeed(analogRead(0));      // this starts Arduino's random number generator
}

//ring every 1 to 60 minutes if the phone is down (onhook, arduino gnd and digitalpin connected
//and dont ring if the phone is picked up (no onhook)
void loop()
{
  if (digitalRead(onhook) == HIGH) { // phone is hung up, aka onhook
    // kill any tune that is playing
    //put above code here
    // check to see if it's time to ring the phone
    if(millis() > lastRing + randTime){
      // ring the phone a few times
      ringCount++; // used to stop phone from ringing forever unless answered
      for (int x = 0; x < 15; x++) {     //ring for 2 secs
        digitalWrite(ringerPin, HIGH);   // turn the solenoid on (HIGH is the voltage level)
        delay(50);                       // wait for 50 msec
        digitalWrite(ringerPin, LOW);    // turn the solenoid off by making the voltage LOW
        delay(80);                       // wait for 80 msec
      }
      if(ringCount > maxCount) {
        // reset next time to ring phone if not answered soon enough
        ringCount = 0;
        lastRing = millis();
        randTime = random(60000, 3600000); // generate a random number between 1 min and 60 min
      }
    }
  }
  else // phone has been picked up, aka offhook
  {
    // play some tune
    //put above code here
    // now reset randome time for ringing
    lastRing = millis();
    randTime = random(60000, 3600000); // generate a random number between 1 min and 60 min
  }
}
I note that pin D1/TRIG4 is normally an output when connected to the FTDI link. Then it might maybe be redefined into a MIDI output, if so coded. The above code makes it an input and connects it to a NC switch for the phone handset. When hungup (onhook) the code expects the switch to be open and thus allow a pull-up to logic 1. You can reverse this of you have a NO switch (more common) instead. Now this might work w/the FTDI cable attached (input connected to input+switch), I don't see it hurting anything if the FTDI cable is connected when the above code is running (which is why I did it this way) but it may not work w/the FTDI cable attached. Test w/o the cable attached. Power the LP somehow.
By MICyborg
#176869
It works...
thank you so much Mee n Mac.

i had to make the ringing period with 2500ms delays after 15 pulses in it so it sounds like the original phone.
did this by using my old code (with the while loop) at this section. So, it works and the A4 triggers the TRIGGER2, dont know why but it is like that.

Final Code (random time values are others cuz after testing this sounds better) :
Code: Select all
#include <SPI.h>            // To talk to the SD card and MP3 chip
#include <SdFat.h>          // SD card file system
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>   // MP3 decoder chip

//name the pins, elapsed time and output. pin1 = A0, pin2 = A4, pin3 = A5, pin4=1, pin5 = 0
const int ringerPin = A0; // TRIG1 is a free pin
const int onhook = A4;     // TRIG4 is not normally used when not FTDI'ing
const int maxCount = 5;  // ring phone this number of times before stopping
int ringCount = 0;        // variable to count number of unanswered rings
unsigned long lastRing;   // time of last ring start
unsigned long randTime;   // a random time btw 1 and 60 mins

const uint8_t volume = 0; // MP3 Player volume 0=max, 255=lowest (off)
const uint16_t monoMode = 1;  // Mono setting 0=off, 3=max

// Create library objects:
SFEMP3Shield MP3player;
SdFat sd;

void setup() {
  pinMode(ringerPin, OUTPUT);     // connect to transistor driver
  pinMode(onhook, INPUT_PULLUP);  //connect to NC switch, SW = open when onhook
  randomSeed(analogRead(0));      // this starts Arduino's random number generator

    //start the shield
    sd.begin(SD_SEL, SPI_HALF_SPEED);
    MP3player.begin();
}


//ring every 1 to 60 minutes if the phone is down (onhook, arduino gnd and digitalpin connected
//and dont ring if the phone is picked up (no onhook)
void loop()
{
  if (digitalRead(onhook) == HIGH) { // phone is hung up, aka onhook
    // kill any tune that is playing
     MP3player.stopTrack();
    //put above code here
    // check to see if it's time to ring the phone
    if(millis() > lastRing + randTime && digitalRead(onhook) == HIGH){
      // ring the phone a few times
      for (int x = 0; x < 15; x++) {     //ring for 2 secs
        digitalWrite(ringerPin, HIGH);   // turn the solenoid on (HIGH is the voltage level)
        delay(50);                       // wait for 50 msec
        digitalWrite(ringerPin, LOW);    // turn the solenoid off by making the voltage LOW
        delay(80);                       // wait for 80 msec
      }
      delay(2500);
      ringCount++; // used to stop phone from ringing forever unless answered
    }
      if(ringCount > maxCount) {
        // reset next time to ring phone if not answered soon enough
        ringCount = 0;
        lastRing = millis();
        randTime = random(30000, 1200000); // generate a random number between 1 min and 60 min = 3600000
      }
        if(ringCount < maxCount && digitalRead(onhook) == LOW) {
        // reset next time to ring phone if not answered soon enough
        MP3player.playTrack(1);
        ringCount = 0;
        lastRing = millis();
        randTime = random(30000, 1200000); // generate a random number between 1 min and 60 min = 3600000
      }
       
    }
  else // phone has been picked up, aka offhook
  {
    // play some tune
    MP3player.playTrack(2);
    //put above code here
    // now reset randome time for ringing
    lastRing = millis();
    randTime = random(30000, 1200000); // generate a random number between 1 min and 60 min
  }
}
and again. thank you for your excellent help.
:D :clap: :dance: :violin: :P 8)
By MICyborg
#176974
ok. it was still wrong.

this is it. :D
Code: Select all
#include <SPI.h>            // To talk to the SD card and MP3 chip
#include <SdFat.h>          // SD card file system
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>   // MP3 decoder chip

//name the pins, elapsed time and output. pin1 = A0, pin2 = A4, pin3 = A5, pin4=1, pin5 = 0
const int ringerPin = A0; // TRIG1 is a free pin
const int onhook = A4;     // TRIG4 is not normally used when not FTDI'ing
const int maxCount = 6;  // ring phone this number of times before stopping
int ringCount = 0;        // variable to count number of unanswered rings
unsigned long lastRing;   // time of last ring start
unsigned long randTime;   // a random time btw 1 and 60 mins
unsigned long nextRing; // time the next ring will start 

const uint8_t volume = 0; // MP3 Player volume 0=max, 255=lowest (off)
const uint16_t monoMode = 1;  // Mono setting 0=off, 3=max

// Create library objects:
SFEMP3Shield MP3player;
SdFat sd;

void setup() {
  pinMode(ringerPin, OUTPUT);     // connect to transistor driver
  pinMode(onhook, INPUT_PULLUP);  //connect to NC switch, SW = open when onhook
  randomSeed(analogRead(0));      // this starts Arduino's random number generator

  //start the shield
  sd.begin(SD_SEL, SPI_HALF_SPEED);
  MP3player.begin();
}


//ring every 1 to 60 minutes if the phone is down (onhook, arduino gnd and digitalpin connected
//and dont ring if the phone is picked up (no onhook)
void loop()
{
  if (digitalRead(onhook) == HIGH) { // phone is hung up, aka onhook
    // kill any tune that is playing
    MP3player.stopTrack();
    //put above code here
    // check to see if it's time to ring the phone
    if(millis() > nextRing + randTime){ //onhook is probably still LOW here
      // ring the phone a few times
      for (int x = 0; x < 15; x++) { //ring for 2 secs
        if(digitalRead(onhook) == LOW){ //if hook was taken off while ringing, quit function and start loop() over with else branch
          goto pickoffstate;
        }
        digitalWrite(ringerPin, HIGH); // turn the solenoid on (HIGH is the voltage level)
        delay(50); // wait for 50 msec
        digitalWrite(ringerPin, LOW); // turn the solenoid off by making the voltage LOW
        delay(80); // wait for 80 msec
      }
      nextRing = millis() + 2500; // phone will ring next after 2500 ms, if hook is taken off in the meantime, it will play the track instantly.
      ringCount++; // used to stop phone from ringing forever unless answered
    }
    if(ringCount > maxCount) {
      // reset next time to ring phone if not answered soon enough
      ringCount = 0;
      nextRing = millis() + random(10000, 500000);//after ringCount surpassed maxCount, phone won´t ring again for 1 to 60 min
    } 

  }
  else // phone has been picked up, aka offhook
  {
      pickoffstate: ;
    if(ringCount < maxCount && ringCount != 0) {
      // reset next time to ring phone if not answered soon enough
      MP3player.playTrack(1);
      ringCount = 0;

    }
    else{
      // play some tune
      MP3player.playTrack(2);
      //put above code here
      // now reset randome time for ringing

    }
    lastRing = millis();
    randTime = random(10000, 500000); // generate a random number between 1 min and 60 min = 3600000
  } 
}