// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin1, OUTPUT);
digitalWrite(pingPin1, LOW);
delayMicroseconds(2);
digitalWrite(pingPin1, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin1, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin1, INPUT);
duration1 = pulseIn(pingPin1, HIGH);
// convert the time into a distance
inches1 = microsecondsToInches(duration1);
cm1 = microsecondsToCentimeters(duration1);

delay(50);

pinMode(pingPin2, OUTPUT);
digitalWrite(pingPin2, LOW);
delayMicroseconds(2);
digitalWrite(pingPin2, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin2, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin2, INPUT);
duration2 = pulseIn(pingPin2, HIGH);
// convert the time into a distance
inches2 = microsecondsToInches(duration2);
cm2 = microsecondsToCentimeters(duration2);

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
float microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2.0;
}

void clearVolumeOutput(){
//ground LATCH_PIN and hold low for as long as you are transmitting
digitalWrite(LATCH_PIN, 0);
//move 'em out
myShiftOut(DATA_PIN, CLOCK_PIN, 0);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(LATCH_PIN, 1);
delay(50);
}

//if the value passed to myDATA_OUT_PIN and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDATA_OUT_PIN & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}

//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDATA_PIN, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myCLOCK_PIN, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDATA_PIN, 0);
}

//stop shifting
digitalWrite(myCLOCK_PIN, 0);
}

//Prints data to the matrix
void printmat(int first,int second)
{
//Load colors into the first row color buffer array.
//This will be the array of colors sent to the RGB matrix.

//Activate the RGB Matrix
digitalWrite(SLAVESELECT, LOW);
//Send the color buffer to the RGB Matrix
for(int LED=0; LED<64; LED++){
spi_transfer(color_buffer_mix[LED]);
}
//Deactivate the RGB matrix.
digitalWrite(SLAVESELECT, HIGH);
//wait for 100ms
delay(100); // allow some time for the Serial data to be sent

}

//Use this command to send a single color value to the RGB matrix.
//NOTE: You must send 64 color values to the RGB matrix before it displays an image!
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
{
};
return SPDR; // return the received byte
}