diff --git a/ModuleButton/ModuleButton.ino b/ModuleButton/ModuleButton.ino new file mode 100644 index 0000000..c584e10 --- /dev/null +++ b/ModuleButton/ModuleButton.ino @@ -0,0 +1,125 @@ +#include +#include +#include + +WiFiMulti wifiMulti; +HTTPClient http; + +#define buttonPin 26 +#define led1 25 +#define led2 0 + +#define longPressThreshhold 150 + +bool lightA, lightB = false; + +void initRandomSeed() +{ + wifiMulti.addAP("AIRPORT-FREE-WIFI-NEW", "alexalex"); + wifiMulti.addAP("Next-Guest", ""); + + long startStartedWifi = millis(); + + while ((wifiMulti.run() != WL_CONNECTED)) + { + Serial.println("Connecting to wifi..."); + } + + randomSeed(millis() - startStartedWifi); +} + +void reset(){ + //assert(false); + + lightA = random(2) == 1 ? true : false; + lightB = random(2) == 1 ? true : false; +} + +void drawUI(){ + M5.Lcd.setCursor(0, 0); + M5.Lcd.setTextSize(4); + M5.Lcd.println("Light A: " + String(lightA)); + M5.Lcd.println("Light B: " + String(lightB)); + M5.Lcd.setTextSize(2); + M5.Lcd.println("Answer is long press?: " + String(lightA ^ lightB)); +} + +void enterWinLoop(){ + while (true){ + delay(1000); + digitalWrite(led1, LOW); + digitalWrite(led2, LOW); + delay(1000); + digitalWrite(led1, HIGH); + digitalWrite(led2, HIGH); + + if (digitalRead(buttonPin) == HIGH){ + + } + } +} + +void setup() +{ + M5.Lcd.begin(); + M5.Lcd.setRotation(1); + M5.Lcd.setCursor(0, 0); + M5.Lcd.setTextSize(4); + M5.Lcd.println("Loading.."); + + pinMode(39, INPUT_PULLDOWN); + pinMode(buttonPin, INPUT_PULLDOWN); + + pinMode(led1, OUTPUT_OPEN_DRAIN); + pinMode(led2, OUTPUT_OPEN_DRAIN); + + Serial.begin(115200); + + initRandomSeed(); + reset(); + M5.Lcd.fillScreen(BLACK); +} + +long pressedTime = 0; +long releasedTime = 0; +bool pressed = false; + +void loop() +{ + drawUI(); + + if (digitalRead(39) == LOW){ + reset(); + } + + if (lightA) + digitalWrite(led1, LOW); + else + digitalWrite(led1, HIGH); + if (lightB) + digitalWrite(led2, LOW); + else + digitalWrite(led2, 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 < longPressThreshhold){ + Serial.println("Short press detected!"); + }else{ + Serial.println("Long press detected!"); + } + } +}