So its been few days and I really love my WAV Trigger! I manage to integrate buttons and led with it to start track and to indicate which track are playing.
I have been written a code that count the number of track that currently are playing and change the gain for each track based of the number of tracks.
I’m using the function: “wTrig.trackFade(int t, int gain, int time, bool stopFlag)”
It is indeed changing the track gain to whatever value I specify, but the fade time doesn’t work! instead of fading each track to the value I decided, it change the gain immediately! (the section of the ‘wTrig.trackFade’ is at the end lines of the code)
here is my full code:
#include <wavTrigger.h>
/* Arduino Mega Serial port to use: Serial Port 3:
Pin 14: TX
Pin 15: RX
*/
// Buttons //
const int buttonPins[] = {7,3,4,5,6}; //array of buttons pins
const int numOfButtons = (sizeof(buttonPins) / sizeof(buttonPins[0])); // size of push buttons
// Track Lengths //
// formula for milliseconds = (minutes * 60 + seconds) * 1000 = milliseconds //
const unsigned long trackLength[] = {71000,57000,65000,60000,85000};
// Leds //
const int ledPins[] ={30,31,32,33,54}; // array of leds pins
const int numOfLeds = (sizeof(ledPins) / sizeof(ledPins[0])); // size of Leds
// array to store the time when button is pressed //
unsigned long timeButtonPressed[numOfButtons];
int counter = 0;
const int fadeTime = 5000;
bool buttonState[numOfButtons];
// WAV Trigger object //
wavTrigger wTrig;
//track number//
int gNumTracks;
// firmware version//
char gWTrigVersion[VERSION_STRING_LEN];
//// potentiomer //
//int potPin = A7;
//int potValue = 0;
//------------------------------------------//
void setup() {
// starting a commuincation with the serial port //
Serial.begin(9600);
// setting up the digital pins for the buttons to be an input //
for (int i = 0; i < numOfButtons; i++)
{
pinMode(buttonPins[i], INPUT);
}
// setting up the digital pins for the leds to be an output //
for (int j = 0; j < numOfLeds; j++)
{
pinMode(ledPins[j], OUTPUT);
}
// intial button state to 0 //
for (int i = 0; i < numOfButtons; i++)
{
buttonState[i] = 0;
}
// intial button pressed time to the length of each audio file //
for (int i = 0; i < numOfButtons; i++)
{
timeButtonPressed[i] = trackLength[i];
}
delay(1000);
// WAV Trigger startup at 57600
wTrig.start();
delay(10);
// Send a stop-all command and reset the sample-rate offset, in case we have
// reset while the WAV Trigger was already playing.
wTrig.stopAllTracks();
wTrig.samplerateOffset(0);
// Enable track reporting from the WAV Trigger
wTrig.setReporting(true);
delay(1000);
/* checking bi-directional communication. should be able
to return the version of the board and number of tracks on the SD card.*/
if (wTrig.getVersion(gWTrigVersion, VERSION_STRING_LEN))
{
Serial.print(gWTrigVersion);
Serial.print("\n");
gNumTracks = wTrig.getNumTracks();
Serial.print("Number of tracks = ");
Serial.print(gNumTracks);
Serial.print("\n");
}
else
Serial.print("WAV Trigger response not available\n");
Serial.print("\n");
Serial.print("Num of Buttons: ");
Serial.println(numOfButtons);
Serial.print("\n");
Serial.print("Num of Leds: ");
Serial.println(numOfLeds);
Serial.print("\n");
// print the length of each track //
for (int i = 0;i < numOfButtons; i++)
{
Serial.print("The Length of track number ");
Serial.print(i+1);
Serial.print(" is: ");
Serial.print(trackLength[i]);
Serial.println(" Milliseconds");
}
}
//------ loop ------//
void loop()
{
wTrig.update();
// set master gain to -12db;
wTrig.masterGain(-12);
//
// // pitch offset //
// potValue = analogRead(potPin);
// potValue = map(potValue, 0, 1023, -32767, 32676);
// wTrig.samplerateOffset(potValue);
for (int i = 0; i < numOfButtons; i++)
{
for (int j = 0; j < numOfButtons; j++)
{
if (digitalRead(buttonPins[i]) == HIGH)
{
if(millis() - timeButtonPressed[i] >= trackLength[i])
{
timeButtonPressed[i] = millis(); //storing the time of button pressed
wTrig.trackPlayPoly(i+1); //play track number i
Serial.print("Track number ");
Serial.print(i+1);
Serial.println(" is playing");
Serial.println("");
digitalWrite(ledPins[i],HIGH); // turn on led corresponding to the track that is playing
buttonState[i] = 1; // store 1 to indicate that the track number i is currently playing
counter++; // count how many track playing
Serial.print("Number of tracks playing: ");
Serial.print(counter);
Serial.println("\n");
}
}
}
if (millis() > timeButtonPressed[i] + trackLength[i]) // check if the track number i is finished playing
{
digitalWrite(ledPins[i],LOW); // if so - turn off led
if (millis() > timeButtonPressed[i] + trackLength[i] && buttonState[i] == 1) // check if track number i that finisihed was previusly on
{
counter--; // if so - reduce 1 from counting
buttonState[i] = 0; //store 0 for that button state at track index i
Serial.print("Track number ");
Serial.print(i+1);
Serial.println(" stopped");
Serial.println("");
Serial.print("Number of tracks playing: ");
Serial.print(counter);
Serial.println("\n");
}
}
if (counter == 1)
{
wTrig.trackFade(i+1, 0, fadeTime, 0);
}
else if (counter >= 2 && counter <=3)
{
wTrig.trackFade(i+1, -12, fadeTime, 0);
}
else if(counter >= 4 && counter <=5)
{
wTrig.trackFade(i+1, -24, fadeTime, 0);
}
else if(counter >= 6 && counter <=7)
{
wTrig.trackFade(i+1, -18, fadeTime, 0);
}
else if(counter >= 8 && counter <=12)
{
wTrig.trackFade(i+1, -24, fadeTime, 0);
}
else if(counter >= 13 && counter <=14)
{
wTrig.trackFade(i+1, -36, fadeTime, 0);
}
}
}
Any help will be much appreciate!
Thanks