i am trying to make a connect 4 game with led's being wired to each output of a mega arduino. My problem right now is that when i press the buttons to move around the led if one has been saved as on it turns off after i move off it.
const int buttonPin = 2;
const int subButtonPin = 3;
const int add4ButtonPin = 4;
const int sub4ButtonPin = 5;
const int groupTogglePin = 6;
const int group1LedPins[] = {22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37};
const int group2LedPins[] = {38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};
const int numLeds = sizeof(group1LedPins) / sizeof(group1LedPins[0]);
int currentLedGroup = 1;
int lastActiveLedGroup1 = 0;
int lastActiveLedGroup2 = 0;
bool group1Initialized = false;
bool group2Initialized = false;
int group1win[] = {22, 23, 24, 25};
int group2win[] = {26, 27, 28, 29};
int group3win[] = {30, 31, 32, 33};
int group4win[] = {34, 35, 36, 37};
int group5win[] = {22, 26, 30, 34};
int group6win[] = {23, 27, 31, 35};
int group7win[] = {24, 28, 32, 36};
int group8win[] = {25, 29, 33, 37};
int group9win[] = {22, 27, 32, 37};
int group10win[] = {25, 28, 31, 34};
int group11win[] = {38, 39, 40, 41};
int group12win[] = {42, 43, 44, 45};
int group13win[] = {46, 47, 48, 49};
int group14win[] = {50, 51, 52, 53};
int group15win[] = {38, 42, 46, 50};
int group16win[] = {39, 43, 47, 51};
int group17win[] = {40, 44, 48, 52};
int group18win[] = {41, 45, 49, 53};
int group19win[] = {38, 43, 48, 53};
int group20win[] = {41, 44, 47, 50};
const int numGroups = 20;
int* winningGroups[numGroups] = {
group1win, group2win, group3win, group4win,
group5win, group6win, group7win, group8win,
group9win, group10win, group11win, group12win,
group13win, group14win,
group15win, group16win, group17win, group18win,
group19win, group20win,
};
const int numLedss = 32;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(subButtonPin, INPUT);
pinMode(add4ButtonPin, INPUT);
pinMode(sub4ButtonPin, INPUT);
pinMode(groupTogglePin, INPUT);
for (int i = 0; i < numLeds; i++) {
pinMode(group1LedPins[i], OUTPUT);
pinMode(group2LedPins[i], OUTPUT);
digitalWrite(group1LedPins[i], LOW);
digitalWrite(group2LedPins[i], LOW);
}
// Set the first LED in the first group (pin 22) to HIGH at the start
digitalWrite(group1LedPins[lastActiveLedGroup1], HIGH);
for (int i = 0; i < numLeds; i++) {
pinMode(i + 22, OUTPUT); // Assuming LEDs are connected to pins 22 to 29
pinMode(i + 38, OUTPUT);
}
}
void loop() {
if (digitalRead(groupTogglePin) == HIGH) {
toggleLedGroup();
delay(200);
}
if (currentLedGroup == 1) {
handleButtonPress(group1LedPins, lastActiveLedGroup1, group2LedPins);
} else if (currentLedGroup == 2) {
handleButtonPress(group2LedPins, lastActiveLedGroup2, group1LedPins);
}
}
void handleButtonPress(int ledGroup[], int &lastActiveLed, const int otherGroupPins[]) {
if (digitalRead(buttonPin) == HIGH) {
if (digitalRead(ledGroup[lastActiveLed]) == LOW && !isCorrespondingLedOn(otherGroupPins, lastActiveLed)) {
digitalWrite(ledGroup[lastActiveLed], HIGH);
} else {
advanceLed(ledGroup, lastActiveLed);
}
delay(200);
}
if (digitalRead(subButtonPin) == HIGH) {
if (digitalRead(ledGroup[lastActiveLed]) == LOW && !isCorrespondingLedOn(otherGroupPins, lastActiveLed)) {
digitalWrite(ledGroup[lastActiveLed], HIGH);
} else {
subtractLed(ledGroup, lastActiveLed);
}
delay(200);
}
if (digitalRead(add4ButtonPin) == HIGH) {
advanceLedBy(ledGroup, lastActiveLed, 4);
delay(200);
}
if (digitalRead(sub4ButtonPin) == HIGH) {
subtractLedBy(ledGroup, lastActiveLed, 4);
delay(200);
}
}
void toggleLedGroup() {
if (currentLedGroup == 1) {
lastActiveLedGroup1 = findLastActiveLed(group1LedPins);
turnOffCorrespondingLed(group2LedPins, lastActiveLedGroup1);
lastActiveLedGroup2 = 0;
digitalWrite(group2LedPins[lastActiveLedGroup2], HIGH);
currentLedGroup = 2;
} else {
lastActiveLedGroup2 = findLastActiveLed(group2LedPins);
turnOffCorrespondingLed(group1LedPins, lastActiveLedGroup2);
lastActiveLedGroup1 = 0;
digitalWrite(group1LedPins[lastActiveLedGroup1], HIGH);
currentLedGroup = 1;
}
}
void advanceLed(int ledGroup[], int &lastActiveLed) {
digitalWrite(ledGroup[lastActiveLed], LOW);
lastActiveLed = (lastActiveLed + 1) % numLeds;
digitalWrite(ledGroup[lastActiveLed], HIGH);
}
void subtractLed(int ledGroup[], int &lastActiveLed) {
digitalWrite(ledGroup[lastActiveLed], LOW);
lastActiveLed = (lastActiveLed - 1 + numLeds) % numLeds;
digitalWrite(ledGroup[lastActiveLed], HIGH);
}
void advanceLedBy(int ledGroup[], int &lastActiveLed, int steps) {
for (int i = 0; i < steps; i++) {
advanceLed(ledGroup, lastActiveLed);
delay(50);
}
}
void subtractLedBy(int ledGroup[], int &lastActiveLed, int steps) {
for (int i = 0; i < steps; i++) {
subtractLed(ledGroup, lastActiveLed);
delay(50);
}
}
int findLastActiveLed(const int ledGroup[]) {
for (int i = 0; i < numLeds; i++) {
if (digitalRead(ledGroup[i]) == HIGH) {
return i;
}
}
return -1;
}
bool isCorrespondingLedOn(const int otherGroupPins[], int currentLedIndex) {
int correspondingLedIndex = (currentLedIndex + numLeds / 2) % numLeds;
return digitalRead(otherGroupPins[correspondingLedIndex]) == HIGH;
}
void turnOffCorrespondingLed(int otherGroupPins[], int currentLedIndex) {
int correspondingLedIndex = (currentLedIndex + numLeds / 2) % numLeds;
digitalWrite(otherGroupPins[correspondingLedIndex], LOW);
for (int i = 0; i < numGroups; i++) {
if (checkWinningGroup(winningGroups[i])) {
flashLeds(5000);
break;
}
}
}
bool checkWinningGroup(int* group) {
for (int i = 0; i < 4; i++) {
if (digitalRead(group[i]) != HIGH) {
return false;
}
}
return true;
}
void flashLeds(int duration) {
unsigned long startTime = millis();
while (millis() - startTime < duration) {
for (int i = 0; i < numLeds; i++) {
digitalWrite(i + 22, HIGH);
digitalWrite(i + 38, HIGH);
}
delay(250); // Flash on for 250ms
for (int i = 0; i < numLeds; i++) {
digitalWrite(i + 22, LOW);
digitalWrite(i + 38, LOW);
}
delay(250); // Off for 250ms
}
}