#include <stdio.h>
#include "pico/stdlib.h"
const unsigned int PIN_START = 8;
int aPinChicken[6] = {2, 3, 4, 5, 6, 7};
int aChickensDone[6];
void initialize_gpio() {
gpio_init(PIN_START);
gpio_set_dir(PIN_START, GPIO_IN);
gpio_pull_up(PIN_START);
// Initialize chickens and set magnets on
for (int i=0; i<6; i++) {
gpio_init(aPinChicken[i]);
gpio_set_dir(aPinChicken[i], GPIO_OUT);
gpio_put(aPinChicken[i], 1);
}
}
int randomWait() {
// Wait between 1 and 4 seconds
return 1000 + rand() % 4000;
}
int randomChicken(int iIndex) {
int iRandomChicken = 0;
bool bFound;
// Get unique chicken number
do {
iRandomChicken = 1 + rand() % 6;
bFound = false;
for (int i=0; i<6; i++) {
if (aChickensDone[i] == iRandomChicken) {
bFound = true;
break;
}
}
} while (bFound == true);
aChickensDone[iIndex] = iRandomChicken;
return iRandomChicken;
}
int main() {
initialize_gpio();
stdio_init_all();
while (true) {
if (gpio_get(PIN_START) == 0) {
// Start
for (int i=0; i<6; i++) {
sleep_ms(randomWait());
gpio_put(aPinChicken[randomChicken(i)-1], 0);
}
// Clear array and set magnets on
sleep_ms(2000);
memset(aChickensDone, 0, 6*sizeof(int));
for (int i=0; i<6; i++) {
gpio_put(aPinChicken[i], 1);
}
}
}
}