// These constants won't change:
#include <TM1637.h> //Include the library for the 4 digit display
const int CLK = 11; // Set the clock pin for the 4 digit display
const int DIO = 12; // Set the digital input pin for the 4 digit display
//Create a virtual instance of the display
TM1637 tm(CLK, DIO);
const int NumberOfTargets = 2; //Set the number of targets
int TargetStatus[NumberOfTargets]; //Declare an array to hold the target status
int DetectorPins [NumberOfTargets]; // Declare an array to hold the pins for the shot detectors
int TotalTargetStatus;
//Set the detectorpins
DetectorPins = {A1,A2};
const int HitThreshold = 200; // an arbitrary threshold level that's in the range of the analog input
void setup() {
//Set the initial status of the targets (Set them all to 1 for all targets live initially)
for(int i=0;i<NumberOfTargets;i++){
TargetStatus[i] = 1;
}
// Initialise the 4 digit display to act as a score counter
tm.init();
tm.set(BRIGHT_TYPICAL);
///Show a countdown on the score counter
ScoreCounter=0003;
DisplayScore(ScoreCounter);
delay(1000); //Hold the score for 1 second
ScoreCounter=0002;
DisplayScore(ScoreCounter);
delay(1000); //Hold the score for 1 second
ScoreCounter=0001;
DisplayScore(ScoreCounter);
delay(1000); //Hold the score for 1 second
ScoreCounter=0000;
DisplayScore(ScoreCounter);
ScoreCounter=9999;
}
void loop() {
for(int i=0;i<NumberOfTargets;i++){
if ((analogRead(DetectorPins(i)))> HitThreshold){
TargetStatus(i) = 1;
}else{
}
delay(5); // delay in between reads for stability
}
TotalTargetStatus=0;
for(int i=0;i<NumberOfTargets;i++){
TotalTargetStatus = TotalTargetStatus + TargetStatus(i);
}
if (TotalTargetStatus==0){ //If all of the targets have been hit
// Display The Score
DisplayScore(ScoreCounter);
Target1Hit=false;
}else{
if (ScoreCounter>0) {
ScoreCounter=ScoreCounter-1;
}
else{
ScoreCounter = 0;
}
DisplayScore(ScoreCounter);
}
}
void DisplayScore(int ScoreToDisplay){
tm.display(0, (ScoreToDisplay / 1000) % 10);
tm.display(1, (ScoreToDisplay / 100) % 10);
tm.display(2, (ScoreToDisplay / 10) % 10);
tm.display(3, (ScoreToDisplay % 10));
}