168 lines
3.3 KiB
C++

#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
/*
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(bool easy) {
for (int i = 0; i < NUM_LEDS; i++) {
if (easy)
{
sequence[i] = 0;
}
else {
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);
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(
ledIndex(i),
strip.Color(35, 10, 0)
);
strip.show();
delay(20);
}
setAll(strip.Color(35, 10, 0));
strip.show();
}
void checkPress(int buttonID) {
if (buttonID == sequence[currentStep]) {
strip.setPixelColor(
ledIndex(currentStep),
strip.Color(35, 10, 0)
);
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);
powerOnState = digitalRead(POWER_PIN) == HIGH;
pinMode(austausch_master_pin, OUTPUT);
pinMode(randomizer_master_pin, INPUT);
digitalWrite(austausch_master_pin, LOW);
strip.begin();
strip.show();
randomSeed(millis());
generateNewSequence(false);
resetGame();
}
bool reset = false;
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)
{
unsigned long resetTimer = millis();
while (digitalRead(randomizer_master_pin) == HIGH)
{
delay(10);
}
generateNewSequence(millis() - resetTimer > 150);
resetGame();
}
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;
}