I hope it is not too unpolite to past code and ask for debugging, but this is the situation: the two interrupts I have used in this code seem not to work. What’s wrong with this code and in particular with my use of interrupts?
It is a game. The interrupts are used to react when interrupt0 input goes down (coin inserted) and then when interrupt1 goes up. The code is for an arduino nano.
#include <SD.h> // need to include the SD library
#define SD_ChipSelectPin 10 //using digital pin 4 on arduino nano 328 //it seems I can use every pin except 11,12,13
#include <TMRpcm.h> // also need to include this library...
#include <SPI.h>
const byte motorEnablePin = 12; //pin 13 internally attached to the led, thus its output can't reach 5V. 12 is a better choice
const byte coinPin = 2; // = pin D2 = INT0 (digitalPinToInterrupt not working)
const byte winPin = 3; // = pin D3 = INT1 (digitalPinToInterrupt not working)
const byte audioPin = 9; //11 on Mega, 9 on Uno, Nano, etc
const unsigned long maxrelaxtime = 45; //duration of the soft background music...
const unsigned long maxtotaltime = 60; //total time of the game. NB: maxtotaltime>maxrelaxtime!
const byte numberOfAudioFiles = 5; //number of win AND lose files
volatile boolean coin = false;
volatile boolean win = false;
boolean ensia = false;
TMRpcm tmrpcm; // create a sound object
int whatsound;
unsigned long starttime;
char filename[20];
//you need the following music files to be present in the SD card:
//win0.wav, win1.wav ... (numberOfAudioFiles files)
//lose0.wav, lose1.wav ... (numberOfAudioFiles files)
//normal.wav (the normal background music)
//ensia.wav (the high tension background music)
//on.wav (the starting sound)
//for a total amount of 2*numberOfAudioFiles+3 audio files
void setup(){
pinMode(motorEnablePin, OUTPUT);
pinMode(coinPin, INPUT_PULLUP);
pinMode(winPin, INPUT_PULLUP);
attachInterrupt(0, coinDetected, FALLING);
attachInterrupt(1, victoryDetected, RISING);
tmrpcm.speakerPin = audioPin;
if (!SD.begin(SD_ChipSelectPin)) { // see if the card is present and can be initialized:
return; // don't do anything more if not
}
tmrpcm.volume(1);
tmrpcm.play("on.wav"); //the sound file "1" will play each time the arduino powers up, or is reset
}
void loop(){
if (coin) { //if a coin has been inserted
coin = false;
win = false;
ensia = false;
starttime = millis();
whatsound = random(numberOfAudioFiles); //returns a random number between 0 and numberOfAudioFiles-1 (both included)
tmrpcm.play("normal.wav"); //background music
while (((millis() - starttime) <= maxtotaltime) && (!win)) {// do this loop for up to maxtotaltime
if (win) { //if a candy has been recovered
strcpy(filename,"win");
sprintf(filename,"%d", whatsound);
strcat(filename,".wav");
tmrpcm.play(filename); //the sound of victory
} else {
if (((millis() - starttime) <= maxrelaxtime) && (!ensia)) {
ensia = true;
tmrpcm.play("ensia.wav");//background music for the last seconds
}
}
}
strcpy(filename,"lose");
sprintf(filename,"%d", whatsound);
strcat(filename,".wav");
tmrpcm.play(filename); //the sound of defeat
}
}
void coinDetected() {
coin = true;
}
void victoryDetected() {
win = true;
}