// 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 Target3DetectorPin = A3; // pin that the sensor for the third target is attached to
const int Target1IndicatorPin = 2; // pin that the indicator for the first target is attached to
const int Target2IndicatorPin = 3; // pin that the indicator for the second target is attached to
const int Target3IndicatorPin = 4; // pin that the indicator for the third target is attached to
const int BuzzerPin = 5; //pin that the buzzer 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;
bool Target3Hit= false;
int ScoreCounter= 0;
#include <TM1637.h>
const int CLK = 11;
const int DIO = 12;
TM1637 tm(CLK, DIO);
void setup() {
// initialize serial communications:
pinMode(Target1IndicatorPin, OUTPUT);
pinMode(Target2IndicatorPin, OUTPUT);
pinMode(Target3IndicatorPin, OUTPUT);
pinMode(BuzzerPin, OUTPUT);
Target1Hit = false;
Target2Hit= false;
Target3Hit= false;
tm.init();
tm.set(BRIGHT_TYPICAL);
tm.display(0, 0);
tm.display(1, 0);
tm.display(2, 0);
tm.display(3, 0);
tm.display(3,3);
delay(1000);
tm.display(3,2);
delay(1000);
tm.display(3,1);
delay(1000);
tm.display(3,0);
tone(BuzzerPin, 1000);
delay(500);
noTone(BuzzerPin);
tm.display(0, 1);
tm.display(1, 0);
tm.display(2, 0);
tm.display(3, 0);
Serial.begin(9600);
}
void loop() {
//Start the sequence by turning on all of the lights.
if (Target1Hit==false and Target2Hit==false and Target3Hit==false){
digitalWrite(Target1IndicatorPin,HIGH);
digitalWrite(Target2IndicatorPin,HIGH);
digitalWrite(Target3IndicatorPin,HIGH);
ScoreCounter=9999;
}
// read the values from the detectors:
int Target1Value = analogRead(Target1DetectorPin);
int Target2Value = analogRead(Target2DetectorPin);
int Target3Value = analogRead(Target3DetectorPin);
// if the analog value is high enough, turn on the LED:
if (Target1Value > HitThreshold) {
Target1Hit=true;
digitalWrite(Target1IndicatorPin,LOW);
} else {
}
delay(10); // delay in between reads for stability
if (Target2Value > HitThreshold) {
Target2Hit=true;
digitalWrite(Target2IndicatorPin,LOW);
} else {
}
delay(10); // delay in between reads for stability
if (Target3Value > HitThreshold) {
Target3Hit=true;
digitalWrite(Target1IndicatorPin,LOW);
} else {
}
delay(10); // delay in between reads for stability
if (Target1Hit==true and Target2Hit==true and Target3Hit==true){ //If all of the targets have been hit
Serial.println(ScoreCounter);
tm.display(0, (ScoreCounter / 1000) % 10);
tm.display(1, (ScoreCounter / 100) % 10);
tm.display(2, (ScoreCounter / 10) % 10);
tm.display(3, (ScoreCounter % 10));
//flash all of the lights 3 times
digitalWrite(Target1IndicatorPin,HIGH);
digitalWrite(Target2IndicatorPin,HIGH);
digitalWrite(Target3IndicatorPin,HIGH);
tone(BuzzerPin, 200);
delay(1000);
digitalWrite(Target1IndicatorPin,LOW);
digitalWrite(Target2IndicatorPin,LOW);
digitalWrite(Target3IndicatorPin,LOW);
noTone(BuzzerPin);
delay(1000);
digitalWrite(Target1IndicatorPin,HIGH);
digitalWrite(Target2IndicatorPin,HIGH);
digitalWrite(Target3IndicatorPin,HIGH);
tone(BuzzerPin, 100);
delay(1000);
digitalWrite(Target1IndicatorPin,LOW);
digitalWrite(Target2IndicatorPin,LOW);
digitalWrite(Target3IndicatorPin,LOW);
noTone(BuzzerPin);
delay(1000);
digitalWrite(Target1IndicatorPin,HIGH);
digitalWrite(Target2IndicatorPin,HIGH);
digitalWrite(Target3IndicatorPin,HIGH);
tone(BuzzerPin, 100);
delay(1000);
digitalWrite(Target1IndicatorPin,LOW);
digitalWrite(Target2IndicatorPin,LOW);
digitalWrite(Target3IndicatorPin,LOW);
noTone(BuzzerPin);
delay(1000);
//Then reset the sequence
Target1Hit = false;
Target2Hit= false;
Target3Hit= false;
}
ScoreCounter=ScoreCounter-1;
tm.display(0, (ScoreCounter / 1000) % 10);
tm.display(1, (ScoreCounter / 100) % 10);
tm.display(2, (ScoreCounter / 10) % 10);
tm.display(3, (ScoreCounter % 10));
}