// The target function returns the time taken for the target switch to be triggered unsigned long target(){ int targetnumber = random(4); // Generates a number between 0 and 4 int friendorfoe = random(10);// Generates a number either 0 or 1 (0 for enemy, 1 for friend) if (friendorfoe != 1){ friendorfoe = 0; } int targetval = 0; // Value used as standard input for target int current = 10000; unsigned long timebegin = 0; // Using the targetnumber taken a target is selected switch(targetnumber) { case 0 : digitalWrite(latchtargetpin, LOW); if (friendorfoe == 0) { // Lights up the correct LED by shifting a one out to the correct shift register pin shiftOut(datapin, clockpin, LSBFIRST, B10000000); } else { shiftOut(datapin, clockpin, LSBFIRST, B01000000); } digitalWrite(latchtargetpin, HIGH); timebegin = millis(); // Records down the time at which the target came on while( (targetval == 0) && (friendorfoe == 0) ){ // Get the enemy in your sites and get shooting targetval = digitalRead(targetzeropin); } if( (targetval == 0) && (friendorfoe == 1) ){ // Only runs for friend while( (( millis() - timebegin ) < 10000) && (targetval == 0) ){ // Shows friend LED for 10 seconds targetval = digitalRead(targetzeropin); // Checks to make sure you havent hit it //current = millis(); } if( targetval == 1){ // Low and behold you hit it, your going to jail buzzer(3); // Plays a sound for your FAIL unsigned long timetaken = 1; // Sets time to 1 // This clears the shift register turning off all LEDs digitalWrite(latchtargetpin, LOW); shiftOut(datapin, clockpin, LSBFIRST, B00000000); digitalWrite(latchtargetpin, HIGH); return(timetaken); // Breaks out of the target function and returns the time } else { // You didnt hit it, well done buzzer(2); // Plays a standard hit sound signifying you passed unsigned long timetaken = 0; // Sets time to 1 // This clears the shift register turning off all LEDs digitalWrite(latchtargetpin, LOW); shiftOut(datapin, clockpin, LSBFIRST, B00000000); digitalWrite(latchtargetpin, HIGH); return(timetaken);// Breaks out of the target function and returns the time } } break; case 1 : digitalWrite(latchtargetpin, LOW); if (friendorfoe == 0) { shiftOut(datapin, clockpin, LSBFIRST, B00100000); } else { shiftOut(datapin, clockpin, LSBFIRST, B00010000); } digitalWrite(latchtargetpin, HIGH); timebegin = millis(); // Records down the time at which the target came on while( (targetval == 0) && (friendorfoe == 0) ){ // Get the enemy in your sites and get shooting targetval = digitalRead(targetonepin); } if( (targetval == 0) && (friendorfoe == 1) ){ // Only runs for friend while( (( millis() - timebegin ) < 10000) && (targetval == 0) ){ // Shows friend LED for 10 seconds targetval = digitalRead(targetzeropin); // Checks to make sure you havent hit it //current = millis(); } if( targetval == 1){ // Low and behold you hit it, your going to jail buzzer(3); // Plays a sound for your FAIL unsigned long timetaken = 1; // Sets time to 1 // This clears the shift register turning off all LEDs digitalWrite(latchtargetpin, LOW); shiftOut(datapin, clockpin, LSBFIRST, B00000000); digitalWrite(latchtargetpin, HIGH); return(timetaken); // Breaks out of the target function and returns the time } else { // You didnt hit it, well done buzzer(2); // Plays a standard hit sound signifying you passed unsigned long timetaken = 0; // Sets time to 1 // This clears the shift register turning off all LEDs digitalWrite(latchtargetpin, LOW); shiftOut(datapin, clockpin, LSBFIRST, B00000000); digitalWrite(latchtargetpin, HIGH); return(timetaken);// Breaks out of the target function and returns the time } } break; case 2 : digitalWrite(latchtargetpin, LOW); if (friendorfoe == 0) { shiftOut(datapin, clockpin, LSBFIRST, B00001000); } else { shiftOut(datapin, clockpin, LSBFIRST, B00000100); } digitalWrite(latchtargetpin, HIGH); timebegin = millis(); // Records down the time at which the target came on while( (targetval == 0) && (friendorfoe == 0) ){ // Get the enemy in your sites and get shooting targetval = digitalRead(targettwopin); } if( (targetval == 0) && (friendorfoe == 1) ){ // Only runs for friend while( (( millis() - timebegin ) < 10000) && (targetval == 0) ){ // Shows friend LED for 10 seconds targetval = digitalRead(targetzeropin); // Checks to make sure you havent hit it //current = millis(); } if( targetval == 1){ // Low and behold you hit it, your going to jail buzzer(3); // Plays a sound for your FAIL unsigned long timetaken = 1; // Sets time to 1 // This clears the shift register turning off all LEDs digitalWrite(latchtargetpin, LOW); shiftOut(datapin, clockpin, LSBFIRST, B00000000); digitalWrite(latchtargetpin, HIGH); return(timetaken); // Breaks out of the target function and returns the time } else { // You didnt hit it, well done buzzer(2); // Plays a standard hit sound signifying you passed unsigned long timetaken = 0; // Sets time to 1 // This clears the shift register turning off all LEDs digitalWrite(latchtargetpin, LOW); shiftOut(datapin, clockpin, LSBFIRST, B00000000); digitalWrite(latchtargetpin, HIGH); return(timetaken);// Breaks out of the target function and returns the time } } break; case 3 : digitalWrite(latchtargetpin, LOW); if (friendorfoe == 0) { shiftOut(datapin, clockpin, LSBFIRST, B00000010); } else { shiftOut(datapin, clockpin, LSBFIRST, B00000001); } digitalWrite(latchtargetpin, HIGH); timebegin = millis(); // Records down the time at which the target came on while( (targetval == 0) && (friendorfoe == 0) ){ // Get the enemy in your sites and get shooting targetval = digitalRead(targetthreepin); } if( (targetval == 0) && (friendorfoe == 1) ){ // Only runs for friend while( (( millis() - timebegin ) < 10000) && (targetval == 0) ){ // Shows friend LED for 10 seconds targetval = digitalRead(targetzeropin); // Checks to make sure you havent hit it //current = millis(); } if( targetval == 1){ // Low and behold you hit it, your going to jail buzzer(3); // Plays a sound for your FAIL unsigned long timetaken = 1; // Sets time to 1 // This clears the shift register turning off all LEDs digitalWrite(latchtargetpin, LOW); shiftOut(datapin, clockpin, LSBFIRST, B00000000); digitalWrite(latchtargetpin, HIGH); return(timetaken); // Breaks out of the target function and returns the time } else { // You didnt hit it, well done buzzer(2); // Plays a standard hit sound signifying you passed unsigned long timetaken = 0; // Sets time to 1 // This clears the shift register turning off all LEDs digitalWrite(latchtargetpin, LOW); shiftOut(datapin, clockpin, LSBFIRST, B00000000); digitalWrite(latchtargetpin, HIGH); return(timetaken);// Breaks out of the target function and returns the time } } break; } buzzer(2); // Plays a standard hit sound signifying you passed // This clears the shift register turning off all LEDs digitalWrite(latchtargetpin, LOW); shiftOut(datapin, clockpin, LSBFIRST, B00000000); digitalWrite(latchtargetpin, HIGH); unsigned long timeend = millis(); unsigned long timetaken = timeend - timebegin; return(timetaken); }