I got this art project where an old phone is made up to ring randomly and if you pick it up it plays a audiofile and another one if you pick it up during not ringing time. That was a little tricky for me cuz i dont had a clue at all befor this project and i was a beginner. Now that it works i got a lot small informations in my head to make some easy projects and i am about it but i cant find the mistake in this one. It does its job only direct after upload and if u turn it off and on again, more than one time, it is just ringing but not playing an audiofile anymore. I got to upload it again to make it working again.
Maybe its an arduino related issue i thought.
#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();
// check to see if it's time to ring the phone
if(millis() > nextRing){ //onhook is probably still LOW here
// 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
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.
}
if(ringCount > maxCount) {
// reset next time to ring phone if not answered soon enough
ringCount = 0;
nextRing = millis() + random(10000, 1800000);//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) {
delay(2000);
MP3player.playTrack(2);
//in future i wona play a randomly choosen track out of 8 files numbered track002,003,004 - track009,
//i wona make this working after i know whats the issue now,
//why it only does its job direct after upload
//MP3player.playTrack(random(2, 9)); //not testet this code now
ringCount = 0;
nextRing = millis() + random(10000, 1800000);
}
else{
delay(1500);
MP3player.playTrack(1);
}
}
}
It would be nice if somebody could help me to make this project healthy