From e910c2451e4e568412f9bb76b88d68eff2448106 Mon Sep 17 00:00:00 2001 From: BOT Alex <44818698+MagicBOTAlex@users.noreply.github.com> Date: Tue, 14 May 2024 12:25:23 +0200 Subject: [PATCH 1/2] Module button done --- ModuleButton/ModuleButton.ino | 57 +++++++++++++++++++++++++++++------ ModuleWires/ModuleWires.ino | 0 2 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 ModuleWires/ModuleWires.ino diff --git a/ModuleButton/ModuleButton.ino b/ModuleButton/ModuleButton.ino index c584e10..f82b766 100644 --- a/ModuleButton/ModuleButton.ino +++ b/ModuleButton/ModuleButton.ino @@ -7,16 +7,23 @@ HTTPClient http; #define buttonPin 26 #define led1 25 -#define led2 0 +#define led2 255 +#define retractPin 0 #define longPressThreshhold 150 bool lightA, lightB = false; +// Press / hold detection +long pressedTime = 0; +long releasedTime = 0; +bool pressed = false; + void initRandomSeed() { wifiMulti.addAP("AIRPORT-FREE-WIFI-NEW", "alexalex"); wifiMulti.addAP("Next-Guest", ""); + wifiMulti.addAP("boost-v2", "crystalize"); long startStartedWifi = millis(); @@ -31,8 +38,12 @@ void initRandomSeed() void reset(){ //assert(false); + M5.Lcd.fillScreen(BLACK); + lightA = random(2) == 1 ? true : false; lightB = random(2) == 1 ? true : false; + + digitalWrite(retractPin, HIGH); } void drawUI(){ @@ -45,18 +56,36 @@ void drawUI(){ } void enterWinLoop(){ + M5.Lcd.fillScreen(GREEN); + digitalWrite(retractPin, LOW); while (true){ - delay(1000); + delay(250); digitalWrite(led1, LOW); digitalWrite(led2, LOW); - delay(1000); + delay(250); digitalWrite(led1, HIGH); digitalWrite(led2, HIGH); - if (digitalRead(buttonPin) == HIGH){ - + if (!pressed && digitalRead(buttonPin) == HIGH) + { + // On click + pressedTime = millis(); + pressed = true; + } + else if (pressed && digitalRead(buttonPin) == LOW) + { + // On release + releasedTime = millis(); + pressed = false; + + long totalPressedTime = releasedTime - pressedTime; + Serial.println("Total time: " + String(totalPressedTime)); + + if (totalPressedTime > 2000) + break; } } + } void setup() @@ -72,6 +101,7 @@ void setup() pinMode(led1, OUTPUT_OPEN_DRAIN); pinMode(led2, OUTPUT_OPEN_DRAIN); + pinMode(retractPin, OUTPUT_OPEN_DRAIN); Serial.begin(115200); @@ -80,10 +110,6 @@ void setup() M5.Lcd.fillScreen(BLACK); } -long pressedTime = 0; -long releasedTime = 0; -bool pressed = false; - void loop() { drawUI(); @@ -121,5 +147,18 @@ void loop() }else{ Serial.println("Long press detected!"); } + + if (totalPressedTime < longPressThreshhold != lightA ^ lightB) + { + enterWinLoop(); + } + else + { + // Fail + M5.Lcd.fillScreen(RED); + delay(1000); + } + + reset(); } } diff --git a/ModuleWires/ModuleWires.ino b/ModuleWires/ModuleWires.ino new file mode 100644 index 0000000..e69de29 From 5e214b68520f628ce3f662200cd4b143f47e5afc Mon Sep 17 00:00:00 2001 From: BOT Alex <44818698+MagicBOTAlex@users.noreply.github.com> Date: Tue, 14 May 2024 13:45:34 +0200 Subject: [PATCH 2/2] Wires module done --- ModuleWires/ModuleWires.ino | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/ModuleWires/ModuleWires.ino b/ModuleWires/ModuleWires.ino index e69de29..7e93a4f 100644 --- a/ModuleWires/ModuleWires.ino +++ b/ModuleWires/ModuleWires.ino @@ -0,0 +1,65 @@ +#include + +#define correctWirePin 26 +#define wrongWirePin 25 +#define extenderPin 0 + +bool hasWon, hasLost = false; + +void reset() +{ + hasWon = false; + hasLost = false; +} + +void setup() +{ + M5.begin(); + M5.Lcd.begin(); + M5.Lcd.setRotation(3); + + pinMode(correctWirePin, INPUT_PULLDOWN); + pinMode(wrongWirePin, INPUT_PULLDOWN); + + pinMode(extenderPin, OUTPUT_OPEN_DRAIN); + + Serial.begin(9600); +} + +void loop() +{ + if (hasWon){ + digitalWrite(extenderPin, LOW); + }else + digitalWrite(extenderPin, HIGH); + + if (digitalRead(correctWirePin) == HIGH && digitalRead(wrongWirePin) == HIGH) + { + reset(); + } + + Serial.println("Won: " + String(hasWon)); + Serial.println("Lost: " + String(hasLost)); + + if (hasWon || hasLost) return; + else{ + M5.Lcd.fillScreen(BLACK); + M5.Lcd.setCursor(0, 0); + M5.Lcd.setTextSize(2); + M5.Lcd.setTextColor(WHITE); + M5.Lcd.println("Waiting for cut..."); + } + + if (digitalRead(correctWirePin) == LOW) + { + M5.Lcd.fillScreen(GREEN); + hasWon = true; + } + + if (digitalRead(wrongWirePin) == LOW) + { + M5.Lcd.fillScreen(RED); + hasLost = true; + } + delay(50); +} \ No newline at end of file