/*#define BLYNK_TEMPLATE_ID "TMPL6pJx1voDR"
#define BLYNK_TEMPLATE_NAME "Wokwi"
#define BLYNK_AUTH_TOKEN "QbBOKn_xLsRdNOj1VUPQlXqHMimKBFxQ"
*/
//#include <WiFi.h>
//#include <WiFiClient.h>
#include <TM1637Display.h>
//#include <BlynkSimpleEsp32.h>
/*
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
*/
#define CLK 22
#define DIO 23
//#include <WiFi.h>
// Define the pin connected to the push button
const int buttonPin = 32;
// Variable to store the state of the button
int buttonState = 1;
// Variable to store the previous state of the button
int lastButtonState = 1;
// Variable to store the time when the button was pressed
unsigned long buttonPressStartTime = 0;
// Variable to store the current value of i
int i = 0;
int value;
int value1;
TM1637Display display(CLK, DIO);
/*
BlynkTimer timer;
BLYNK_WRITE(V0) ///function blynk ลักษณะ input ส่งคำสั่งจาก blynk ลงมา
{
value = param.asInt();
if (value==1)
{
i=i+1;
display.clear();
display.showNumberDec(i,false);
}
//Blynk.virtualWrite(V1, value);
}
*/
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(2, OUTPUT);
Serial.begin(9600);
display.setBrightness(3);
display.clear();
display.showNumberDec(0,false);
/*
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("Wifi Connected!");
*/
/*WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Blynk.begin(BLYNK_AUTH_TOKEN,ssid,pass);
/*
Serial.print("Connecting to Blynk");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("Blynk Connected!");
*/
}
void loop() {
//Blynk.run();
buttonState = digitalRead(buttonPin);
// Check if the button state has changed
if (buttonState != lastButtonState) {
// If the button is pressed (HIGH), record the start time of the button press
//digitalWrite(V0,HIGH);
if (buttonState == LOW) {
//digitalWrite(V0,LOW);
buttonPressStartTime = millis();
} else { // If the button is released
// Calculate the duration of the button press
unsigned long buttonPressDuration = millis() - buttonPressStartTime;
// If the button was pressed for more than 3 seconds, reset the value of i
if (buttonPressDuration >= 3000) {
i = 0;
Serial.println("RESET");
display.clear();
display.showNumberDec(i,false);
digitalWrite(2,0);
//Blynk.virtualWrite(V1, 0);
//Blynk.virtualWrite(V2, i);
} else { // If the button was pressed for less than 3 seconds, increment the value of i
i += 1;
Serial.println(i);
display.clear();
display.showNumberDec(i,false);
digitalWrite(2,1);
//Blynk.virtualWrite(V1, 1);
//Blynk.virtualWrite(V2, i);
}
}
// Save the current button state for comparison in the next iteration
lastButtonState = buttonState;
// A small delay to debounce the button (optional)
delay(50);
}
}