Hi!
I just solved one of two problems so I edited my post... Now is the biggest problem left.
I have a Uno with a mp3-shield, V1053 from geeetech and a keypad.
The problem is that somehow the arduino seems to freeze when I press 1 or 2 on the keypad as the first button I press. As long as I start with any other digit I can press 1 or 2 without it freezes. Isn't that weird?
I am using the current keypad on the phone, which I dismembered for the project(thats why the key layout in the code looks a bit messy) and I first thought that could be the problem somehow but I've tried with other kaypads and this code and the arduino was still freezing up. So it must be something with this code?
It seems that all the analog pins and 10, 5, 4, 3 are free pins according to this v1053 info. So it shouldn't be any problems with using the analog pins and pin10, 5, 4, and 3 for the keypad, right?
What could cause this?!
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
#include <Bounce2.h>
#include <Keypad.h>
int currentLength = 0; //defines which number we are currently writing
int luronoff = 1;
char entered[5];
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'2', 'A', '3', '1'},
{'5', '6', 'B', '4'},
{'7', '9', '8', '7'},
{'*', 'D', '0', '*'}
};
byte rowPins[ROWS] = {10, 5, 4, 3};
byte colPins[COLS] = {A0, A1, A2, A3};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define B_STOP A5
#define BUTTON_DEBOUNCE_PERIOD 20 //ms
SdFat sd;
SFEMP3Shield MP3player;
Bounce b_Stop = Bounce();
void setup() {
Serial.begin(9600);
pinMode(B_STOP, INPUT_PULLUP);
b_Stop.attach(B_STOP);
// b_Stop.interval(BUTTON_DEBOUNCE_PERIOD);
if (!sd.begin(9, SPI_HALF_SPEED)) sd.initErrorHalt();
if (!sd.chdir("/")) sd.errorHalt("sd.chdir");
MP3player.begin();
MP3player.setMonoMode(1);
MP3player.setVolume(10, 10);
Serial.println("Waiting");
}
void loop()
{
// Below is only needed if not interrupt driven. Safe to remove if not using.
#if defined(USE_MP3_REFILL_MEANS) \
&& ( (USE_MP3_REFILL_MEANS == USE_MP3_SimpleTimer) \
|| (USE_MP3_REFILL_MEANS == USE_MP3_Polled) )
MP3player.available();
#endif
if (b_Stop.update() ) {
if (b_Stop.read() == HIGH ) { //handset picked up
Serial.print("Handset up ");
MP3player.playTrack(4); //dial tone
luronoff = 1;
Serial.println("Waiting for number");
char key2 = keypad.getKey();
while (currentLength < 5 ) // || luronoff == 1) //exit while if 5 numbers inserted OR hand set is hung up
{
if (b_Stop.update() ) {
if (b_Stop.read() == LOW ) { //handset put down
break;
}
}
char key2 = keypad.getKey();
if (key2 != NO_KEY)
{
Serial.print("Pressed button: ");
Serial.println(key2);
MP3player.stopTrack();
entered[currentLength] = key2;
currentLength++;
}
}
//else if (b_Stop.read() == LOW ) { //handset picked up
// Serial.println("Hang up2");
// MP3player.stopTrack();
// currentLength = 0;
//}
if (currentLength == 5)
{
if (entered[0] == '5' && entered[1] == '4' && entered[2] == '1' && entered[3] == '7' && entered[4] == '5')
{
MP3player.playTrack(1);
MP3player.playTrack(1); //Somehow sometimes it doesn't work with only one .playTrack. But it works with two
Serial.println("Swedish");
currentLength = 0;
}
else
{
Serial.println("Wrong number");
MP3player.playTrack(3);
MP3player.playTrack(3); //Somehow sometimes it doesn't work with only one .playTrack. But it works with two
currentLength = 0;
}
}
}
if (b_Stop.read() == LOW ) { //handset put down
Serial.println("Hang up2 ");
MP3player.stopTrack();
currentLength = 0;
}
}
}