// 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 HitThreshold = 200; // an arbitrary threshold level that's in the range of the analog input
bool Target1Hit = false;
bool Target2Hit= false;
bool Target3Hit= false;
void setup() {
// initialize serial communications:
pinMode(Target1IndicatorPin, OUTPUT);
pinMode(Target2IndicatorPin, OUTPUT);
pinMode(Target3IndicatorPin, OUTPUT);
Target1Hit = false;
Target2Hit= false;
Target3Hit= false;
}
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);
}
// 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
//flash all of the lights 3 times
digitalWrite(Target1IndicatorPin,HIGH);
digitalWrite(Target2IndicatorPin,HIGH);
digitalWrite(Target3IndicatorPin,HIGH);
delay(1000);
digitalWrite(Target1IndicatorPin,LOW);
digitalWrite(Target2IndicatorPin,LOW);
digitalWrite(Target3IndicatorPin,LOW);
delay(1000);
digitalWrite(Target1IndicatorPin,HIGH);
digitalWrite(Target2IndicatorPin,HIGH);
digitalWrite(Target3IndicatorPin,HIGH);
delay(1000);
digitalWrite(Target1IndicatorPin,LOW);
digitalWrite(Target2IndicatorPin,LOW);
digitalWrite(Target3IndicatorPin,LOW);
delay(1000);
digitalWrite(Target1IndicatorPin,HIGH);
digitalWrite(Target2IndicatorPin,HIGH);
digitalWrite(Target3IndicatorPin,HIGH);
delay(1000);
digitalWrite(Target1IndicatorPin,LOW);
digitalWrite(Target2IndicatorPin,LOW);
digitalWrite(Target3IndicatorPin,LOW);
delay(1000);
//Then reset the sequence
Target1Hit = false;
Target2Hit= false;
Target3Hit= false;
}
}