#include <Arduino.h>
const int buttonPin = 32; // GPIO pin for the button
volatile int buttonState = HIGH; // current state of the button
volatile int lastButtonState = HIGH; // previous state of the button
volatile unsigned long lastDebounceTime = 0; // last time the button state was toggled
volatile unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void handleButtonClick();
void setup() {
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonPin), handleButtonClick, FALLING);
}
void loop() {
// Your main code goes here
}
void handleButtonClick() {
unsigned long currentTime = millis();
// Check for debounce
if (currentTime - lastDebounceTime > debounceDelay) {
// Read the button state
buttonState = digitalRead(buttonPin);
// Check for a valid button press
if (buttonState == LOW && lastButtonState == HIGH) {
// Determine if it's a single click or double click
if (currentTime - lastDebounceTime < 500) {
Serial.println("Double click!");
} else {
Serial.println("Single click!");
}
}
// Save the current button state
lastButtonState = buttonState;
// Save the last debounce time
lastDebounceTime = currentTime;
}
}
/*#include <SD.h>
#include <cstdio>
#define tempPin 34
const int chipSelect = 5; // Change this to the correct pin for your SD card module
const int buttonPin = 32; // the number of the pushbutton pin
const int ledPin = 12; // the number of the first LED pin
const int ledPin1 = 25; // the number of the second LED pin
// Variables to track the button state and timing
int buttonState = 0;
int lastButtonState = 0;
unsigned long buttonPressTime = 0;
unsigned long lastReleaseTime = 0;
boolean ledOn = false;
boolean buttonHeld = false;
boolean doubleClickDetected = false;
File dataFile;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
bool loggingEnabled = false;
float celsius;
float fahrenheit;
int reading;
int sensorValue;
float voltage;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(115200);
if (SD.begin(chipSelect)) {
Serial.println("SD card is initialized.");
} else {
Serial.println("Error initializing SD card.");
return;
}
}
void loop() {
reading = digitalRead(buttonPin);
sensorValue = analogRead(tempPin);
voltage = sensorValue * (5.0 / 1023.0);
celsius = (voltage - 0.5) * 100.0;
fahrenheit = (celsius * 9 / 5) + 32;
buttonState = digitalRead(buttonPin);
unsigned long currentTime = millis();
if (buttonState == HIGH && lastButtonState == LOW) {
buttonPressTime = currentTime;
buttonHeld = false;
if (doubleClickDetected) {
// If double-click detected on the last click, turn off the second LED
// digitalWrite(ledPin1, LOW);
// Serial.println("Second LED OFF");
// Close the file only if it was opened successfully
if (dataFile) {
dataFile.close();
Serial.println("File closed successfully.");
}
doubleClickDetected = false;
}
}
if (buttonState == HIGH && currentTime - buttonPressTime >= 5000 && !buttonHeld) {
ledOn = true;
//digitalWrite(ledPin, HIGH);
buttonHeld = true;
//Serial.println("Button held for 3 seconds. First LED ON");
// Restart ESP32
Serial.println("Restarting ESP32...");
ESP.restart();
}
if (buttonState == LOW && lastButtonState == HIGH) {
if (currentTime - lastReleaseTime < 300) {
// Detect a double-click within 300 milliseconds
if (!doubleClickDetected) {
// Turn on the second LED with the first double-click
//digitalWrite(ledPin1, HIGH);
//Serial.println("Second LED ON");
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if ((buttonState == HIGH)) {
loggingEnabled = !loggingEnabled;
if (loggingEnabled) {
Serial.println("Data logging started");
} else {
Serial.println("Data logging stopped");
}
delay(500);
}
}
}*/
/*if (loggingEnabled) {
reading = digitalRead(buttonPin);
sensorValue = analogRead(tempPin);
voltage = sensorValue * (5.0 / 1023.0);
celsius = (voltage - 0.5) * 100.0;
fahrenheit = (celsius * 9 / 5) + 32;
Serial.print(" Temp (Celsius): ");
Serial.print(celsius);
Serial.print(", Temp (Fahrenheit): ");
Serial.println(fahrenheit);
logData(celsius, fahrenheit);
}*/
/* if (loggingEnabled) {
int clickCounter = 0;
unsigned long lastClickTime = 0;
const unsigned long debounceDelay = 300; // Adjust this as needed
while (1) {
reading = digitalRead(buttonPin);
// Check for a button click with debounce
if (reading == HIGH && (millis() - lastClickTime) > debounceDelay) {
lastClickTime = millis();
clickCounter++;
if (clickCounter == 2) {
// Exit the loop after double-click
break;
}
}
sensorValue = analogRead(tempPin);
voltage = sensorValue * (5.0 / 1023.0);
celsius = (voltage - 0.5) * 100.0;
fahrenheit = (celsius * 9 / 5) + 32;
Serial.print(" Temp (Celsius): ");
Serial.print(celsius);
Serial.print(", Temp (Fahrenheit): ");
Serial.println(fahrenheit);
// battry_login( &celsius ,&fahrenheit);
logData(celsius, fahrenheit);
}
}
doubleClickDetected = true;
}
}
lastReleaseTime = currentTime;
}
lastButtonState = buttonState;
if (buttonState == HIGH && !buttonHeld && !doubleClickDetected) {
Serial.print("Button held for: ");
Serial.print(currentTime - buttonPressTime);
Serial.println(" ms");
}
}
void logData(float celsius, float fahrenheit) {
File dataFile = SD.open("/temperature.log", FILE_APPEND);
if (dataFile) {
dataFile.print(" Temperature (Celsius): ");
dataFile.print(celsius);
dataFile.print(", Temperature (Fahrenheit): ");
dataFile.println(fahrenheit);
//dataFile.close();
}else{
Serial.println("Error opening file for writing");
}
}*/
//2 code single print temp value
/*#include <SD.h>
#include <cstdio>
#define tempPin 34
const int chipSelect = 5; // Change this to the correct pin for your SD card module
const int buttonPin = 32; // the number of the pushbutton pin
const int ledPin = 12; // the number of the first LED pin
const int ledPin1 = 25; // the number of the second LED pin
// Variables to track the button state and timing
int buttonState = 0;
int lastButtonState = 0;
unsigned long buttonPressTime = 0;
unsigned long lastReleaseTime = 0;
boolean ledOn = false;
boolean buttonHeld = false;
boolean doubleClickDetected = false;
File dataFile;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
bool loggingEnabled = false;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(115200);
if (SD.begin(chipSelect)) {
Serial.println("SD card is initialized.");
} else {
Serial.println("Error initializing SD card.");
return;
}
}
void loop() {
float celsius;
float fahrenheit;
int reading = digitalRead(buttonPin);
int sensorValue = analogRead(tempPin);
float voltage = sensorValue * (5.0 / 1023.0);
celsius = (voltage - 0.5) * 100.0;
fahrenheit = (celsius * 9 / 5) + 32;
buttonState = digitalRead(buttonPin);
unsigned long currentTime = millis();
if (buttonState == HIGH && lastButtonState == LOW) {
buttonPressTime = currentTime;
buttonHeld = false;
if (doubleClickDetected) {
// If double-click detected on the last click, turn off the second LED
digitalWrite(ledPin1, LOW);
Serial.println("Second LED OFF");
// Close the file only if it was opened successfully
if (dataFile) {
dataFile.close();
Serial.println("File closed successfully.");
}
doubleClickDetected = false;
}
}
if (buttonState == HIGH && currentTime - buttonPressTime >= 5000 && !buttonHeld) {
ledOn = true;
digitalWrite(ledPin, HIGH);
buttonHeld = true;
Serial.println("Button held for 3 seconds. First LED ON");
// Restart ESP32
Serial.println("Restarting ESP32...");
ESP.restart();
}
if (buttonState == LOW && lastButtonState == HIGH) {
if (currentTime - lastReleaseTime < 300) {
// Detect a double-click within 300 milliseconds
if (!doubleClickDetected) {
// Turn on the second LED with the first double-click
digitalWrite(ledPin1, HIGH);
Serial.println("Second LED ON");
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if ((buttonState == HIGH)) {
loggingEnabled = !loggingEnabled;
if (loggingEnabled) {
Serial.println("Data logging started");
} else {
Serial.println("Data logging stopped");
}
delay(500);
}
}
}
if (loggingEnabled) {
Serial.print(" Temp (Celsius): ");
Serial.print(celsius);
Serial.print(", Temp (Fahrenheit): ");
Serial.println(fahrenheit);
logData(celsius, fahrenheit);
}
doubleClickDetected = true;
}
}
lastReleaseTime = currentTime;
}
lastButtonState = buttonState;
if (buttonState == HIGH && !buttonHeld && !doubleClickDetected) {
Serial.print("Button held for: ");
Serial.print(currentTime - buttonPressTime);
Serial.println(" ms");
}
}
void logData(float celsius, float fahrenheit) {
File dataFile = SD.open("/temperature.log", FILE_APPEND);
if (dataFile) {
dataFile.print(" Temperature (Celsius): ");
dataFile.print(celsius);
dataFile.print(", Temperature (Fahrenheit): ");
dataFile.println(fahrenheit);
//dataFile.close();
}else{
Serial.println("Error opening file for writing");
}
}*/