#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          // These are the chip-select pins for SPI. They control chips 1-4
#define cs2 9
#define cs3 8
#define cs4 7
int hold = 0;       // Integer used when calling words
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
  // The following code sets up the output of each chip.  
  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);
  value2 = 0;   // THIS STARTS THE POINTER AT WORD 0
}



void loop()             // IN THIS LOOP, ALL WORDS ARE PLAYED SEQUENTIALLY. VALUE2 INTEGER IS INCREMENTED AFTER EVERY WORD PLAYED.
{
  if(value2 < 254)      // If the number in VALUE2 integer is less than 254, then talk to chip#1
  {
  hold = value2;        // HOLD THE NUMBER IN VALUE2 IN THE HOLD INTEGER FOR THE READOUT FUNCTION
  readout();            // CALL readout() function.
  value2 = value2 + 1;  // Increment voice command group
  }
else if (value2 < 508)  // If the number in VALUE2 integer is less than 508, then talk to chip#2
{
  hold = value2;        
  hold = hold - 254;    // Subtract 254 from hold, so that we're sending a valid number to the correct chip
  readout2();
  value2 = value2 + 1;  
}
else if (value2 < 762)   // If the number in VALUE2 integer is less than 762, then talk to chip#3
{
  hold = value2;
  hold = hold - 508;     // Subtract 508 from hold, so that we're sending a valid number to the correct chip
  readout3();
  value2 = value2 + 1;  
}
else if (value2 < 1016)   // If the number in VALUE2 integer is less than 1016, then talk to chip#4
{
  hold = value2;
  hold = hold - 762;      // // Subtract 762 from hold, so that we're sending a valid number to the correct chip
  readout4();
  value2 = value2 + 1;  // Increment voice command group
}
}


void readout()
{
  digitalWrite(cs,LOW);   // Talk to chip#1
  SPI.transfer(value1);   // Seond command info
  SPI.transfer(hold);     // Call word number stored in hold
  digitalWrite(cs,HIGH);  // Stop talking to chip#1.  The word will be said by the BBT at this time
  delay(850);             // Delay of 850ms 
}

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);
  }



   

   



