diff --git a/ModuleButton/ModuleButton.ino b/ModuleButton/ModuleButton.ino index 1ed5831..68e833b 100644 --- a/ModuleButton/ModuleButton.ino +++ b/ModuleButton/ModuleButton.ino @@ -7,17 +7,24 @@ 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("boost-v2", "crystalize"); wifiMulti.addAP("AIRPORT-FREE-WIFI-NEW", "alexalex"); wifiMulti.addAP("Next-Guest", ""); + wifiMulti.addAP("boost-v2", "crystalize"); long startStartedWifi = millis(); @@ -32,8 +39,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(){ @@ -46,18 +57,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() @@ -73,6 +102,7 @@ void setup() pinMode(led1, OUTPUT_OPEN_DRAIN); pinMode(led2, OUTPUT_OPEN_DRAIN); + pinMode(retractPin, OUTPUT_OPEN_DRAIN); Serial.begin(115200); @@ -81,10 +111,6 @@ void setup() M5.Lcd.fillScreen(BLACK); } -long pressedTime = 0; -long releasedTime = 0; -bool pressed = false; - void loop() { drawUI(); @@ -122,5 +148,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..7e93a4f --- /dev/null +++ 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