// For Arduino Discord channel
// Sketch by styl
// Not working yet
// Tips:
// * ledPinsb are not set as output
// * no debouncing of buttons (the delay is good enough for now)
// * a long delay in the loop of 100ms
// * no clear path from input to output,
// buttons are read in updateLEDs()
const int switchPinX = 4;
const int switchPinY = 3;
const int numRows = 3;
const int numCols = 3;
const int numLEDs = numRows * numCols;
const int ledPins[] = {13, 12, 11, 10, 9, 8, 7, 6, 5};
const int ledPinsb[] = {0, 1, 2, A3, A4, A5, A0, A1, A2};
int grid[numRows][numCols] = {0};
int X = 0;
int Y = 0;
int colorchange = 0;
void setup() {
pinMode(switchPinX, INPUT);
pinMode(switchPinY, INPUT);
for (int i = 0; i < numLEDs; i++) {
pinMode(ledPins[i], OUTPUT);
}
for (auto pin : ledPinsb) {
pinMode(pin, OUTPUT);
}
}
void loop() {
// These switches are normally HIGH, so invert to determine
// if they are pressed.
int switchXDown = !digitalRead(switchPinX);
int switchYDown = !digitalRead(switchPinY);
if (!switchXDown && !switchYDown) {
setLEDPermanent();
if (colorchange == 0) {
colorchange = 1;
} else if (colorchange == 1) {
colorchange = 0;
}
} else if (!switchXDown && switchYDown) {
moveLeft();
} else if (switchXDown && !switchYDown) {
moveDown();
}
// Wait for buttons to be released
while (switchXDown) switchXDown = !digitalRead(switchPinX);
while (switchYDown) switchYDown = !digitalRead(switchPinY);
updateLEDs();
checkwin();
delay(100);
}
void setLEDPermanent() {
grid[Y][X] = 2;
}
void moveLeft() {
if (grid[Y][X] != 2) {
grid[Y][X] = 0;
}
X = (X + 1) % numCols;
while (grid[Y][X] == 2) {
X = (X + 1) % numCols;
}
if (grid[Y][X] != 2) {
grid[Y][X] = 1;
}
}
void moveDown() {
if (grid[Y][X] != 2) {
grid[Y][X] = 0;
}
Y = (Y + 1) % numRows;
while (grid[Y][X] == 2) {
Y = (Y + 1) % numRows;
}
if (grid[Y][X] != 2) {
grid[Y][X] = 1;
}
}
void updateLEDs() {
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
int index = row * numCols + col;
int state = grid[row][col];
if (colorchange == 0) {
if (state == 0) {
digitalWrite(ledPins[index], LOW);
} else if (state == 1) {
digitalWrite(ledPins[index], HIGH);
} else if (state == 2 && digitalRead(ledPinsb[index]) == LOW) {
digitalWrite(ledPins[index], HIGH);
}
}
if (colorchange == 1) {
if (state == 0) {
digitalWrite(ledPinsb[index], LOW);
} else if (state == 1) {
digitalWrite(ledPinsb[index], HIGH);
} else if (state == 2 && digitalRead(ledPins[index]) == LOW) {
digitalWrite(ledPinsb[index], HIGH);
}
}
}
}
}
void checkwin() {
}