const int wires[] = {2, 3, 4}; // the numbers of the first through third wires
const int lights[] = {7, 9, 11}; // the numbers of the first through third lights
const int buzzer1 = 6; // the number of the first through third buzzers
const int period = 2000; // The amount of time that the light will be on
int getWire(int x)
{
return digitalRead(wires[x]);
}
void setup()
{
Serial.begin(9600);
delay(2000);
for (int i = 0; i < 3; i++)
{
pinMode(wires[i], INPUT);
pinMode(lights[i], OUTPUT);
}
pinMode(buzzer1, OUTPUT);
}
unsigned long lastTouches[] = {0, 0, 0}; // initialize each of the lastTouches
unsigned long currTime = 0; // initialize currTime
void loop()
{
currTime = millis(); // update currTime to the number of milliseconds since beginning
for (int i = 0; i < 3; i++)
{
if (getWire(i) == LOW)
{
lastTouches[i] = currTime;
digitalWrite(lights[i], HIGH); // turn the light on
tone(buzzer1, 800); // turn the buzzer on
Serial.print("wire ");
Serial.print(i);
Serial.print(" status ");
Serial.println(digitalRead(getWire(i)));
}
else
{
noTone(buzzer1); // turn the buzzer off
}
// turn off the light if it hasn't touched in the last period
if (currTime - lastTouches[i] >= period)
{
digitalWrite(lights[i], LOW);
}
}
}