// https://wokwi.com/projects/369410932236902401
// https://forum.arduino.cc/t/3d-archery-pop-up-system-computer-replacement/1144336
# include <Adafruit_NeoPixel.h>
# define PIN A2 // the pin
# define NPIXELS 12 // number of LEDs on strip
Adafruit_NeoPixel relayRing(NPIXELS, PIN, NEO_GRB + NEO_KHZ800);
const int relayPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
const int numRelays = sizeof(relayPins) / sizeof(relayPins[0]);
const int buttonPin = A0;
const int targetsPerCycle = 6;
unsigned char targetList[targetsPerCycle] = {0, 1, 2, 3, 4, 5};
//const int targetDelay = 3000; // life too short
const int targetDelay = 800; // life too short
int currentCycle = 0;
int currentTarget = 0;
bool systemRunning = false;
bool relayStates[numRelays][targetsPerCycle] = {
{1, 0, 1, 0, 1, 0},
{0, 1, 0, 1, 0, 1},
// every target for now
// {1, 1, 1, 1, 1, 1},
// {1, 1, 1, 1, 1, 1},
};
void digitalWrite(unsigned char pin, unsigned char val, char *msg)
{
Serial.print(msg);
Serial.print(pin);
Serial.print(" goes ");
Serial.println(val ? "HIGH" : "LOW");
digitalWrite(pin, val);
relayRing.setPixelColor(pin - 2, val ? 0xff0000 : 0x008080);
relayRing.show();
}
void setup() {
Serial.begin(115200);
Serial.println("Jello Whirled!\n");
relayRing.begin();
relayRing.clear();
relayRing.show();
for (int i = 0; i < numRelays; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], LOW);
}
pinMode(buttonPin, INPUT_PULLUP);
// randomSeed(analogRead(A1)); // later - for now it is good to have the same sequence
// shuffleTargetList();
}
void loop() {
if (digitalRead(buttonPin) == LOW) {
delay(50);
if (digitalRead(buttonPin) == LOW) {
if (systemRunning) {
systemRunning = false;
for (int i = 0; i < numRelays; i++)
digitalWrite(relayPins[i], LOW);
} else {
systemRunning = true;
currentCycle = 0;
currentTarget = 0;
}
}
}
if (systemRunning) {
bool* currentPermutation = relayStates[currentCycle];
bool targetState = currentPermutation[currentTarget];
unsigned char pinIndex = currentCycle * targetsPerCycle + targetList[currentTarget];
digitalWrite(relayPins[pinIndex], targetState, " on or not ");
delay(targetDelay);
digitalWrite(relayPins[pinIndex], LOW, " off in any case ");
currentTarget++;
if (currentTarget >= targetsPerCycle) {
shuffleArray(currentPermutation, targetsPerCycle);
shuffleTargetList();
currentCycle++;
if (currentCycle >= numRelays / targetsPerCycle) {
currentCycle = 0;
}
currentTarget = 0;
}
}
}
void shuffleArray(bool* array, int size) {
Serial.println(" s h u f f l e ");
for (int i = size - 1; i > 0; i--) {
int j = random(i + 1);
bool temp = array[i];
array[i] = array[j];
array[j] = temp;
}
for (int i = 0; i < size; i++) {
Serial.print(array[i]);
Serial.print(" ");
}
Serial.println();
}
void shuffleTargetList()
{
int size = 6;
Serial.println(" target list ");
for (int i = size - 1; i > 0; i--) {
int j = random(i + 1);
unsigned char temp = targetList[i];
targetList[i] = targetList[j];
targetList[j] = temp;
}
for (int i = 0; i < size; i++) {
Serial.print(targetList[i]);
Serial.print(" ");
}
Serial.println();
}