#include <Arduino.h>
#include <FastLED.h>
//Debouncing
constexpr unsigned long debounceTime{ 30 };
class BtnClass {
private:
byte pin;
byte state;
byte lastState;
unsigned long lastChange;
unsigned long lastPressTime;
public:
void init(byte p) {
pin = p;
pinMode(pin, INPUT_PULLUP);
lastPressTime = 0;
}
unsigned long getLastPressTime() {
return lastPressTime;
};
void reset() {
lastPressTime = 0;
}
boolean pressed() {
byte actState = digitalRead(pin);
if (actState != lastState) {
lastChange = millis();
lastState = actState;
};
if (actState != state && millis() - lastChange > debounceTime) {
state = actState;
if (!state) lastPressTime = lastChange;
return !state;
}
return false;
}
};
/*
Zahlen 1 ... 30 0 ... 29
Annahmeschluss 30, 31
Lotto 32, 33
Wählen 34, 35
Diagnose 36, 37
Zusatzz. 31 .. 34 38, 39, 40, 41
Circle 42 ... 49
*/
uint8_t lottoValues[32] = {
60, 40, 20, 80, 10, 40, 20, 10, 40, 10, 60, 10, 40, 20, 10, 40,
100, 20, 80, 20, 10, 60, 10, 20, 10, 10, 80, 20, 20, 40, 20, 60
};
// Leds Nr
constexpr byte CIRCLE_LED_PIN = 6;
constexpr int CIRCLE_NUM_LEDS = 8;
// Farben
constexpr CRGB colBlack = CRGB(0, 0, 0);
constexpr CRGB colRed = CRGB(250, 0, 0);
constexpr CRGB colGreen = CRGB(0, 250, 0);
constexpr CRGB colBlue = CRGB(0, 0, 250);
constexpr CRGB colWhite = CRGB(250, 250, 250);
constexpr CRGB colNiete = colRed;
constexpr CRGB colTreffer = colGreen;
constexpr CRGB colAuswahl = colWhite;
constexpr unsigned long randomDauer{ 5000 }; // 5000
// State Machine
enum class STATES { IDLE,
PLAY,
RANDOM,
FINAL
};
STATES state = STATES::IDLE;
CRGB circle[CIRCLE_NUM_LEDS];
byte btnMode = 0;
unsigned long startTime = 0;
BtnClass btnLinks;
BtnClass btnRechts;
boolean links, rechts;
byte randomZahl, randomZusatz;
void setup() {
Serial.begin(115200);
btnLinks.init(2);
btnRechts.init(5);
FastLED.addLeds<WS2812B, CIRCLE_LED_PIN, RGB>(circle, CIRCLE_NUM_LEDS);
FastLED.setBrightness(255);
}
void loop() {
stateMachine();
}
void stateMachine() {
switch (state) {
case STATES::IDLE:
if (btnLinks.pressed()) {
Serial.println("TEST Random");
startTime = millis();
randomSeed(startTime);
state = STATES::RANDOM;
};
break;
case STATES::RANDOM:
randomTime();
break;
}
}
void randomTime() {
static unsigned long startRnd = 0;
static int idx = 0;
static unsigned int delayVal = 10;
if (millis() - startRnd > delayVal) {
startRnd = millis();
circle[idx] = colBlack;
idx++;
if (idx >= CIRCLE_NUM_LEDS) idx = 0;
circle[idx] = colRed;
delayVal *= 1.1;
FastLED.show();
}
if (millis() - startTime > randomDauer) {
randomSeed(millis());
int start = idx;
randomZahl = idx*4+random(0, 4); // 0 ... 31 = Zahlen, mit 0 und 16 = Lotto (2 Chancen)
randomZusatz = random(31, 35); // 31 .. 35
delayVal = 10 + random(5);
auswerten(idx);
Serial.println("Idle");
startTime = millis();
state = STATES::IDLE;
}
}
void auswerten(int indx) {
int gewinn = 0;
Serial.print("Index: ");
Serial.println(indx);
switch (randomZahl) {
case 0:
case 16:
Serial.print("Lotto: ");
Serial.println(randomZahl);
gewinn = lottoValues[randomZahl];
break;
default:
gewinn = lottoValues[randomZahl];
if (randomZahl > 16) randomZahl--;
Serial.print("Gewinnzahl: ");
Serial.println(randomZahl);
};
Serial.print("Gewinn (ohne Zusatzzahl): ");
Serial.println(gewinn);
}
/*
0 -> L ... 3 mit 0 = Lotto sonst Zahl = ZUfallszahl
1 -> 4 ... 7
2 -> 8 ... 11
3 -> 12 ... 15
4 -> L ... 18 mit 16 = Lotto, mit > 16 -> Zahl = Zufallszahl-1
5 -> 19 ... 22
6 -> 23 ... 26
7 -> 27 ... 30
*/