Thanks for your answer. Here is my full code:
Code: Select all
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
// These are the pins used for the breakout example
#define BREAKOUT_RESET 9 // VS1053 reset pin (output)
#define BREAKOUT_CS 10 // VS1053 chip select pin (output)
#define BREAKOUT_DCS 8 // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET -1 // VS1053 reset pin (unused!)
#define SHIELD_CS 7 // VS1053 chip select pin (output)
#define SHIELD_DCS 6 // VS1053 Data/command select pin (output)
// These are common pins between breakout and shield
#define CARDCS 4 // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3 // VS1053 Data request, ideally an Interrupt pin
Adafruit_VS1053_FilePlayer musicPlayer =
// create breakout-example object!
//Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
// create shield-example object!
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS); // these are all variables state above
//pins for the task
const int pinDepart = 22; // pin departure detector
const int pinReward = 24; // pin arrival detector
const int pinPompe = 26; // pin arrival reward
const int pinPompe2 = 28; // pin departure reward
const int pinButton = 30; //pin button error (pressed when there is an error)
const int pinButtonporte = 32; //pin button departure door
const int pinBruit = 34; //pin light when there is an error (when the button is pressed)
// Door reward
const int stepPin = 31;
const int dirPin = 33;
const int enPin = 35;
// Door reward 2
const int stepPin2 = 37;
const int dirPin2 = 39;
const int enPin2 = 35;
// Declaring state variables
int s_a, e_a, b_a, s_p; //s_a : dectector departure activated, e_a : detector arrival activated
//s_p: door departure open, b_a : button error pressed
int state;
unsigned long int timing;
// Allowance variables
int sia, eia; //sia : detector departure can be activated, eia: detector arrival can be detected
// Pump variables
const float delayPump = 30; // ms
void setup() {
Serial.begin(9600);
musicPlayer.begin();
SD.begin(CARDCS);
musicPlayer.setVolume(40, 40);
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);
pinMode(pinDepart, INPUT);
pinMode(pinReward, INPUT);
pinMode(pinButton, INPUT_PULLUP);
pinMode(pinButtonporte, INPUT_PULLUP);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
pinMode(stepPin2, OUTPUT);
pinMode(dirPin2, OUTPUT);
pinMode(enPin2, OUTPUT);
pinMode(pinPompe, OUTPUT);
pinMode(pinPompe2, OUTPUT);
pinMode(pinBruit, OUTPUT);
digitalWrite(enPin, LOW);
digitalWrite(enPin2, LOW);
s_a = 0;
e_a = 0;
b_a = 0;
s_p = 0;
sia = 1;
eia = 0;
}
void release_reward(int delayPump, int pinPompe) {
delay(delayPump);
digitalWrite(pinPompe, HIGH);
delay(delayPump / 2);
digitalWrite(pinPompe, LOW);
}
void release_reward_loop(int delayPump, int pinPompe, int n_releases) {
int i;
for (i = 0; i < n_releases; i++) {
release_reward(delayPump, pinPompe);
}
}
void move_door(int closedoor, int dirPin, int stepPin, int port) {
int d = 50;
if (port == 0) {
d = 55;
}
int msg = HIGH;
if (closedoor) {
msg = LOW;
}
for (int x = 0; x < 16.4 * 1600; x++) {
digitalWrite(dirPin, msg); // Enables the motor to move in a particular direction
digitalWrite(stepPin, HIGH);
delayMicroseconds(d);
digitalWrite(stepPin, LOW);
delayMicroseconds(d);
}
}
void loop() {
if (s_p == 0) {
if (digitalRead(pinButtonporte) == HIGH) { //If I opened the door then:
move_door(0, dirPin2, stepPin2, 0);
timing = millis();
Serial.print("Detecteur_depart_aller, debut trial");
Serial.print(",");
Serial.println(timing);
s_p = 1; //door is open
s_a = 1; //departure detector has been activated
eia = 1; // arrival detector can be detected
sia = 0; // departure detector cannot be detected anymore
}
} else {
if (b_a == 0) { //if no error
state = digitalRead(pinReward);
if (state == LOW) { //if arrival detector activated
if (s_a == 1 && e_a == 0 && eia == 1) {
timing = millis();
Serial.print("Detecteur_arrivee, fin aller");
Serial.print(",");
Serial.println(timing);
s_a = 1;
e_a = 1;
eia = 0;
sia = 1;
musicPlayer.startPlayingFile("rewardsd.wav"); //a sound cause the trial is correct
move_door(1, dirPin, stepPin, 1); //door is moving
delay(2000);
release_reward_loop(delayPump, pinPompe, 10); //reward is released
delay(1000);
move_door(0, dirPin, stepPin, 1); //door is moving again
timing = millis();
Serial.print("Detecteur_arrivee, debut retour");
Serial.print(",");
Serial.println(timing);
}
}
state = digitalRead(pinDepart);
if (state == LOW) { //if departure dectector is activated
if (s_a == 1 && e_a == 1 && sia == 1) { // if the task has been completed :
s_a = 0;
e_a = 0;
timing = millis();
Serial.print("Detecteur_depart_retour, fin trial");
Serial.print(",");
Serial.println(timing);
move_door(1, dirPin2, stepPin2, 0); //door is closing
delay(1000);
release_reward_loop(delayPump, pinPompe2, 10); //reward
s_p = 0; //door closed
}
}
if (digitalRead(pinButton) == HIGH) { //if there is an error (I pressed the button)
b_a = 1; //button is pressed
digitalWrite(pinBruit, HIGH); //light is ON
timing = millis();
Serial.print("Debut erreur");
Serial.print(",");
Serial.println(timing);
}
} else if (b_a == 1) { //if button is active
eia = 0; // arrival detector can't be detected anymore
state = digitalRead(pinDepart);
if (state == HIGH) {
musicPlayer.startPlayingFile("errorsnd.wav"); //play error sound until detector is low 1 time
} else if (state == LOW) { // if the depature detector has been activated once
musicPlayer.stopPlaying(); //stop the sound
digitalWrite(pinBruit, LOW); //the light is OFF now
timing = millis();
Serial.print("end");
Serial.print(",");
Serial.println(timing);
move_door(1, dirPin2, stepPin2, 0); //door is closing
s_p = 0; //door is closed
sia = 1; //everything is set to do a new trial
s_a = 0;
b_a = 0;
}
}
}
}
When I press the button I have "Debut erreur" printed and then my sound is playing (in loop). But with my code when the departure detector are supposed to be detected (here is LOW) the sound is supposed to stop but it's not the case and nothing is printed, the "end" is not printed. I think there is a bug and my detector cannot be detected right if the sound is playing! Cause if i let my hand in front of the detector at some point they can be detected, but this should not work like this. When I put my hand once it supposed to be detected and the sound is supposed to stop to allow the door to be closing.