Module button done

This commit is contained in:
BOT Alex 2024-05-14 12:25:23 +02:00
parent 462690a82d
commit e910c2451e
2 changed files with 48 additions and 9 deletions

View File

@ -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();
}
}

View File