diff --git a/terminal/puzzle_leftRight/.gitignore b/terminal/puzzle_leftRight/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/terminal/puzzle_leftRight/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/terminal/puzzle_leftRight/platformio.ini b/terminal/puzzle_leftRight/platformio.ini new file mode 100644 index 0000000..d5fd72a --- /dev/null +++ b/terminal/puzzle_leftRight/platformio.ini @@ -0,0 +1,15 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:nanoatmega328new] +platform = atmelavr +board = nanoatmega328new +framework = arduino +lib_deps = adafruit/Adafruit NeoPixel@^1.15.2 diff --git a/terminal/puzzle_leftRight/src/main.cpp b/terminal/puzzle_leftRight/src/main.cpp new file mode 100644 index 0000000..1337f8e --- /dev/null +++ b/terminal/puzzle_leftRight/src/main.cpp @@ -0,0 +1,155 @@ +#include +#include + +/* +Slave Modul 1 +- Taster an A0 und A1 (INPUT_PULLUP) +- 16 Neopixel an D4 +- D3 -> Master + LOW = nicht gelöst + HIGH = gelöst (bleibt HIGH) +- D2 -> Master + HIGH = neue Sequenz generieren +*/ + +#define LED_PIN 4 +#define NUM_LEDS 16 + +#define POWER_PIN 12 +bool powerOnState = true; + +#define BUTTON1_PIN A0 +#define BUTTON2_PIN A1 +#define austausch_master_pin 3 +#define randomizer_master_pin 2 + +Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_RGB + NEO_KHZ800); + +int sequence[NUM_LEDS]; +int currentStep = 0; +bool gameWon = false; + +int lastButton1State = HIGH; +int lastButton2State = HIGH; + +int ledIndex(int logical) { + return (NUM_LEDS - 1) - logical; +} + +void generateNewSequence() { + for (int i = 0; i < NUM_LEDS; i++) { + sequence[i] = random(0, 2); + } +} + +void setAll(uint32_t color) { + for (int i = 0; i < NUM_LEDS; i++) { + strip.setPixelColor(i, color); + } +} + +void resetGame() { + setAll(strip.Color(0, 0, 0)); + strip.show(); + currentStep = 0; + gameWon = false; + digitalWrite(austausch_master_pin, LOW); +} + +void winAnimation() { + strip.show(); + delay(250); + + setAll(strip.Color(0, 0, 0)); + strip.show(); + delay(250); + + uint8_t w = 50; + for (int i = 0; i < NUM_LEDS; i++) { + strip.setPixelColor( + ledIndex(i), + strip.Color(0, 0, w) + ); + strip.show(); + delay(20); + } + + setAll(strip.Color(0, 0, w)); + strip.show(); +} + +void checkPress(int buttonID) { + if (buttonID == sequence[currentStep]) { + strip.setPixelColor( + ledIndex(currentStep), + strip.Color(0, 0, 40) + ); + strip.show(); + currentStep++; + + if (currentStep >= NUM_LEDS) { + winAnimation(); + gameWon = true; + digitalWrite(austausch_master_pin, HIGH); + } + } else { + resetGame(); + } +} + +void setup() { + pinMode(BUTTON1_PIN, INPUT_PULLUP); + pinMode(BUTTON2_PIN, INPUT_PULLUP); + pinMode(POWER_PIN, INPUT_PULLUP); + powerOnState = digitalRead(POWER_PIN) == HIGH; + pinMode(austausch_master_pin, OUTPUT); + pinMode(randomizer_master_pin, INPUT_PULLUP); + + digitalWrite(austausch_master_pin, LOW); + + strip.begin(); + strip.show(); + + randomSeed(millis()); + generateNewSequence(); + resetGame(); +} + +void loop() { + //Detecting turn-off + if (powerOnState && digitalRead(POWER_PIN) == LOW) { + powerOnState = false; + resetGame(); + } + //Detecting turn-on + if (!powerOnState && digitalRead(POWER_PIN) == HIGH) { + powerOnState = true; + } + if (!powerOnState) { + //We are turned-off and stop anything after here + return; + } + // Master-D2 HIGH → neue Sequenz + if (digitalRead(randomizer_master_pin) == HIGH) { + generateNewSequence(); + resetGame(); + delay(50); // Puls kurz blockieren + } + + int currentButton1State = digitalRead(BUTTON1_PIN); + int currentButton2State = digitalRead(BUTTON2_PIN); + + if (!gameWon) { + if (lastButton1State == HIGH && currentButton1State == LOW) { + checkPress(0); + delay(200); + } + if (lastButton2State == HIGH && currentButton2State == LOW) { + checkPress(1); + delay(200); + } + } + + lastButton1State = currentButton1State; + lastButton2State = currentButton2State; +} \ No newline at end of file