/*
Who's Next?
*/
//Define pins here
//Define 6 pins
int F1button = 2;
int F2button = 3;
int F3button = 4;
//Define variables to hold outr time in/out
int F1timeIn = 0;
int F2timeIn = 1;
int F3timeIn = 2;
int counter = 3;
// variable called not avilable which adds 10,000 to the count assigned to time in variable to bump it down the list
int NotAvailable = 10000;
//boolean variable to hold the argument, is F out?
boolean F1out = false;
boolean F2out = false;
boolean F3out = false;
//Create an array and load it up!
int sortValues[4] = { NotAvailable, F1timeIn, F2timeIn, F3timeIn };
void setup(){
//setup here
//sets our button pins as inputs
pinMode(F1button, INPUT);
pinMode(F2button, INPUT);
pinMode(F3button, INPUT);
//turn on the internal pullup resistors
digitalWrite(F1button, HIGH);
digitalWrite(F2button, HIGH);
digitalWrite(F3button, HIGH);
//create a string with the value of NotAvailable as the name
char NotAvailable[ ] = "Not Available";
Serial.begin(115200); //open up a serial port for debugging and messages
delay(1000);
Serial.println("Turn order"); //warm up serial port
}//end of setup
void loop(){
//Handle the button pushes
//F1 Button Handling
if (digitalRead(F1button) == LOW){ //if F1 button is pressed
if(F1out == false){ //and if F1 is in
F1timeIn = (counter + NotAvailable); // then make F1 time in equal to counter plus the not available constant
counter ++; //add one to the counter ready for when the button is next pushed
F1out = true; //make a note that F1 is now out
delay(2000); // wait 2 seconds to prevent accidental button pushes
}else{ //and if F1 was out
F1timeIn = (counter); //give the variable F1time in a number based on count (like picking a number and waiting in a queue
counter ++; // incriment the counter ready for the next button push
F1out = false; // F1 is now in so the statement becomes false
delay(2000); // again wait 2 sec to prevent accidental button pushes
} //end of if else
} //end of F1 button handling statement
//F2 Button Handling
if (digitalRead(F2button) == LOW){ //if F2 button is pressed
if(F2out == false){ //and if F2 is in
F2timeIn = (counter + NotAvailable); // then make F2 time in equal to counter plus the not available constant
counter ++; //add one to the counter ready for when the button is next pushed
F2out = true; //make a note that F2 is now out
delay(2000); // wait 2 seconds to prevent accidental button pushes
}else{ //and if F2 was out
F2timeIn = (counter); //give the variable F1time in a number based on count (like picking a number and waiting in a queue
counter ++; // incriment the counter ready for the next button push
F2out = false; // F2 is now in so the statement becomes false
delay(2000); // again wait 2 sec to prevent accidental button pushes
} //end of if else
} //end of F2 button handling statement
//F3 Button Handling
if (digitalRead(F3button) == LOW){ //if F3 button is pressed
if(F3out == false){ //and if F3 is in
F3timeIn = (counter + NotAvailable); // then make F3 time in equal to counter plus the not available constant
counter ++; //add one to the counter ready for when the button is next pushed
F3out = true; //make a note that F3 is now out
delay(2000); // wait 2 seconds to prevent accidental button pushes
}else{ //and if F3 was out
F3timeIn = (counter); //give the variable F1time in a number based on count (like picking a number and waiting in a queue
counter ++; // incriment the counter ready for the next button push
F3out = false; // F3 is now in so the statement becomes false
delay(2000); // again wait 2 sec to prevent accidental button pushes
} //end of if else
} //end of F3 button handling statement
//Write the latest values into the array
sortValues[0] = NotAvailable;
sortValues[1] = F1timeIn;
sortValues[2] = F2timeIn;
sortValues[3] = F3timeIn;
//sort the order using the 'sort' routine
sort (sortValues,4);
Serial.println("The order is;");
Serial.println();
//Line 1
if (sortValues[0] == F1timeIn){
Serial.println("F1");
}else if (sortValues[0] == F2timeIn){
Serial.println("F2");
}else if (sortValues[0] == F3timeIn){
Serial.println("F3");
}else if (sortValues[0] == NotAvailable){
Serial.println("NotAvailable");
}
//Line 2
if (sortValues[1] == F1timeIn){
Serial.println("F1");
}else if (sortValues[1] == F2timeIn){
Serial.println("F2");
}else if (sortValues[1] == F3timeIn){
Serial.println("F3");
}else if (sortValues[1] == NotAvailable){
Serial.println("NotAvailable");
}
//Line 3
if (sortValues[2] == F1timeIn){
Serial.println("F1");
}else if (sortValues[2] == F2timeIn){
Serial.println("F2");
}else if (sortValues[2] == F3timeIn){
Serial.println("F3");
}else if (sortValues[2] == NotAvailable){
Serial.println("NotAvailable");
}
//Line 4
if (sortValues[3] == F1timeIn){
Serial.println("F1");
}else if (sortValues[3] == F2timeIn){
Serial.println("F2");
}else if (sortValues[3] == F3timeIn){
Serial.println("F3");
}else if (sortValues[3] == NotAvailable){
Serial.println("NotAvailable");
}
Serial.println();
Serial.println();
delay(2000);
}// end of loop
//Sorting routine (http://www.hackshed.co.uk/arduino-sorting-array-integers-with-a-bubble-sort-algorithm/)
//sorts the array in order of smallest first
void sort(int a[], int size) {
for(int i=0; i<(size-1); i++) {
for(int o=0; o<(size-(i+1)); o++) {
if(a[o] > a[o+1]) {
int t = a[o];
a[o] = a[o+1];
a[o+1] = t;
}
}
}
}
The program now does what i want it to, but because its in a loop it keeps printing out to the serial over and over.
Its pretty much there, just needs some optimization.
Paul i hope you can see what ive done with "assigning a ticket" so to speak. I probably didnt explain what i meant that well.
I'm now working on how i can write it so that the button code is universal for all buttons but just subs in the values that are relevant. Otherwise once ive loaded the full 6 buttons it'll look pretty clumsy! plus ill learn more in the process.