#include <SPI.h>    // include the SPI communcation library
// Connect pin#13 (SCLK) To S2 on AP23 
// Connect pin#11 (DATOUT) To S3 on AP23 
// Connect pin#13 (SCLK) To S2 on AP23 
// Connect pin#10 (CS) To S1 on AP23
// DO from Ap23 optional S4 - Not used here
#define cs 10          // this is the chip-select pin for SPI
#define cs2 9
#define cs3 8
#define cs4 7
int hold = 0;
int del=200;        // short 200ms delay
int value1 = 0x98;  // play command - This value never changes
int value2 = 0;  // voice address - when you place a hex value in this integer and call the "readout()" function, that specific word will play
int value3 = 0xA8;  // COUT ramp up - This value never changes
int value4 = 0xB8;   // COUT ramp down - This value never changes
// ALL OF THE ABOVE CODE IS REQUIRED FOR THE OPERATION OF THE LBT

void setup()  // Set up SPI
{
  SPI.begin();             // Initialize SPI
  SPI.setClockDivider(SPI_CLOCK_DIV32); // low frequency SPI
  pinMode(cs,OUTPUT);    // Chip select pins is an output
  pinMode(cs2,OUTPUT);    // Chip select pins is an output
  pinMode(cs3,OUTPUT);    // Chip select pins is an output
  pinMode(cs4,OUTPUT);    // Chip select pins is an output
  digitalWrite(cs,HIGH); // Set chip select to be HIGH (5v) by default.  The chip on the shield is selected when this line is brought low. 
  digitalWrite(cs2,HIGH); // Set chip select to be HIGH (5v) by default.  The chip on the shield is selected when this line is brought low. 
  digitalWrite(cs3,HIGH); // Set chip select to be HIGH (5v) by default.  The chip on the shield is selected when this line is brought low. 
  digitalWrite(cs4,HIGH); // Set chip select to be HIGH (5v) by default.  The chip on the shield is selected when this line is brought low. 
  SPI.setBitOrder(MSBFIRST);  // OTP requires MSB first
  SPI.setDataMode(SPI_MODE0);  // Use MODE0, as all other modes to not work
  value2 = 0x00; // Start at voice group 0
  delay(1000);   // One second delay
  // ramp up
  digitalWrite(cs,LOW);
  SPI.transfer(value3);
  SPI.transfer(0x00);
  digitalWrite(cs,HIGH);
  delay(5);
  digitalWrite(cs2,LOW);
  SPI.transfer(value3);
  SPI.transfer(0x00);
  digitalWrite(cs2,HIGH);
  delay(5);
   digitalWrite(cs3,LOW);
  SPI.transfer(value3);
  SPI.transfer(0x00);
  digitalWrite(cs3,HIGH);
  delay(5);
  digitalWrite(cs4,LOW);
  SPI.transfer(value3);
  SPI.transfer(0x00);
  digitalWrite(cs4,HIGH); 
}

void loop() // This loop tells the BBT to play one word from each of the four audio chips
{
  value2 = 0;
  check();
  value2 = 259;
  check();
  value2 = 512;
  check();
  value2 = 770;
  check();
}


void check() // This routine takes the number in value2 (Between 0-1015), and determines which chip to talk to, and then executes the word.  
// For more information on the below code, see the other code sample that plays ALL words.
{
  if(value2 < 254)
  {
  hold = value2;
  readout();
  value2 = value2 + 1;  // Increment voice command group
  }
else if (value2 < 508)
{
  hold = value2;
  hold = hold - 254;
  Serial.println("cs2");
  Serial.println(hold);
  readout2();
  value2 = value2 + 1;  // Increment voice command group
}
else if (value2 < 762)
{
  hold = value2;
  hold = hold - 508;
    Serial.println("cs3");
  Serial.println(hold);
  readout3();
  value2 = value2 + 1;  // Increment voice command group
}
else if (value2 < 1016)
{
  hold = value2;
  hold = hold - 762;
    Serial.println("cs4");
  Serial.println(hold);
  readout4();
  value2 = value2 + 1;  // Increment voice command group
}
}


void readout()
{
  digitalWrite(cs,LOW);
  SPI.transfer(value1);
  SPI.transfer(hold);
  digitalWrite(cs,HIGH);
  delay(850);
}

void readout2()
{
  digitalWrite(cs2,LOW);
  SPI.transfer(value1);
  SPI.transfer(hold);
  digitalWrite(cs2,HIGH);
  delay(850);
  }

void readout3()
{
  digitalWrite(cs3,LOW);
  SPI.transfer(value1);
  SPI.transfer(hold);
  digitalWrite(cs3,HIGH);
  delay(850);
  }

 void readout4()
 {
  digitalWrite(cs4,LOW);
  SPI.transfer(value1);
  SPI.transfer(hold);
  digitalWrite(cs4,HIGH);
  delay(850);
  }



   

   



