/*
Forum: https://forum.arduino.cc/t/arduino-nano-projekt-led-array-und-tastersteuerung-fur-magisches-quadrat-funktionieren-nicht-wie-erwartet/1312361
Wokwi: https://wokwi.com/projects/412016357755096065
Original-Sketch
ec2021
*/
// LED-Pins
int ledPins[3][3] = {
{2, 3, 4},
{5, 6, 7},
{8, 9, 10}
};
// Taster-Pins
const int tasterUnten = 11;
const int tasterRechts = 12;
const int tasterSpeichern = 13;
const int tasterReset = A0;
// RGB-LED-Pins
const int pinRot = A1;
const int pinGruen = A2;
const int pinBlau = A3;
// 3x3 Array für das magische Quadrat
int quadrat[3][3] = {0};
// Zahlen für das magische Quadrat
int zahlen[9] = {5, 47, 17, 29, 71, 89, 59, 113, 101};
int aktuelleZahl = 0;
// Startposition
int posX = 0, posY = 0;
void setup() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
pinMode(ledPins[i][j], OUTPUT);
digitalWrite(ledPins[i][j], LOW);
}
}
pinMode(tasterUnten, INPUT_PULLUP);
pinMode(tasterRechts, INPUT_PULLUP);
pinMode(tasterSpeichern, INPUT_PULLUP);
pinMode(tasterReset, INPUT_PULLUP);
pinMode(pinRot, OUTPUT);
pinMode(pinGruen, OUTPUT);
pinMode(pinBlau, OUTPUT);
digitalWrite(ledPins[posX][posY], HIGH);
}
void loop() {
if (digitalRead(tasterUnten) == LOW) {
bewegeUnten();
delay(200);
}
if (digitalRead(tasterRechts) == LOW) {
bewegeRechts();
delay(200);
}
if (digitalRead(tasterSpeichern) == LOW) {
speichereZahl();
delay(200);
}
if (digitalRead(tasterReset) == LOW) {
resetSpiel();
delay(200);
}
}
void bewegeUnten() {
digitalWrite(ledPins[posX][posY], LOW);
posX = (posX + 1) % 3;
digitalWrite(ledPins[posX][posY], HIGH);
}
void bewegeRechts() {
digitalWrite(ledPins[posX][posY], LOW);
posY = (posY + 1) % 3;
digitalWrite(ledPins[posX][posY], HIGH);
}
void speichereZahl() {
quadrat[posX][posY] = zahlen[aktuelleZahl];
aktuelleZahl++;
digitalWrite(ledPins[posX][posY], HIGH);
}
void resetSpiel() {
aktuelleZahl = 0;
posX = 0;
posY = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
quadrat[i][j] = 0;
digitalWrite(ledPins[i][j], LOW);
}
}
digitalWrite(ledPins[posX][posY], HIGH);
}