Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
4.0 kB
1
Indexable
Never
#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[] = {5000,20000,65000,20000,60000};

// Leds //
const int ledPins[] ={30,31,32}; // 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 buttons_pressed[numOfButtons]; 

int counter = 0;

int buttonState[numOfButtons];

// WAV Trigger object //
wavTrigger wTrig;            

//track number//
int  gNumTracks;  

// firmware version//
char gWTrigVersion[VERSION_STRING_LEN]; 


//------------------------------------------//

 
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 0 //
  for (int i = 0; i < numOfButtons; i++)
    {
      buttons_pressed[i] = 0;
    }
 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");
  }
}
  
  
void loop()
{

  wTrig.update();
   
  for (int i = 0; i < numOfButtons; i++)
  {
    for (int j = 0; j < numOfButtons; j++)
    {
    if (digitalRead(buttonPins[i]) == HIGH)
      {
        if(millis()-buttons_pressed[i]>=trackLength[i])
        {
         buttons_pressed[i]=millis();//storing the time of button pressed in index i
         wTrig.trackPlayPoly(i+1); //play track number i poly
         Serial.print("track number: ");
         Serial.print(i+1);
         Serial.println(" Is playing");
         wTrig.update();
         digitalWrite(ledPins[i],HIGH);
         buttonState[i] = 1;
         counter++;
         Serial.print("counter: ");
         Serial.println(counter);
          }
         }
      }
      if (millis() > buttons_pressed[i] + trackLength[i])
         {
            digitalWrite(ledPins[i],LOW);
         }
      if (millis() > buttons_pressed[i] + trackLength[i] && buttonState == 1)
      {
        counter--;
        buttonState[i] = 0;
        Serial.print("Counter: ");
        Serial.println(counter); 
      }
    }    
}