Ok, I cleaned it up with for loops. However it is still stuck in the record function only iterating the while loop once. Also once you press the buttons that turn on the outputs they only light up once. I think its where I'm trying to implement the debounce. I've been trying to sort this out all day and have had no luck. I hope its easier to follow now. Here's the code:
int button[] = {
2,3,4,5};
int trig1= 6;
int rec = 7;
int LEDoutput[] = {
8,9,10,11};
int pla_lite = 12;
int rec_lite = 13;
bool show_pres = false;
unsigned long show_seq[100][3];
int rButState;
int tButState;
int buttonState[4];
void setup(){
//pinMode config
pinMode(rec,INPUT);
pinMode(trig1,INPUT);
for (int i = 0; i < 5; i++){
pinMode(button[i],INPUT);
}
for (int i = 0; i < 5; i++){
pinMode(LEDoutput[i],OUTPUT);
}
pinMode(rec_lite,OUTPUT);
pinMode(pla_lite,OUTPUT);
//read initial button states
rButState = digitalRead(rec);
tButState = digitalRead(trig1);
for (int i = 0; i < 5; i++){
buttonState[i] = digitalRead(button[i]);
}
}
void loop(){
int rval; //
int rval2;
int tval;
int tval2;
for (int i = 0; i < 5; i++){ //Make sure outputs are low
digitalWrite(LEDoutput[i], LOW);
}
tval = digitalRead(trig1); //Read Trigger State
delay(10); //Debounce
tval2 = digitalRead(trig1); //
if (tval == tval2 && tval == LOW && tval != tButState && show_pres == true){ //Trigger show if one is recorded
playShow();
tButState = tval;
}
rval = digitalRead(rec); //Read REC button state
delay(10); //Debounce
rval2 = digitalRead(rec); //
if (rval == rval2 && rval == LOW && rval != rButState){ //Record show
recordShow();
rButState = rval;
}
}
void recordShow(){ //Record show function
int buttonVal[4];
int buttonVal2[4];
int rval;
int rval2;
unsigned long start_time = millis(); //Log time recording starts
bool isrec = true;
memset(show_seq, 0, sizeof(show_seq[0][0])*100*3); //Clear show
int counter = 0;
rButState = digitalRead(rec);
digitalWrite(rec_lite,HIGH); //Turn on REC light
while (isrec == true){ //Keep looping until record is pressed again
for (int i = 0; i <5; i++){
buttonVal[i] = digitalRead(button[i]); //Debounce
delay(10);
buttonVal2[i] = digitalRead(button[i]);
if (buttonVal[i] == buttonVal2[i] && buttonVal[i] == LOW && buttonVal[i] != buttonState[i]){ //Is button pressed?
digitalWrite(LEDoutput[i], HIGH); //Echo button press on output
show_seq[counter][1] = LEDoutput[i]; //Register the button pressed in 1st column
if (counter == 0){
show_seq[counter][2] = millis() - start_time; //Register time until button is pressed from start if first button press
}
else{
show_seq[counter][2] = millis() - show_seq[counter-1][3]; //Register time since last button press
}
while (digitalRead(button[i])==0){ //Wait until button is released
delay(10);
}
show_seq[counter][3] = millis() - show_seq[counter][2]; //Register time button is held
digitalWrite(LEDoutput[i], LOW);
counter++;
buttonState[i] = buttonVal[i];
}
rval = digitalRead(rec);
delay(10);
rval2 = digitalRead(rec);
if (rval == rval2 && rval == LOW && rval != rButState){ //Exit out of record
isrec = false;
rButState = rval;
}
}
}
digitalWrite(rec_lite,LOW);
}
void playShow(){ //Play the show function
int counter = 0;
digitalWrite(pla_lite,HIGH); //Turn on play light
while (show_seq[counter][1] > 0){ //Play show as long as there is a button value present
delay(show_seq[counter][2]);
digitalWrite(show_seq[counter][1], HIGH);
delay(show_seq[counter][3]);
digitalWrite(show_seq[counter][1], LOW);
counter++;
}
digitalWrite(pla_lite,LOW); //Turn off play light
}