// These constants won't change:
const int Target1DetectorPin = A1; // pin that the sensor for the first target is attached to
const int Target2DetectorPin = A2; // pin that the sensor for the second target is attached to
const int HitThreshold = 200; // an arbitrary threshold level that's in the range of the analog input
bool Target1Hit = false;
bool Target2Hit= false;
int ScoreCounter= 0;
#include <TM1637.h>
const int CLK = 11;
const int DIO = 12;
TM1637 tm(CLK, DIO);
void setup() {
// initialize serial communications:
Target1Hit = false;
Target2Hit = false;
tm.init();
tm.set(BRIGHT_TYPICAL);
///Start by resetting the score counter and displaying the score
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() {
// read the values from the detectors:
int Target1Value = analogRead(Target1DetectorPin);
int Target2Value = analogRead(Target2DetectorPin);
// if the analog value is high enough, turn on the LED:
if (Target1Value > HitThreshold) {
Target1Hit=true;
} else {
}
delay(5); // delay in between reads for stability
if (Target2Value > HitThreshold) {
Target2Hit=true;
} else {
}
if (Target1Hit==true and Target2Hit==true){ //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));
}