const int pushButtonPin = 26; // Pin number where the push button is connected
bool buttonState = HIGH; // Variable to store the current state of the push button
bool lastButtonState = HIGH; // Variable to store the previous state of the push button
unsigned long lastDebounceTime = 0; // Variable for debounce delay
unsigned long debounceDelay = 50; // Debounce time in milliseconds
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(pushButtonPin, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
int reading = digitalRead(pushButtonPin); // Read the state of the push button
//Serial.println(reading);
// Check if the button state has changed and debounce the button
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
// Generate OTP if the button state has been stable for the debounce delay
if ((millis() - lastDebounceTime) > debounceDelay) {
// Check if the button state is LOW (pressed)
if (reading == LOW) {
// Generate and print a 6-digit random OTP
int otpLength = 6; // You can change the OTP length as per your requirement
String otp = generateOTP(otpLength);
Serial.println("Generated OTP: " + otp);
}
}
// Update the last button state
lastButtonState = reading;
}
String generateOTP(int length) {
String otp = "";
for (int i = 0; i < length; ++i) {
otp += String(random(1, 10)); // Generates random digits from 0 to 9
}
return otp;
}