// https://forum.arduino.cc/t/3d-archery-pop-up-system-computer-replacement/1144336/18
// https://wokwi.com/projects/369652820252224513
/*
Total targets: 12
Active targets: 6 (random 6 of 12)
Original sequence of events:
- random start time
- 6 random relays
- 10 to 15 seconds relay activation
- 20 seconds cycle deactivate relays
Updated sequence of events:
- add pushbutton
- press start button. *do not use D0 or D1*... use A0 with INPUT_PULLUP
- pause 20 seconds
- target 1 of 6 activated for 5 to 10 seconds then deactivated
- 15 to 20 seconds between relay activation
- target 2 of 6 activated for 5 to 10 seconds then deactivated
- repeat for targets #3 through #6
o 10 seconds pause after first round of six targets
- activate active six target relays to pull arrows
- wait for button to deactivate first six targets
o repeat sequence for remaining 6 target targets
- restart
*/
// int timer = 1000; // "live" time
int timer = 50; // shortened time for debugging
int i; // counter
const int buttonPin = A0; // start/restart button
const int relayPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // pins the relays will use
const int relayTotal = 12; // total number of targets/relays
int relayNumber[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // all relays
int randomSequence; // a random number
int relayCount; // relayNumber
int activationTime; // random delay to activate relay
int cycleNumber; // number of cycles
void setup() {
Serial.begin(115200); // can i haz monitor
randomSeed(analogRead(A0)); // creates more random pseudo-random numbers
for (i = 0; i < relayTotal; i++) { // number of relays
pinMode(relayPins[i], OUTPUT); // configure array of relay pins for output
}
pinMode(buttonPin, INPUT_PULLUP); // start button
if (timer < 1000) {
Serial.print("Running on SIM time... x");
Serial.print( 1000 / timer );
Serial.println(" speed.");
} else {
Serial.print("WE ");
delay(500);
Serial.print("ARE ");
delay(500);
Serial.println("LIVE!");
}
}
void loop() {
if (cycleNumber++ > 1)
cycleNumber = 1; // cycleNumber is 1 or 2
deactivateRelays();
cycleStart();
assignSequence();
activateSequence();
activateAllRelays(); // retrieve arrows
}
void deactivateRelays() {
Serial.print("Cycle ");
Serial.print(cycleNumber);
Serial.print("A: Deactivate targets: ");
for (i = 0; i < relayTotal; i++) {
digitalWrite(relayPins[i], LOW); // deactivate all relays
relayNumber[i] = 0; // clear sequence number
Serial.print(relayNumber[i]); // display relay sequence - not activated (0)
Serial.print(" ");
}
Serial.println();
}
void cycleStart() {
Serial.print("Cycle ");
Serial.print(cycleNumber);
Serial.print("B: Press button to continue.");
while (digitalRead(buttonPin)) {/* contemplate 42 */} // wait for LOW from button press
Serial.print(" Begin 20 seconds delay.");
delay(20 * timer - activationTime * timer); // 20 seconds minus the start time
Serial.println(" Delay complete.");
}
void assignSequence() {
relayCount = 0;
for (i = 0; i < relayTotal; i++)
relayNumber[i] = 0; // clear relay sequence
while (relayCount < relayTotal) { // number of relays assigned a random sequence
randomSequence = random (relayTotal); // get a random number
if (relayNumber[randomSequence] == 0) { // if zero, this element can be assigned a sequence number
relayNumber[randomSequence] = relayCount; // element/relay is assigned a sequence number
relayCount++; // increase number of relays assigned a sequence number
}
}
Serial.print("Cycle ");
Serial.print(cycleNumber);
Serial.print("C: Target sequence: ");
for (i = 0; i < relayTotal; i++) { // display relay sequence - not activated (0)
Serial.print(relayNumber[i]);
Serial.print(" ");
}
Serial.println();
}
void activateSequence() {
for (i = 0; i < relayTotal; i++) {
activationTime = random (5, 10); // random delay - 5 to 10 seconds
Serial.print("Cycle ");
Serial.print(cycleNumber);
Serial.print("D: Target:");
if (relayNumber[i] < 10) // single digit number
Serial.print(" "); // add space padding
Serial.print(relayNumber[i]);
Serial.print(" Delay:");
if (activationTime) // single digit number
Serial.print(" "); // add space padding
Serial.print(activationTime);
Serial.print(" ");
Serial.print("ON");
digitalWrite(relayPins[relayNumber[i]], HIGH); // activate relay in sequence
delay(activationTime * timer); // delay before next relay activation
digitalWrite(relayPins[relayNumber[i]], LOW); // deactivate relay
delay(random(15, 20)); // delay 15 to 20 seconds between target activation
Serial.println("...off");
}
}
void activateAllRelays() {
Serial.print("Cycle ");
Serial.print(cycleNumber);
Serial.println("E: Press button to activate all relays to retrieve arrows.");
while (digitalRead(buttonPin)) {/* do nothing */} // wait for LOW from button press
for (i = 0; i < relayTotal; i++)
digitalWrite(relayPins[i], HIGH); // activate relay in sequence
delay(150); // debounce the button
Serial.print("Cycle ");
Serial.print(cycleNumber);
Serial.println("F: Press button to deactivate targets.");
while (digitalRead(buttonPin)) {/* do nothing */} // wait for LOW from button press
delay(150); // debounce the button
}